fork download
  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. struct binaryTreeNode{
  5. int data;
  6. struct binaryTreeNode * left;
  7. struct binaryTreeNode * right;
  8. };
  9.  
  10. struct node
  11. {
  12. struct binaryTreeNode * data;
  13. struct node * next;
  14. };
  15.  
  16. struct queue
  17. {
  18. struct node * front;
  19. struct node * rear;
  20. };
  21.  
  22. struct queue * enQueue(struct queue * q, struct binaryTreeNode * num)
  23. {
  24. struct node * temp = (struct node*)malloc(sizeof(struct node));
  25. temp -> data = num;
  26. temp -> next = NULL;
  27. if(q == NULL)
  28. {
  29. q = (struct queue*)malloc(sizeof(struct queue));
  30. q -> front = temp;
  31. }
  32. else
  33. q -> rear -> next = temp;
  34. q -> rear = temp;
  35. //Code obtained from http://w...content-available-to-author-only...s.com
  36. //Feel free to copy but please acknowledge the site wherever possible
  37. return q;
  38. }
  39.  
  40. struct queue * deQueue(struct queue * q)
  41. {
  42. struct node * temp = q->front;
  43. q -> front = q->front->next;
  44. free(temp);
  45. if(q->front == NULL)
  46. return NULL;
  47. else
  48. return q;
  49. }
  50.  
  51. int isQueueEmpty(struct queue * q)
  52. {
  53. if(q)
  54. return 0;
  55. else
  56. return 1;
  57. }
  58.  
  59. int heightOfBinaryTreeRecursive(struct binaryTreeNode * root)
  60. {
  61. // Initialize 2 variables for left and right height
  62. int leftHeight;
  63. int rightHeight;
  64.  
  65. // The terminating case
  66. if(root == NULL)
  67. return 0;
  68. else
  69. {
  70. // Find the depth of left sub-tree
  71. leftHeight = heightOfBinaryTreeRecursive(root -> left);
  72.  
  73. // Find the depth of right sub-tree
  74. rightHeight = heightOfBinaryTreeRecursive(root -> right);
  75.  
  76. // Compare the two heights and return the larger of two + 1
  77. if(leftHeight > rightHeight)
  78. {
  79. // Return the leftHeight + 1
  80. return (leftHeight + 1);
  81. }
  82. else
  83. {
  84. // Return the rightHeight + 1
  85. return (rightHeight + 1);
  86. }
  87. }
  88. }
  89.  
  90. int heightOfBinaryTree(struct binaryTreeNode * root)
  91. {
  92. // Level order traversal
  93. struct binaryTreeNode * temp = NULL;
  94. struct queue * Q = NULL;
  95.  
  96. // Maintain a level count
  97. int level = 0;
  98.  
  99. if(root == NULL)
  100. return 0;
  101.  
  102. Q = enQueue(Q, root);
  103. // Now the first level will end over here,
  104. // So append a NULL node
  105. Q = enQueue(Q, NULL);
  106.  
  107. while(!isQueueEmpty(Q))
  108. {
  109. temp = Q -> front -> data;
  110. Q = deQueue(Q);
  111.  
  112. // If we encounter a NULL, that means an end of a level
  113. // And we need to increment the level count
  114. if(temp == NULL)
  115. {
  116. // Put the marker for next level also
  117. if(!isQueueEmpty(Q))
  118. Q = enQueue(Q, NULL);
  119.  
  120. level++;
  121. }
  122. else
  123. {
  124. // We continue with the level order traversal
  125. if(temp -> left)
  126. Q = enQueue(Q, temp -> left);
  127. if(temp -> right)
  128. Q = enQueue(Q, temp -> right);
  129. }
  130. }
  131. // Delete the queue
  132. free(Q);
  133.  
  134. // Now return the count
  135. return level;
  136. }
  137.  
  138. // Test the above functions
  139. int main(void)
  140. {
  141. // Initialize the tree
  142. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  143. root-> data = 1;
  144. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  145. l -> data = 2;
  146. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  147. ll -> data = 4;
  148. ll -> left = ll -> right = NULL;
  149. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  150. lr -> data = 5;
  151. lr -> left = lr -> right = NULL;
  152. l -> left = ll;
  153. l -> right = lr;
  154. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  155. r -> data = 3;
  156. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  157. rl -> data = 6;
  158. rl -> left = rl -> right = NULL;
  159. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  160. rr -> data = 7;
  161. rr -> left = rr -> right = NULL;
  162. r -> left = rl;
  163. r -> right = rr;
  164. root -> left = l;
  165. root -> right = r;
  166.  
  167. printf("Height of the binary tree = %d",heightOfBinaryTree(root));
  168.  
  169. return 0;
  170. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Height of the binary tree = 3