Advertisement
Guest User

trees

a guest
Jun 12th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 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(t1->rc==t){
  127. t1->rc=NULL;
  128. }else{
  129. t1->lc=NULL;
  130. }
  131.  
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138. }
  139.  
  140. void display(struct node *t)
  141. {
  142.  
  143. if(t!=NULL)
  144. {
  145. display(t->lc);
  146. cout<<t->data<<" ";
  147. display(t->rc);
  148. }
  149. }
  150.  
  151. int main()
  152. {
  153. insertion(10);
  154. insertion(20);
  155. insertion(30);
  156. search(10);
  157. display(root);
  158. deletion(30);
  159. display(root);
  160.  
  161.  
  162.  
  163.  
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement