Advertisement
lordasif

tree

Jul 30th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9. int val;
  10. Node * left;
  11. Node *right;
  12. Node *parent;
  13.  
  14. Node(int v)
  15. { val = v;
  16. left = right = NULL;
  17. parent = NULL;
  18. }
  19. };
  20.  
  21.  
  22. class BST
  23. {
  24.  
  25. public:
  26. Node * root;
  27. BST()
  28. {
  29. root = NULL;
  30. }
  31.  
  32. void inorder()
  33. {
  34. if ( root==NULL)
  35. printf("Tree is empty ");
  36. else
  37. inorder (root);
  38. printf("\n");
  39. }
  40.  
  41. Node * findMin(Node * x)
  42. {
  43. while(x->left!=NULL)
  44. x= x->left;
  45. return x;
  46. }
  47.  
  48. void inorder(Node * r)
  49. {
  50. if (r != NULL)
  51. {
  52.  
  53. inorder(r->left);
  54. printf(" %d ", r->val);
  55. inorder(r->right);
  56. }
  57. printf("\n");
  58. }
  59.  
  60.  
  61.  
  62.  
  63. void iinsert ( Node * z)
  64.  
  65. {
  66. Node * x = root;
  67. Node * y = NULL;
  68. y = NULL;
  69. x=root;
  70. while (x != NULL)
  71. {
  72. y=x;
  73. if( z->val < x->val )
  74. x= x->left;
  75. else x= x->right;
  76. }
  77. z->parent = y;
  78. if (y == NULL)
  79. root = z;
  80. else if (z->val < y->val)
  81. y->left = z;
  82. else
  83. y->right = z;
  84. }
  85.  
  86.  
  87.  
  88. void insert (int val)
  89.  
  90. {
  91. //root = insert(root, val, NULL);
  92. Node * temp = new Node (val);
  93. iinsert(temp);
  94. }
  95.  
  96.  
  97. Node * insert(Node * r, int val, Node * p)
  98. {
  99. if(r==NULL)
  100. {
  101. Node * newnode = new Node(val);
  102. newnode->parent = p;
  103. return newnode;
  104. }
  105.  
  106. else if ( val < r ->val)
  107. {
  108. r->left = insert (r->left, val, r) ;
  109. }
  110.  
  111. else ( r-> val <val);
  112.  
  113. r->right = insert (r->right, val,r);
  114.  
  115.  
  116. return r;
  117. }
  118.  
  119. void transplant(Node * u, Node * v)
  120. {
  121. if (u->parent == NULL)
  122. root = v;
  123. else if(u == u->parent->left)
  124. u->parent->left = v;
  125. else u->parent->right = v;
  126. if(v!=NULL)
  127. v->parent = u->parent;
  128. }
  129.  
  130. void search(int val)
  131. {
  132. Node* temp = search(root, val);
  133. if (temp == NULL)
  134. printf("%d not found", val);
  135. else
  136. printf("\n%d found", val);
  137. }
  138.  
  139.  
  140. Node * search( Node * x, int k)
  141. {
  142.  
  143. if( x == NULL || k == x->val)
  144. return x;
  145. if (k < x-> val)
  146. return search (x->left, k);
  147. else
  148. return search (x->right, k);
  149.  
  150. }
  151.  
  152.  
  153.  
  154. void deleteNode(Node * z)
  155.  
  156. {
  157. if( z->left == NULL)
  158. {
  159. transplant(z, z->right);
  160. }
  161.  
  162. else if(z->right == NULL)
  163. {
  164. transplant(z, z->left);
  165. }
  166.  
  167. else
  168. {
  169. Node * y = findMin(z->right);
  170. if (y->parent != z)
  171. {
  172. transplant ( y, y->right);
  173. y->right->parent= y;
  174. }
  175. transplant(z,y);
  176. y->left = z->left;
  177. y->left->parent = y;
  178. }
  179.  
  180. }
  181. void deleteByVal(int val)
  182. {
  183. Node * temp = search(root, val);
  184. if(temp == NULL)
  185. printf("Value not found");
  186. else deleteNode (temp);
  187. }
  188.  
  189. };
  190.  
  191. int main()
  192. {
  193. BST b1;
  194. //Node *r;
  195. // b1.inorder();
  196. b1.insert(2);
  197. b1.insert(3);
  198. b1.insert(4);
  199. b1.insert(5);
  200. b1.inorder();
  201. b1.search(10);
  202. b1.search(2);
  203. b1.deleteByVal(2);
  204. b1.inorder();
  205.  
  206. Node * a = b1.findMin(b1.root);
  207. printf("\nMin value is %d ", a->val);
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement