Guest User

Untitled

a guest
Dec 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. class agac
  6. {
  7. private:
  8. struct node
  9. {
  10. int data;
  11. node* sol;
  12. node* sag;
  13. };
  14.  
  15. node* root;
  16.  
  17. node* ilk(node* t)
  18. {
  19. if(t == NULL)
  20. return NULL;
  21. {
  22. ilk(t->sol);
  23. ilk(t->sag);
  24. delete t;
  25. }
  26. return NULL;
  27. }
  28.  
  29. node* ekle(int x, node* t)
  30. {
  31. if(t == NULL)
  32. {
  33. t = new node;
  34. t->data = x;
  35. t->sol = t->sag = NULL;
  36. }
  37. else if(x < t->data)
  38. t->sol = ekle(x, t->sol);
  39. else if(x > t->data)
  40. t->sag = ekle(x, t->sag);
  41. return t;
  42. }
  43.  
  44. node* araMin(node* t)
  45. {
  46. if(t == NULL)
  47. return NULL;
  48. else if(t->sol == NULL)
  49. return t;
  50. else
  51. return araMin(t->sol);
  52. }
  53.  
  54. node* araMax(node* t)
  55. {
  56. if(t == NULL)
  57. return NULL;
  58. else if(t->sag == NULL)
  59. return t;
  60. else
  61. return araMax(t->sag);
  62. }
  63.  
  64. node* sil(int x, node* t)
  65. {
  66. node* temp;
  67. if(t == NULL)
  68. return NULL;
  69. else if(x < t->data)
  70. t->sol = sil(x, t->sol);
  71. else if(x > t->data)
  72. t->sag = sil(x, t->sag);
  73. else if(t->sol && t->sag)
  74. {
  75. temp = araMin(t->sag);
  76. t->data = temp->data;
  77. t->sag = sil(t->data, t->sag);
  78. }
  79. else
  80. {
  81. temp = t;
  82. if(t->sol == NULL)
  83. t = t->sag;
  84. else if(t->sag == NULL)
  85. t = t->sol;
  86. delete temp;
  87. }
  88.  
  89. return t;
  90. }
  91.  
  92. void inorder(node* t)
  93. {
  94. if (t == NULL) return;
  95.  
  96. // Create an empty queue for level order tarversal
  97. queue<node *> q;
  98.  
  99. // Enqueue Root and initialize height
  100. q.push(t);
  101.  
  102. while (q.empty() == false)
  103. {
  104. // nodeCount (queue size) indicates number
  105. // of nodes at current lelvel.
  106. int nodeCount = q.size();
  107.  
  108. // Dequeue all nodes of current level and
  109. // Enqueue all nodes of next level
  110. while (nodeCount > 0)
  111. {
  112. node *node = q.front();
  113. cout << node->data << " ";
  114. q.pop();
  115. if (node->sol != NULL)
  116. q.push(node->sol);
  117. if (node->sag != NULL)
  118. q.push(node->sag);
  119. nodeCount--;
  120. }
  121. cout << endl;
  122. }
  123. }
  124.  
  125. node* ara(node* t, int x)
  126. {
  127. if(t == NULL)
  128. return NULL;
  129. else if(x < t->data)
  130. return ara(t->sol, x);
  131. else if(x > t->data)
  132. return ara(t->sag, x);
  133. else
  134. return t;
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. public:
  143. agac()
  144. {
  145. root = NULL;
  146. }
  147.  
  148. ~agac()
  149. {
  150. root = ilk(root);
  151. }
  152.  
  153. void ekle(int x)
  154. {
  155. root = ekle(x, root);
  156. }
  157.  
  158. void sil(int x)
  159. {
  160. root = sil(x, root);
  161. }
  162.  
  163. void yazdir()
  164. {
  165. inorder(root);
  166. cout << endl;
  167. }
  168.  
  169. void arama(int x)
  170. {
  171. root = ara(root, x);
  172. }
  173.  
  174. };
  175.  
  176. int main()
  177. {
  178. agac* t=new agac();;
  179. t->ekle(20);
  180. t->ekle(25);
  181. t->ekle(15);
  182. t->ekle(10);
  183. t->ekle(30);
  184. t->yazdir();
  185. t->sil(20);
  186. t->yazdir();
  187. t->sil(25);
  188. t->yazdir();
  189. t->sil(30);
  190. t->yazdir();
  191. cout<<"\n--------------------\n";
  192. }
Add Comment
Please, Sign In to add comment