fork download
  1. #include<iostream>
  2. using namespace std;
  3. class Node{
  4. public:
  5. int val;
  6. Node *parent;
  7. Node *left;
  8. Node *right;
  9. };
  10.  
  11. class binarysearchtree
  12. {
  13. Node *root;
  14.  
  15. public:
  16.  
  17. binarysearchtree(){
  18. root =NULL;
  19. }
  20.  
  21. void insertdata(int val)
  22. {
  23. Node *newnode=new Node;
  24. newnode->val=val;
  25. newnode->parent=NULL;
  26. newnode->left=NULL;
  27. newnode->right=NULL;
  28.  
  29. if(root==NULL)
  30. {
  31. root=newnode;
  32. }
  33. else{
  34. Node *temp=root;
  35. Node *prev=NULL;
  36. while(temp!=NULL)
  37. {
  38. prev=temp;
  39. if(val < temp->val)
  40. {
  41. temp=temp->left;
  42. }
  43. else{
  44. temp=temp->right;
  45. }
  46. }
  47. temp=newnode;
  48. newnode->parent=prev;
  49. if(val < prev->val)
  50. {
  51. prev->left=newnode;
  52. }
  53. else{
  54. prev->right=newnode;
  55. }
  56. // cout<<prev->val<<" "<<newnode->val<<endl;
  57. }
  58. }
  59.  
  60. Node* searchdata(int val)
  61. {
  62. Node *temp=root;
  63. while(temp!=NULL)
  64. {
  65. if(temp->val==val) return temp;
  66. if(val<temp->val)
  67. {
  68. temp=temp->left;
  69. }
  70. else
  71. {
  72. temp=temp->right;
  73. }
  74. }
  75. return temp;
  76. }
  77.  
  78. Node* maximumnode(Node* cur)
  79. {
  80. if(cur==NULL)
  81. {
  82. cout << "Invalid!" << endl;
  83. return cur;
  84. }
  85. Node *temp=cur;
  86. while(temp->right!=NULL)
  87. {
  88. temp=temp->right;
  89. }
  90. cout<<"Maximum node: "<<temp->val<<endl;
  91. return temp;
  92. }
  93.  
  94. Node* minimumnode(Node* cur)
  95. {
  96. if(cur==NULL)
  97. {
  98. cout << "Invalid!" << endl;
  99. return cur;
  100. }
  101. Node *temp=cur;
  102. while(temp->left!=NULL)
  103. {
  104. temp=temp->left;
  105. }
  106. cout<<"Minimum node: "<<temp->val<<endl;
  107. return temp;
  108. }
  109.  
  110. void inorder(Node *temp)
  111. {
  112. if(temp==NULL)
  113. {
  114. return;
  115. }
  116. inorder(temp->left);
  117. cout<<temp->val<<" ";
  118. inorder(temp->right);
  119. }
  120.  
  121. Node* getroot(){
  122. return root;
  123. }
  124.  
  125. void deletenode(int val)
  126. {
  127. Node* found=searchdata(val);
  128. if(found==NULL)
  129. {
  130. cout<<"Not found"<<endl;
  131. }
  132. else if(found->left==NULL && found->right==NULL)
  133. {
  134. deletecase1(found);
  135. }
  136. else if(found->left==NULL || found->right==NULL)
  137. {
  138. deletecase2(found);
  139. }
  140. else{
  141. deletecase3(found);
  142. }
  143. }
  144.  
  145. void deletecase1(Node *temp)
  146. {
  147. cout<<"Case 1:"<<endl;
  148. Node *par=temp->parent;
  149. if(temp->val < par->val)
  150. {
  151. par->left=NULL;
  152. }
  153. else{
  154. par->right=NULL;
  155. }
  156. delete temp;
  157. }
  158.  
  159. void deletecase2(Node *temp)
  160. {
  161. cout<<"Case 2:"<<endl;
  162. Node *par=temp->parent;
  163. Node *child=temp->left;
  164. if(child==NULL)
  165. {
  166. child=temp->right;
  167. }
  168. if(temp->val < par->val)
  169. {
  170. par->left=child;
  171. }
  172. else{
  173. par->right=child;
  174. }
  175. child->parent=par;
  176. delete temp;
  177. }
  178.  
  179. void deletecase3(Node *temp)
  180. {
  181. Node *suc=findsuccessor(temp);
  182. int temp1 = suc->val;
  183. deletenode(suc->val);
  184. temp->val = temp1;
  185. }
  186.  
  187. Node* findsuccessor(Node *temp)
  188. {
  189. if(temp==NULL)
  190. {
  191. cout<<"Not found"<<endl;
  192. return NULL;
  193. }
  194. else if(temp->right!=NULL)
  195. {
  196. Node *cur=temp->right;
  197. cur=minimumnode(cur);
  198. cout<<"Successor: "<<cur->val<<endl;
  199. return cur;
  200. }
  201. else if(temp->parent!=NULL){
  202. Node *cur=temp->parent;
  203. while(cur->val < temp->val)
  204. {
  205. temp=cur;
  206. cur=cur->parent;
  207. if(cur==NULL){
  208. cout<<"Not found"<<endl;
  209. return NULL;
  210. }
  211. }
  212. cout<<"Successor: "<<cur->val<< endl;
  213. return cur;
  214. }
  215. else{
  216. cout<<"Not found"<<endl;
  217. return NULL;
  218. }
  219. }
  220.  
  221. Node* findpredeccessor(Node *temp)
  222. {
  223. if(temp==NULL)
  224. {
  225. cout<<"Not found in the list"<<endl;
  226. return NULL;
  227. }
  228. else if(temp->left!=NULL)
  229. {
  230. Node *cur=temp->left;
  231. cur=maximumnode(cur);
  232. cout<<"Predecessor: "<<cur->val<<endl;
  233. return cur;
  234. }
  235. else if(temp->parent!=NULL){
  236. Node *cur=temp->parent;
  237. while(cur->val > temp->val)
  238. {
  239. temp=cur;
  240. cur=cur->parent;
  241. if(cur==NULL){
  242. cout<<"Not found"<<endl;
  243. return NULL;
  244. }
  245. }
  246. cout<<"Predecessor: "<<cur->val<< endl;
  247. return cur;
  248. }
  249. else{
  250. cout<<"Not found"<<endl;
  251. return NULL;
  252. }
  253. }
  254. };
  255.  
  256. int main()
  257. {
  258. //freopen("input.txt","r",stdin);
  259. int n;
  260. cin>>n;
  261. binarysearchtree bst;
  262. for(int i=0;i<n;i++)
  263. {
  264. int a;
  265. cin>>a;
  266. bst.insertdata(a);
  267. }
  268. Node *temp=bst.getroot();
  269. bst.inorder(temp);
  270. cout << endl;
  271. int i;
  272. cin>>i;
  273. Node* found=bst.searchdata(i);
  274. if(found==NULL)
  275. {
  276. cout<<"Not found"<<endl;
  277. }
  278. else{
  279. cout<<"found"<<endl;
  280. }
  281. bst.minimumnode(bst.getroot());
  282. bst.maximumnode(bst.getroot());
  283. Node *temp1=bst.getroot();
  284. bst.inorder(temp1);
  285. cout << endl;
  286. int del;
  287. cout<<"Want to del:"<<endl;
  288. cin>>del;
  289. bst.deletenode(del);
  290. Node *temp2=bst.getroot();
  291. bst.inorder(temp2);
  292. cout << endl;
  293.  
  294. int p;
  295. cout<<"Find the successor: "<<endl;
  296. cin>>p;
  297.  
  298. Node* found2=bst.searchdata(p);
  299. if(found2==NULL)
  300. {
  301. cout<<"Not found"<<endl;
  302. }
  303. else{
  304. cout<<"found"<<endl;
  305. }
  306.  
  307. cout<<"Find the successor: "<<endl;
  308. bst.findsuccessor(found2);
  309.  
  310.  
  311. int q;
  312. cout<<"Find the predecessor: "<<endl;
  313. cin>>q;
  314.  
  315. Node* found3=bst.searchdata(q);
  316. if(found3==NULL)
  317. {
  318. cout<<"Not found"<<endl;
  319. }
  320. else{
  321. cout<<"found"<<endl;
  322. }
  323.  
  324. cout<<"Find the successor: "<<endl;
  325. bst.findpredeccessor(found3);
  326. //bst.inorder(temp2);
  327.  
  328.  
  329. // bst.deletecase1();
  330. }
  331.  
  332. /*
  333. 12
  334. 44 17 88 32 65 97 28 54 82 29 76 80
  335. 82
  336. 44
  337. 44
  338. 54
  339. */
  340.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Not found
Invalid!
Invalid!

Want to del:
Not found

Find the successor: 
Not found
Find the successor: 
Not found
Find the predecessor: 
Not found
Find the successor: 
Not found in the list