Advertisement
yuku04

ASD-ARBORE

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