Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4.  
  5. class BinarySearchTree
  6. {
  7. private:
  8. struct treeNode
  9. {
  10. treeNode* left;
  11. treeNode* right;
  12. int data;
  13. };
  14. treeNode* root;
  15.  
  16. public:
  17. BinarySearchTree()
  18. {
  19. root=NULL;
  20. }
  21.  
  22. bool isEmpty()
  23. const{ return root==NULL;}
  24.  
  25.  
  26. void printinorder();
  27. void inorder(treeNode*);
  28. void printpreorder();
  29. void preorder(treeNode*);
  30. void printpostorder();
  31. void postorder(treeNode*);
  32. void insert(int);
  33. void remove(int);
  34. };
  35.  
  36.  
  37. void BinarySearchTree::insert(int d)
  38. {
  39. treeNode* t= new treeNode;
  40. treeNode*parent;
  41. t->data=d;
  42. t->left=NULL;
  43. t->right=NULL;
  44. parent=NULL;
  45.  
  46. if(isEmpty())
  47. root=t;
  48. else
  49. {
  50. treeNode* curr;
  51. curr=root;
  52. while(curr)
  53. {
  54. parent=curr;
  55. if(t->data > curr->data)
  56. curr=curr->right;
  57. else
  58. curr=curr->left;
  59. }
  60.  
  61. if(t->data<parent->data)
  62. parent->left=t;
  63. else
  64. parent->right=t;
  65. }
  66. }
  67.  
  68. void BinarySearchTree::remove(int d)
  69. {
  70. bool found=false;
  71. if(isEmpty())
  72. {
  73. cout << "Tree is empty!\n";
  74. return;
  75. }
  76.  
  77. treeNode* curr;
  78. treeNode* parent;
  79. curr=root;
  80.  
  81. while(curr!=NULL)
  82. {
  83. if(curr->data==d)
  84. {
  85. found=true;
  86. break;
  87. }
  88. else
  89. {
  90. parent=curr;
  91. if(d>curr->data)
  92. curr=curr->right;
  93. else
  94. curr=curr->left;
  95. }
  96. }
  97. if(!found)
  98. {
  99. cout << "Data not found!";
  100. return;
  101. }
  102.  
  103. if((curr->left==NULL&&curr->right!=NULL)||(curr->left!=NULL && curr->right==NULL))
  104. {
  105. if(curr->left==NULL&&curr->right!=NULL)
  106. {
  107. if(parent->left==curr)
  108. {
  109. parent->left=curr->right;
  110. delete curr;
  111. }
  112. else
  113. {
  114. parent->right=curr->right;
  115. delete curr;
  116. }
  117. }
  118. else
  119. {
  120. if(parent->left==curr)
  121. {
  122. parent->left==curr->left;
  123. delete curr;
  124. }
  125. else
  126. {
  127. parent->right=curr->left;
  128. delete curr;
  129. }
  130. }
  131. return;
  132. }
  133.  
  134. if(curr->left==NULL&&curr->right==NULL)
  135. {
  136. if(parent->left==curr)
  137. parent->left=NULL;
  138. else
  139. parent->right=NULL;
  140. delete curr;
  141. return;
  142. }
  143.  
  144. if(curr->left!=NULL&&curr->right!=NULL)
  145. {
  146. treeNode* chkr;
  147. chkr=curr->right;
  148. if((chkr->left==NULL)&&(chkr->right==NULL))
  149. {
  150. curr=chkr;
  151. delete chkr;
  152. curr->right=NULL;
  153. }
  154. else
  155. {
  156. if((curr->right)->left!=NULL)
  157. {
  158. treeNode* lcurr;
  159. treeNode* lcurrp;
  160. lcurrp=curr->right;
  161. lcurr=(curr->right)->left;
  162. while(lcurr->left!=NULL)
  163. {
  164. lcurrp=lcurr;
  165. lcurr=lcurr->left;
  166. }
  167. curr->data=lcurr->data;
  168. delete lcurr;
  169. lcurrp->left=NULL;
  170. }
  171. else
  172. {
  173. treeNode* tmp;
  174. tmp=curr->right;
  175. curr->data=tmp->data;
  176. curr->right=tmp->right;
  177. delete tmp;
  178. }
  179. }
  180. return;
  181. }
  182. }
  183.  
  184. void BinarySearchTree::printinorder()
  185. {
  186. inorder(root);
  187. }
  188.  
  189. void BinarySearchTree::inorder(treeNode* p)
  190. {
  191. if(p!=NULL)
  192. {
  193. if(p->left) inorder(p->left);
  194. cout << " " << p->data<<" ";
  195. if(p->right) inorder(p->right);
  196. }
  197. else
  198. return;
  199. }
  200.  
  201. void BinarySearchTree::printpreorder()
  202. {
  203. preorder(root);
  204. }
  205.  
  206.  
  207. void BinarySearchTree::preorder(treeNode* p)
  208. {
  209. if(p!=NULL)
  210. {
  211. cout << " " << p->data<<" ";
  212. if(p->left) preorder(p->left);
  213. if(p->right) preorder(p->right);
  214. }
  215. else
  216. return;
  217. }
  218.  
  219. void BinarySearchTree::printpostorder()
  220. {
  221. postorder(root);
  222. }
  223.  
  224. void BinarySearchTree::postorder(treeNode* p)
  225. {
  226. if(p!=NULL)
  227. {
  228. if(p->left) postorder(p->left);
  229. if(p->right) postorder(p->right);
  230. cout << " "<<p->data<<" ";
  231. }
  232. else
  233. return;
  234. }
  235.  
  236. int main()
  237. {
  238. BinarySearchTree b;
  239. int ch, tmp, tmp1;
  240. while(1)
  241. {
  242. cout <<"\n\n";
  243. cin >> ch;
  244. switch(ch)
  245. {
  246. case 1: cout<< "insert: ";
  247. cin >> tmp;
  248. b.insert(tmp);
  249. break;
  250. case 2: cout << endl;
  251. cout << " In-order traversal " << endl;
  252. b.printinorder();
  253. break;
  254. case 3: cout << endl;
  255. cout << " pre-order " << endl;
  256. b.printpreorder();
  257. break;
  258. case 4: cout << endl;
  259. cout << " post-order " << endl;
  260. b.printpostorder();
  261. break;
  262. case 5: cout << "enter data to delete: ";
  263. cin >> tmp1;
  264. b.remove(tmp1);
  265. break;
  266. case 6:
  267. return 0;
  268. }
  269. }
  270. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement