Advertisement
icatalin

ASD Arbore incomplet

Jan 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8. Node *st, *dr;
  9. int info;
  10. };
  11.  
  12. class Arbore
  13. {
  14. public:
  15. Arbore(){root = NULL;}
  16.  
  17. Node *root;
  18. void add(int x);
  19. Node *deletee(Node* current, int x);
  20. Node *findMaxim(Node* current);
  21.  
  22.  
  23. void RSD(Node* current);
  24. void SRD(Node* current);
  25. void SDR(Node* current);
  26. };
  27.  
  28. void Arbore::add(int x)
  29. {
  30. Node *p = new Node;
  31. p->st = NULL;
  32. p->dr = NULL;
  33. p->info = x;
  34.  
  35.  
  36. if (root == NULL)
  37. {
  38. root = p;
  39. return;
  40. }
  41.  
  42. Node *temp = root;
  43.  
  44. while(true)
  45. {
  46. if (x > temp->info && temp->dr == NULL)
  47. {
  48. temp->dr = p;
  49. break;
  50. }
  51.  
  52. if (x < temp->info && temp->st == NULL)
  53. {
  54. temp->st = p;
  55. break;
  56. }
  57.  
  58. if (x > temp->info)
  59. temp = temp->dr;
  60. else if (x < temp->info)
  61. temp = temp->st;
  62. }
  63.  
  64.  
  65. }
  66.  
  67. Node* Arbore::findMaxim(Node* root)
  68. {
  69. Node *temp = root;
  70. if (temp == NULL)
  71. return NULL;
  72.  
  73. while (temp->dr != NULL)
  74. temp = temp->dr;
  75.  
  76. return temp;
  77. }
  78.  
  79. Node* Arbore::deletee(Node* current, int x)
  80. {
  81. if (current == NULL)
  82. return NULL;
  83. else if (x < current->info)
  84. current->st = deletee(current->st, x); // cautam in suboarborele stang
  85. else if (x > current->info)
  86. current->dr = deletee(current->dr, x); // cautam in suboarborele drept
  87. else // daca valorile sunt egale, am ajuns la elementul cautat
  88. {
  89. //nod frunza
  90. if (current->dr == NULL && current->st == NULL)
  91. {
  92. delete current;
  93. current = NULL;
  94. }
  95. else if (current->dr == NULL) //cu un singur copil
  96. {
  97. Node* temp = current;
  98. current = current->st;
  99. delete temp;
  100. }
  101. else if (current->st == NULL)
  102. {
  103. Node* temp = current;
  104. current = current->dr;
  105. delete temp;
  106. }
  107. else // cu doi copii
  108. {
  109. Node* temp = findMaxim(current->st);
  110. current->info = temp->info;
  111. deletee(current->st, temp->info);
  112. }
  113.  
  114. }
  115. return current;
  116. }
  117.  
  118. void Arbore::RSD(Node* current)
  119. {
  120. if (current == NULL) return;
  121. cout<<current->info<<" ";
  122. RSD(current->st);
  123. RSD(current->dr);
  124. }
  125.  
  126. void Arbore::SRD(Node* current)
  127. {
  128. if (current == NULL) return;
  129. SRD(current->st);
  130. cout<<current->info<<" ";
  131. SRD(current->dr);
  132. }
  133.  
  134. void Arbore::SDR(Node* current)
  135. {
  136. if (current == NULL) return;
  137. SDR(current->st);
  138. SDR(current->dr);
  139. cout<<current->info<<" ";
  140. }
  141.  
  142. int main()
  143. {
  144. Arbore a;
  145. a.add(5);
  146. a.add(3);
  147. a.add(7);
  148. a.deletee(a.root,3);
  149. a.SDR(a.root);
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement