Advertisement
S_h_u_v_r_o

Binary Search Tree

Mar 30th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef struct node
  4. {
  5. int data;
  6.  
  7. struct node *left, *right;
  8. } x;
  9.  
  10. x *root=NULL;
  11. x *temp=NULL;
  12. x *newx;
  13.  
  14.  
  15. void insert(x *temp, x *newx)
  16. {
  17. if (temp->data > newx->data)
  18. {
  19. if (temp->left == NULL)
  20. {
  21. temp->left=newx;
  22. }
  23. else
  24. {
  25. insert(temp->left, newx);
  26. }
  27. }
  28. else
  29. {
  30. if (temp->right == NULL)
  31. {
  32. temp->right=newx;
  33. }
  34. else
  35. {
  36. insert(temp->right, newx);
  37. }
  38. }
  39. }
  40.  
  41. int maximum(x *temp)
  42. {
  43. while (temp->right != NULL)
  44. {
  45. temp=temp->right;
  46. }
  47. printf("Maximum : ");
  48. printf("%d\n", temp->data);
  49. }
  50.  
  51. int minimum(x *temp)
  52. {
  53. while (temp->left != NULL)
  54. {
  55. temp= temp->left;
  56. }
  57.  
  58. printf("Minimum : ");
  59.  
  60. printf("%d\n", temp->data);
  61. }
  62.  
  63. int search(x *temp, int x)
  64. {
  65. if (temp->data == x)
  66. {
  67. printf("Found\n");
  68. }
  69. else if (temp->left == NULL && temp->right == NULL)
  70. {
  71. printf("Not found\n");
  72. }
  73. else if (temp->data > x)
  74. {
  75. search(temp->left, x);
  76. }
  77. else if (temp->data <=x)
  78. {
  79. search(temp->right, x);
  80. }
  81. }
  82.  
  83. int preorder(x *temp)
  84. {
  85. if (temp != NULL)
  86. {
  87.  
  88. printf("%d ", temp->data);
  89.  
  90. preorder(temp->left);
  91. preorder(temp->right);
  92. }
  93.  
  94. }
  95.  
  96. int inorder(x *temp)
  97. {
  98. if (temp != NULL)
  99. {
  100.  
  101. inorder(temp->left);
  102.  
  103. printf("%d ", temp->data);
  104.  
  105. inorder(temp->right);
  106. }
  107.  
  108. }
  109.  
  110. int postorder(x *temp)
  111. {
  112. if (temp != NULL)
  113. {
  114.  
  115. printf("%d ", temp->data);
  116.  
  117. postorder(temp->left);
  118.  
  119. postorder(temp->right);
  120.  
  121. }
  122.  
  123. }
  124.  
  125.  
  126.  
  127. int main ()
  128. {
  129. int i, n;
  130.  
  131. scanf("%d", &n);
  132.  
  133. for (i=0; i<n; i++)
  134. {
  135. newx = (x*) malloc(sizeof(x));
  136.  
  137. scanf("%d", &newx->data);
  138.  
  139. newx->right=NULL;
  140. newx->left=NULL;
  141.  
  142. if (root==NULL)
  143. {
  144. root=newx;
  145. }
  146. else
  147. {
  148. insert(root, newx);
  149. }
  150. }
  151.  
  152. maximum(root);
  153.  
  154. minimum(root);
  155.  
  156. int x, p;
  157.  
  158. scanf("%d", &x);
  159.  
  160. for (i=0; i<x; i++)
  161. {
  162. scanf("%d", &p);
  163.  
  164. search(root, p);
  165.  
  166. }
  167. printf("\nPreorder\n");
  168. preorder(root);
  169.  
  170. printf("\n\n\Inorder\n");
  171. inorder(root);
  172.  
  173. printf("\n\nPostorder\n");
  174. postorder(root);
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement