fork(2) download
  1. #include<stdio.h>
  2. #define MIN -65535
  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...w.studyalgorithms,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 findMaxInTree(struct binaryTreeNode * root)
  60. {
  61. // A variable for root value
  62. int root_val;
  63.  
  64. // Variable to store values in left and right tree
  65. int left, right;
  66.  
  67. // Initialize it with a minimum value
  68. int max = MIN;
  69.  
  70. if(root != NULL)
  71. {
  72. // Get the root value
  73. root_val = root -> data;
  74.  
  75. // Find the maximum value in left sub-tree
  76. left = findMaxInTree(root -> left);
  77.  
  78. // Find the maximum value in right sub-tree
  79. right = findMaxInTree(root -> right);
  80.  
  81. // Now find the largest of 3 values
  82. // Find which is big among left and right
  83. if(left > right)
  84. max = left;
  85. else
  86. max = right;
  87.  
  88. // Compare the max with root value
  89. if(root_val > max)
  90. max = root_val;
  91. }
  92.  
  93. return max;
  94. }
  95.  
  96. int findMaxInTreeNonRecursive(struct binaryTreeNode * root)
  97. {
  98. // Initialize max with a very less value;
  99. int max = MIN;
  100.  
  101. // Level Order Traversal
  102. struct binaryTreeNode * temp = NULL;
  103. struct queue * Q = NULL;
  104. if(root == NULL)
  105. return;
  106.  
  107. Q = enQueue(Q, root);
  108.  
  109. while(!isQueueEmpty(Q))
  110. {
  111. temp = Q -> front -> data;
  112.  
  113. Q = deQueue(Q);
  114.  
  115. // Find the max value
  116. if(temp -> data > max)
  117. max = temp -> data;
  118.  
  119. if(temp -> left)
  120. Q = enQueue(Q, temp -> left);
  121. if(temp -> right)
  122. Q = enQueue(Q, temp -> right);
  123. }
  124. free(Q);
  125.  
  126. return max;
  127. }
  128.  
  129. // Test the above functions
  130. int main(void)
  131. {
  132. // Initialize the tree
  133. struct binaryTreeNode * root = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  134. root-> data = 1;
  135. struct binaryTreeNode * l = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  136. l -> data = 2;
  137. struct binaryTreeNode * ll = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  138. ll -> data = 4;
  139. ll -> left = ll -> right = NULL;
  140. struct binaryTreeNode * lr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  141. lr -> data = 5;
  142. lr -> left = lr -> right = NULL;
  143. l -> left = ll;
  144. l -> right = lr;
  145. struct binaryTreeNode * r = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  146. r -> data = 3;
  147. struct binaryTreeNode * rl = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  148. rl -> data = 6;
  149. rl -> left = rl -> right = NULL;
  150. struct binaryTreeNode * rr = (struct binaryTreeNode *)malloc(sizeof(struct binaryTreeNode));
  151. rr -> data = 7;
  152. rr -> left = rr -> right = NULL;
  153. r -> left = rl;
  154. r -> right = rr;
  155. root -> left = l;
  156. root -> right = r;
  157.  
  158. // Non-recursive version
  159. printf("max = %d",findMaxInTree(root));
  160.  
  161. // Recursive version
  162. printf("max = %d",findMaxInTreeNonRecursive(root));
  163.  
  164. return 0;
  165. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
max = 7max = 7