Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iomanip>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int i = 0;
  9.  
  10.  
  11. class BinarySearchTree
  12. {
  13. private:
  14. struct tree_node
  15. {
  16. tree_node* left;
  17. tree_node* right;
  18. int data;
  19. };
  20. tree_node* root;
  21.  
  22. public:
  23. BinarySearchTree()
  24. {
  25. root = NULL;
  26. }
  27.  
  28. bool isEmpty() const { return root==NULL; }
  29. void print_inorder();
  30. void inorder(tree_node*);
  31. void print_preorder();
  32. void preorder(tree_node*);
  33. void print_postorder();
  34. void postorder(tree_node*,int);
  35. void insert(int);
  36. void remove(int);
  37. };
  38.  
  39. // Smaller elements go left
  40. // larger elements go right
  41. void BinarySearchTree::insert(int d)
  42. {
  43. tree_node* t = new tree_node;
  44. tree_node* parent;
  45. t->data = d;
  46. t->left = NULL;
  47. t->right = NULL;
  48. parent = NULL;
  49.  
  50. // is this a new tree?
  51. if(isEmpty()) root = t;
  52. else
  53. {
  54. //Note: ALL insertions are as leaf nodes
  55. tree_node* curr;
  56. curr = root;
  57. // Find the Node's parent
  58. while(curr)
  59. {
  60. parent = curr;
  61. if(t->data > curr->data){
  62. curr = curr->right;
  63. }
  64. else {
  65. curr = curr->left;
  66. }
  67. }
  68.  
  69. if(t->data < parent->data)
  70. {
  71. parent->left = t;
  72. }
  73. else
  74. {
  75. parent->right = t;
  76. }
  77. }
  78. }
  79.  
  80. void BinarySearchTree::remove(int d)
  81. {
  82. //Locate the element
  83. bool found = false;
  84. if(isEmpty())
  85. {
  86. cout<<" This Tree is empty! "<<endl;
  87. return;
  88. }
  89.  
  90. tree_node* curr;
  91. tree_node* parent;
  92. curr = root;
  93.  
  94. while(curr != NULL)
  95. {
  96. if(curr->data == d)
  97. {
  98. found = true;
  99. break;
  100. }
  101. else
  102. {
  103. parent = curr;
  104. if(d>curr->data) curr = curr->right;
  105. else curr = curr->left;
  106. }
  107. }
  108. if(!found)
  109. {
  110. cout<<" Data not found! "<<endl;
  111. return;
  112. }
  113.  
  114.  
  115. // 3 cases :
  116. // 1. We're removing a leaf node
  117. // 2. We're removing a node with a single child
  118. // 3. we're removing a node with 2 children
  119.  
  120. // Node with single child
  121. if((curr->left == NULL && curr->right != NULL) || (curr->left != NULL && curr->right == NULL))
  122. {
  123. if(curr->left == NULL && curr->right != NULL)
  124. {
  125. if(parent->left == curr)
  126. {
  127. parent->left = curr->right;
  128. delete curr;
  129. }
  130. else
  131. {
  132. parent->right = curr->left;
  133. delete curr;
  134. }
  135. }
  136. return;
  137. }
  138.  
  139. //We're looking at a leaf node
  140. if( curr->left == NULL && curr->right == NULL)
  141. {
  142. if(parent->left == curr)
  143. {
  144. parent->left = NULL;
  145. }
  146. else
  147. {
  148. parent->right = NULL;
  149. }
  150. delete curr;
  151. return;
  152. }
  153.  
  154.  
  155. //Node with 2 children
  156. // replace node with smallest value in right subtree
  157. if (curr->left != NULL && curr->right != NULL)
  158. {
  159. tree_node* chkr;
  160. chkr = curr->right;
  161. if((chkr->left == NULL) && (chkr->right == NULL))
  162. {
  163. curr = chkr;
  164. delete chkr;
  165. curr->right = NULL;
  166. }
  167. else // right child has children
  168. {
  169. //if the node's right child has a left child
  170. // Move all the way down left to locate smallest element
  171.  
  172. if((curr->right)->left != NULL)
  173. {
  174. tree_node* lcurr;
  175. tree_node* lcurrp;
  176. lcurrp = curr->right;
  177. lcurr = (curr->right)->left;
  178. while(lcurr->left != NULL)
  179. {
  180. lcurrp = lcurr;
  181. lcurr = lcurr->left;
  182. }
  183. curr->data = lcurr->data;
  184. delete lcurr;
  185. lcurrp->left = NULL;
  186. }
  187. else
  188. {
  189. tree_node* tmp;
  190. tmp = curr->right;
  191. curr->data = tmp->data;
  192. curr->right = tmp->right;
  193. delete tmp;
  194. }
  195.  
  196. }
  197. return;
  198. }
  199.  
  200. }
  201. void BinarySearchTree::print_postorder()
  202. {
  203. postorder(root,0);
  204. }
  205.  
  206. void BinarySearchTree::postorder(tree_node* p, int indent=0)
  207. {
  208. if(p != NULL) {
  209. if(p->right) postorder(p->right, indent+4);
  210. if(p==root) {
  211. cout<< p->data << "\n ";
  212. if(p->left) postorder(p->left, indent+4);
  213. }else{
  214. if(p->left) postorder(p->left, indent+4);
  215.  
  216. if (indent) {
  217. std::cout << std::setw(indent) << ' ';
  218. }
  219. cout<< p->data << "\n ";
  220. }
  221. }
  222. }
  223.  
  224. int main()
  225. {
  226. BinarySearchTree b;
  227. int ch,tmp,tmp1;
  228. while(1)
  229. {
  230. cout<<endl<<endl;
  231. cout<<" Binary Search Tree Operations "<<endl;
  232. cout<<" ----------------------------- "<<endl;
  233. cout<<" 1. Insertion/Creation "<<endl;
  234. cout<<" 2. Printing "<<endl;
  235. cout<<" 3. Removal "<<endl;
  236. cout<<" 4. Exit "<<endl;
  237. cout<<" Enter your choice : ";
  238. cin>>ch;
  239. switch(ch)
  240. {
  241. case 1 : cout<<" Enter Number to be inserted : ";
  242. cin>>tmp;
  243. b.insert(tmp);
  244. i++;
  245. break;
  246. case 2 : cout<<endl;
  247. cout<<" Printing "<<endl;
  248. cout<<" --------------------"<<endl;
  249. b.print_postorder();
  250. break;
  251. case 3 : cout<<" Enter data to be deleted : ";
  252. cin>>tmp1;
  253. b.remove(tmp1);
  254. break;
  255. case 4:
  256. return 0;
  257.  
  258. }
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement