fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct TreeNode
  4. {
  5. int data;
  6. TreeNode* left;
  7. TreeNode* right;
  8. };
  9.  
  10.  
  11. void printTree(TreeNode* root, int space = 0, int indent = 4)
  12. {
  13. if (root == nullptr)
  14. return;
  15.  
  16.  
  17. space += indent;
  18. printTree(root->right, space);
  19.  
  20. printf("\n");
  21. for (int i = 0; i < space; i++)
  22. {
  23. printf(" ");
  24. }
  25. printf("%d\n", root->data);
  26.  
  27. printTree(root->left, space);
  28. }
  29.  
  30. TreeNode* createNode(int data)
  31. {
  32. TreeNode* temp = (TreeNode *)malloc(sizeof(TreeNode));
  33. temp->data = data;
  34. temp->left = temp->right = NULL;
  35. return temp;
  36. }
  37.  
  38. TreeNode* insertion_BST(TreeNode* root, int data)
  39. {
  40. if(root==NULL)
  41. {
  42. root = createNode(data);
  43. return root;
  44. }
  45. else
  46. {
  47. if(data>root->data)
  48. {
  49. root->right = insertion_BST(root->right, data);
  50. }
  51. else if(data<root->data)
  52. {
  53. root->left = insertion_BST(root->left, data);
  54. }
  55.  
  56. return root;
  57. }
  58. }
  59.  
  60.  
  61. TreeNode* search_BST(TreeNode* root, int key)
  62. {
  63. if(root==NULL)
  64. return NULL;
  65. else
  66. {
  67. if(key==root->data)
  68. {
  69. return root;
  70. }
  71. else if(key>root->data)
  72. {
  73. TreeNode* position = search_BST(root->right, key);
  74. return position;
  75. }
  76. else
  77. {
  78. TreeNode* position = search_BST(root->left, key);
  79. return position;
  80. }
  81. }
  82. }
  83.  
  84.  
  85. bool search_BST_bool(TreeNode* root, int key)
  86. {
  87. if(root==NULL)
  88. return false;
  89. else
  90. {
  91. if(key==root->data)
  92. {
  93. return true;
  94. }
  95. else if(key>root->data)
  96. {
  97. bool friend_answer = search_BST(root->right, key);
  98. return friend_answer;
  99. }
  100. else
  101. {
  102. bool friend_answer = search_BST(root->left, key);
  103. return friend_answer;
  104. }
  105. }
  106. }
  107.  
  108.  
  109. int maximum(int a, int b)
  110. {
  111. return a > b ? a : b;
  112. }
  113.  
  114.  
  115. int height(TreeNode* root)
  116. {
  117. if((root==NULL) || (root->left==NULL && root->right==NULL))
  118. {
  119. return 0;
  120. }
  121. else
  122. {
  123. int left_child_height = height(root->left);
  124. int right_child_height = height(root->right);
  125. return maximum(left_child_height, right_child_height) + 1;
  126. }
  127. }
  128.  
  129. int main()
  130. {
  131. TreeNode* root = NULL;
  132.  
  133. root = insertion_BST(root, 15);
  134. root = insertion_BST(root, 13);
  135. root = insertion_BST(root, 14);
  136. root = insertion_BST(root, 18);
  137. root = insertion_BST(root, 16);
  138.  
  139. root = insertion_BST(root, 100);
  140. root = insertion_BST(root, 200);
  141. root = insertion_BST(root, 300);
  142.  
  143.  
  144.  
  145.  
  146. printTree(root);
  147.  
  148.  
  149. printf("Your height is: %d\n",height(root));
  150.  
  151. return 0;
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. }
  159.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
                    300

                200

            100

        18

            16

    15

            14

        13
Your height is: 4