Advertisement
Guest User

trees

a guest
Jun 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Queue with List
  6.  
  7. struct node
  8. {
  9. int data;
  10. struct node *lc;
  11. struct node *rc;
  12. };
  13.  
  14. struct node *lc=NULL;
  15. struct node *rc=NULL;
  16. struct node *root=NULL;
  17.  
  18.  
  19. void insertion(int x)
  20. {
  21. struct node *N=new node;
  22.  
  23. struct node *t=root;
  24. struct node *t1;
  25. N->data=x;
  26. N->lc=NULL;
  27. N->rc=NULL;
  28.  
  29. if(root==NULL){
  30. root=N;
  31. }
  32. else
  33. {
  34.  
  35. while(t!=NULL)
  36. {
  37. if(x>t->data)
  38. {
  39. t1=t;
  40. t=t->rc;
  41. }
  42. else{
  43. t1=t;
  44. t=t->lc;
  45. }
  46.  
  47.  
  48. }
  49. if(t1->data>x){
  50. t1->lc=N;
  51. }
  52. else{
  53. t1->rc=N;
  54. }
  55. }
  56.  
  57. }
  58.  
  59. void search(int x)
  60. {
  61. int flag=0;
  62. struct node *t=root;
  63.  
  64. while(t!=NULL)
  65. {
  66. if(x==t->data)
  67. {
  68. flag=1;
  69. break;
  70. }
  71.  
  72. else if(x>t->data)
  73. {
  74. t=t->rc;
  75. }
  76.  
  77. else
  78. {
  79. t=t->lc;
  80. }
  81.  
  82.  
  83. }
  84.  
  85. if(flag==1)
  86. {
  87. cout<<"The root itself is equal to your value!!"<<endl;
  88. }
  89.  
  90. if(flag==0)
  91. {
  92. cout<<"Your value has been found"<<endl;
  93. }
  94.  
  95. }
  96.  
  97.  
  98. void deletion(int x)
  99. {
  100. int flag=0;
  101. struct node *t=root;
  102. struct node *t1;
  103.  
  104. while(t!=NULL)
  105. {
  106. if(x==t->data)
  107. {
  108. flag=1;
  109. break;
  110. }
  111.  
  112. else if(x>t->data)
  113. {
  114. t1=t;
  115. t=t->rc;
  116. }
  117.  
  118. else
  119. {
  120. t1=t;
  121. t=t->lc;
  122. }
  123. }
  124. if(flag==1)
  125. {
  126. if(t->rc==NULL && t->lc==NULL)
  127. {
  128.  
  129. if(t1->lc==t)
  130. {
  131. t1->lc=NULL;
  132. }
  133. else
  134. {
  135. t1->rc=NULL;
  136. }
  137.  
  138.  
  139. }
  140. else if(t->rc!=NULL && t->lc!=NULL){
  141. cout<<"Pardon me ,we cannot delete"<<endl;
  142. }
  143. else
  144. {
  145.  
  146. if(t1->lc==t)
  147. {
  148. if(t->rc!=NULL)
  149. {
  150. t1->lc=t->rc;
  151. }
  152. else
  153. {
  154. t1->lc=t->lc;
  155.  
  156. }
  157. }
  158. else
  159. {
  160. if(t->rc==NULL)
  161. {
  162.  
  163. t1->rc=t->lc;
  164. }
  165. else
  166. {
  167. t1->rc=t->rc;
  168. }
  169. }
  170.  
  171. }
  172. }
  173. else{
  174. cout<<"No deletion possible "<<endl;
  175. }
  176. }
  177.  
  178.  
  179. void display(struct node *t)
  180. {
  181.  
  182. if(t!=NULL)
  183. {
  184. display(t->lc);
  185. cout<<t->data<<" ";
  186. display(t->rc);
  187. }
  188. }
  189.  
  190. int main()
  191. {
  192. insertion(10);
  193. insertion(20);
  194. insertion(30);
  195. search(10);
  196. display(root);
  197. cout<<endl;
  198. deletion(30);
  199. display(root);
  200.  
  201.  
  202.  
  203.  
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement