Advertisement
Guest User

BST

a guest
Nov 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct BST{
  5. int data;
  6. BST * left;
  7. BST * right;
  8. };
  9.  
  10. #include "bst_helper.cpp"
  11.  
  12.  
  13. BST * insert_Node(BST * root, int value)
  14. {
  15. if(root == NULL)
  16. {
  17. BST * temp = (BST*) malloc(sizeof(BST));
  18. temp->data = value;
  19. temp->left = NULL;
  20. temp->right = NULL;
  21. return temp;
  22. }
  23. if (value <= root->data)
  24. root->left = insert_Node(root->left, value);
  25. else if (value > root->data)
  26. root->right = insert_Node(root->right, value);
  27.  
  28. return root;
  29. }
  30.  
  31. BST * minimum_Node(BST * root)
  32. {
  33. while(root->left != NULL)
  34. root = root->left;
  35.  
  36. return root;
  37. }
  38.  
  39.  
  40. BST * delete_Node(BST * root, int key)
  41. {
  42. if(root == NULL)
  43. return root;
  44. else if(key < root->data)
  45. root->left = delete_Node(root->left, key);
  46. else if(key > root->data)
  47. root->right = delete_Node(root->right, key);
  48. else
  49. {
  50. BST * temp = (BST*) malloc(sizeof(BST));
  51.  
  52. // no child
  53. if((root->left == NULL) && (root->right == NULL))
  54. {
  55. free(root);
  56. root = NULL;
  57. }
  58.  
  59. // 1 child (right)
  60. else if(root->left == NULL)
  61. {
  62. temp = root;
  63. root = root->right;
  64. free(temp);
  65. }
  66. // 1 child (left)
  67. else if(root->right == NULL)
  68. {
  69. temp = root;
  70. root = root->left;
  71. free(temp);
  72. }
  73. // 2 children
  74. else
  75. {
  76. temp = minimum_Node(root->right);
  77. root->data = temp->data;
  78. root->right = delete_Node(root->right, temp->data);
  79. }
  80. }
  81. return root;
  82. }
  83.  
  84. void inorder(BST * root)
  85. {
  86. if(root == NULL)
  87. return;
  88.  
  89. inorder(root->left);
  90. printf("%d ", root->data);
  91. inorder(root->right);
  92. }
  93.  
  94. void level_order(BST * root)
  95. {
  96. if(root == NULL)
  97. return;
  98.  
  99.  
  100. Queue * q = (Queue*)(malloc(sizeof(Queue)));
  101. q = NULL;
  102. q = enqueue(q, root);
  103.  
  104. BST * cur = (BST*) malloc(sizeof(BST));
  105.  
  106. while(!isEmpty(q))
  107. {
  108. cur = top(q);
  109. q = dequeue(q);
  110. printf("%d ", cur->data);
  111. if(cur->left != NULL)
  112. q = enqueue(q, cur->left);
  113. if(cur->right != NULL)
  114. q = enqueue(q, cur->right);
  115. }
  116.  
  117. }
  118.  
  119.  
  120. bool search_key(BST * root, int key)
  121. {
  122. //ITERATIVE
  123. while(root!=NULL)
  124. {
  125. if(root->data == key)
  126. return true;
  127. if(key > root->data)
  128. root = root->right;
  129. else if(key < root->data)
  130. root = root->left;
  131. }
  132. return false;
  133. }
  134.  
  135.  
  136.  
  137. int main()
  138. {
  139. BST * root = (BST*) malloc(sizeof(BST));
  140. root = NULL;
  141.  
  142. root = insert_Node(root, 90);
  143.  
  144. return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement