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. return;
  117. inorder(temp->left);
  118. cout<<temp->val<<endl;
  119. inorder(temp->right);
  120. }
  121.  
  122. Node* getroot(){
  123. return root;
  124. }
  125.  
  126. void deletenode(int val)
  127. {
  128. Node* found=searchdata(val);
  129. if(found==NULL)
  130. {
  131. cout<<"Not found"<<endl;
  132. }
  133. else if(found->left==NULL && found->right==NULL)
  134. {
  135. deletecase1(found);
  136. }
  137. else if(found->left==NULL || found->right==NULL)
  138. {
  139. deletecase2(found);
  140. }
  141. else{
  142. deletecase3(found);
  143. }
  144. }
  145.  
  146. void deletecase1(Node *temp)
  147. {
  148. cout<<"Case 1:"<<endl;
  149. Node *par=temp->parent;
  150. if(temp->val < par->val)
  151. {
  152. par->left=NULL;
  153. }
  154. else{
  155. par->right=NULL;
  156. }
  157. delete temp;
  158. }
  159.  
  160. void deletecase2(Node *temp)
  161. {
  162. cout<<"Case 2:"<<endl;
  163. Node *par=temp->parent;
  164. Node *child=temp->left;
  165. if(child==NULL)
  166. {
  167. child=temp->right;
  168. }
  169. if(temp->val < par->val)
  170. {
  171. par->left=child;
  172. }
  173. else{
  174. par->right=child;
  175. }
  176. child->parent=par;
  177. delete temp;
  178. }
  179.  
  180. void deletecase3(Node *temp)
  181. {
  182. Node *suc=findsuccessor(temp);
  183. temp->val = suc->val;
  184. deletenode(suc->val);
  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. int i;
  271. cin>>i;
  272. Node* found=bst.searchdata(i);
  273. if(found==NULL)
  274. {
  275. cout<<"Not found"<<endl;
  276. }
  277. else{
  278. cout<<"found"<<endl;
  279. }
  280. bst.minimumnode(bst.getroot());
  281. bst.maximumnode(bst.getroot());
  282. Node *temp1=bst.getroot();
  283. bst.inorder(temp1);
  284. int del;
  285. cout<<"Want to del:"<<endl;
  286. cin>>del;
  287. bst.deletenode(del);
  288. Node *temp2=bst.getroot();
  289. bst.inorder(temp2);
  290.  
  291. int p;
  292. cout<<"Find the successor: "<<endl;
  293. cin>>p;
  294.  
  295. Node* found2=bst.searchdata(p);
  296. if(found2==NULL)
  297. {
  298. cout<<"Not found"<<endl;
  299. }
  300. else{
  301. cout<<"found"<<endl;
  302. }
  303.  
  304. cout<<"Find the successor: "<<endl;
  305. bst.findsuccessor(found2);
  306.  
  307.  
  308. int q;
  309. cout<<"Find the predecessor: "<<endl;
  310. cin>>q;
  311.  
  312. Node* found3=bst.searchdata(q);
  313. if(found3==NULL)
  314. {
  315. cout<<"Not found"<<endl;
  316. }
  317. else{
  318. cout<<"found"<<endl;
  319. }
  320.  
  321. cout<<"Find the successor: "<<endl;
  322. bst.findpredeccessor(found3);
  323. //bst.inorder(temp2);
  324.  
  325.  
  326. // bst.deletecase1();
  327. }
  328.  
  329. /*
  330. 12
  331. 44 17 88 32 65 97 28 54 82 29 76 80
  332. 82
  333. 44
  334. 44
  335. 54
  336. */
  337.  
Success #stdin #stdout 0s 5328KB
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