fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct TreeNode
  5. {
  6. int data;
  7. struct TreeNode* left;
  8. struct TreeNode* right;
  9. };
  10.  
  11. typedef struct TreeNode TreeNode;
  12.  
  13. TreeNode* createNode(int data)
  14. {
  15. TreeNode* temp = (TreeNode *)malloc(sizeof(TreeNode));
  16. temp->data = data;
  17. temp->left = temp->right = NULL;
  18. return temp;
  19. }
  20.  
  21. void printTree(TreeNode* root, int space)
  22. {
  23. if (root == NULL)
  24. return;
  25.  
  26. // Increase distance between levels
  27. int COUNT = 5;
  28. space += COUNT;
  29.  
  30. // Print right child first
  31. printTree(root->right, space);
  32.  
  33. // Print current node after space count
  34. printf("\n");
  35. for (int i = COUNT; i < space; i++)
  36. printf(" ");
  37. printf("%d\n", root->data);
  38.  
  39. // Print left child
  40. printTree(root->left, space);
  41. }
  42.  
  43.  
  44. void preorder(TreeNode* root)
  45. {
  46. if(root==NULL) return;
  47.  
  48. printf("%d ", root->data);
  49. preorder(root->left);
  50. preorder(root->right);
  51. }
  52.  
  53.  
  54. void inorder(TreeNode* root)
  55. {
  56. if(root==NULL) return;
  57.  
  58. inorder(root->left);
  59.  
  60. printf("%d ", root->data);
  61.  
  62. inorder(root->right);
  63. }
  64.  
  65. void postorder(TreeNode* root)
  66. {
  67. if(root==NULL) return;
  68.  
  69. postorder(root->left);
  70. postorder(root->right);
  71. printf("%d ", root->data);
  72. }
  73.  
  74. int height(TreeNode* root)
  75. {
  76. if(root==NULL) /// root NULL
  77. {
  78. return 0;
  79. }
  80. else if(root->left==NULL && root->right==NULL) /// child count: 0
  81. {
  82. return 0;
  83. }
  84. else
  85. {
  86. if(root->left!=NULL && root->right!=NULL) /// child count: 2
  87. {
  88. int left_side_height = height(root->left);
  89. int right_side_height = height(root->right);
  90.  
  91. if(left_side_height>right_side_height)
  92. {
  93. return left_side_height + 1;
  94. }
  95. else
  96. return right_side_height + 1;
  97.  
  98. }
  99. else if(root->left!=NULL) /// child count: 1 (Left Child)
  100. {
  101. int left_side_height = height(root->left);
  102. return left_side_height + 1;
  103. }
  104. else /// child count: 1 (Right Child)
  105. {
  106. int right_side_height = height(root->right);
  107. return right_side_height + 1;
  108. }
  109. }
  110. }
  111.  
  112.  
  113. int findValue(TreeNode* root, int value)
  114. {
  115. if(root==NULL) return 0;
  116. else if(root->data==value) return 1;
  117. else
  118. {
  119. int left_answer = findValue(root->left, value);
  120. int right_answer = findValue(root->right, value);
  121.  
  122. return left_answer || right_answer;
  123. }
  124. }
  125.  
  126. int countNodes(TreeNode* root)
  127. {
  128. if(root==NULL) return 0;
  129. else
  130. {
  131. int left_answer = countNodes(root->left);
  132. int right_answer = countNodes(root->right);
  133. return left_answer + right_answer + 1;
  134. }
  135. }
  136.  
  137. int countLeaves(TreeNode* root)
  138. {
  139. if(root==NULL) return 0;
  140. else if(root->left==NULL && root->right==NULL) return 1;
  141. else
  142. {
  143. int left_answer = countLeaves(root->left);
  144. int right_answer = countLeaves(root->right);
  145. return left_answer + right_answer;
  146. }
  147. }
  148.  
  149. int main()
  150. {
  151. TreeNode* root = createNode(10);
  152.  
  153. root->left = createNode(20);
  154. root->right = createNode(30);
  155.  
  156. root->left->left = createNode(40);
  157. root->left->right = createNode(50);
  158.  
  159. root->right->left = createNode(60);
  160. root->right->right = createNode(70);
  161.  
  162. root->right->right->left = createNode(80);
  163.  
  164. printf("Tree:\n");
  165. printTree(root, 0);
  166.  
  167. printf("\n");
  168.  
  169. printf("Preorder Traversal Sequence: ");
  170. preorder(root);
  171. printf("\n");
  172.  
  173. printf("Inorder Traversal Sequence: ");
  174. inorder(root);
  175. printf("\n");
  176.  
  177. printf("Postorder Traversal Sequence: ");
  178. postorder(root);
  179. printf("\n");
  180.  
  181.  
  182.  
  183. printf("Height of the tree: %d\n", height(root));
  184. printf("\n");
  185.  
  186. return 0;
  187. }
  188.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Tree:

          70

               80

     30

          60

10

          50

     20

          40

Preorder Traversal Sequence: 10 20 40 50 30 60 70 80 
Inorder Traversal Sequence: 40 20 50 10 60 30 80 70 
Postorder Traversal Sequence: 40 50 20 60 80 70 30 10 
Height of the tree: 3