Advertisement
Tucancitto

Lab5 - Pb1

May 15th, 2021 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. constexpr auto SPATIU = 5;
  7.  
  8. struct NOD
  9. {
  10.     int info;
  11.     NOD* st, * dr, * p;
  12.  
  13.     NOD() : info(0), st(NULL), dr(NULL), p(NULL) {}
  14.  
  15.     NOD(int info) : info(info), st(NULL), dr(NULL), p(NULL) {}
  16. };
  17.  
  18. void preordine(NOD* x)
  19. {
  20.     if (x == NULL)
  21.         return;
  22.  
  23.     cout << x->info << ' ';
  24.     preordine(x->st);
  25.     preordine(x->dr);
  26.  
  27. }
  28. void inordine(NOD* x)
  29. {
  30.     if (x == NULL)
  31.         return;
  32.  
  33.     inordine(x->st);
  34.     cout << x->info << ' ';
  35.     inordine(x->dr);
  36.  
  37. }
  38. void postordine(NOD* x)
  39. {
  40.     if (x == NULL)
  41.         return;
  42.  
  43.     postordine(x->st);
  44.     postordine(x->dr);
  45.     cout << x->info << ' ';
  46.  
  47. }
  48. void BFS(NOD* x)
  49. {
  50.     if (x == NULL)
  51.         return;
  52.  
  53.     queue<NOD*> coada;
  54.     coada.push(x);
  55.  
  56.     while (!coada.empty())
  57.     {
  58.         NOD* cap = coada.front();
  59.         cout << cap->info << "  ";
  60.         coada.pop();
  61.  
  62.         if (cap->st)
  63.             coada.push(cap->st);
  64.         if (cap->dr)
  65.             coada.push(cap->dr);
  66.     }
  67. }
  68. void afisareArbore(NOD* x, int spatiu)
  69. {
  70.     if (x == NULL)
  71.         return;
  72.     spatiu += SPATIU;
  73.  
  74.     afisareArbore(x->dr, spatiu);
  75.     cout << endl;
  76.  
  77.     for (int index = SPATIU; index < spatiu; cout << ' ', ++index);
  78.  
  79.     cout << x->info;
  80.     afisareArbore(x->st, spatiu);
  81. }
  82.  
  83. struct ARBORE_CAUT
  84. {
  85.     NOD* rad;
  86.     ARBORE_CAUT() : rad(NULL) {}
  87.  
  88.     void insert(int val)
  89.     {
  90.         NOD* x = rad;
  91.         NOD* y = NULL;
  92.         NOD* z = new NOD(val);
  93.  
  94.         while (x != NULL)
  95.         {
  96.             y = x;
  97.             if (z->info < x->info)
  98.                 x = x->st;
  99.             else
  100.                 x = x->dr;
  101.         }
  102.  
  103.         z->p = y;
  104.  
  105.         if (y == NULL)
  106.             rad = z;
  107.         else
  108.         {
  109.             if (z->info < y->info)
  110.                 y->st = z;
  111.             else
  112.                 y->dr = z;
  113.         }
  114.     }
  115.  
  116.     NOD* maxim(NOD* x)
  117.     {
  118.         NOD* y = x;
  119.         if (y != NULL)
  120.             while (y->dr != NULL)
  121.                 y = y->dr;
  122.         return y;
  123.     }
  124.     NOD* minim(NOD* x)
  125.     {
  126.         NOD* y = x;
  127.         if (y != NULL)
  128.             while (y->st != NULL)
  129.                 y = y->st;
  130.         return y;
  131.     }
  132.  
  133.     NOD* succesor(NOD* x)
  134.     {
  135.         NOD* y = new NOD();
  136.  
  137.         if (x->dr != NULL)
  138.             y = minim(x->dr);
  139.         else
  140.         {
  141.             y = x->p;
  142.             while (y != NULL && x == y->dr)
  143.             {
  144.                 x = y;
  145.                 y = y->p;
  146.             }
  147.         }
  148.  
  149.         return y;
  150.     }
  151.     NOD* predecesor(NOD* x)
  152.     {
  153.         NOD* y = new NOD();
  154.  
  155.         if (x->st != NULL)
  156.             y = maxim(x->st);
  157.         else
  158.         {
  159.             y = x->p;
  160.             while (y != NULL && x == y->st)
  161.             {
  162.                 x = y;
  163.                 y = y->p;
  164.             }
  165.         }
  166.  
  167.         return y;
  168.     }
  169.  
  170.     NOD* search(int val)
  171.     {
  172.         NOD* x = rad;
  173.  
  174.         while (x != NULL && x->info != val)
  175.             if (val < x->info)
  176.                 x = x->st;
  177.             else
  178.                 x = x->dr;
  179.  
  180.         return x;
  181.     }
  182.  
  183.     void Transplant(NOD* x, NOD* y)
  184.     {
  185.         if (x->p == NULL)
  186.             rad = y;
  187.         else
  188.         {
  189.             if (x == x->p->st)
  190.                 x->p->st = y;
  191.             else
  192.                 x->p->dr = y;
  193.         }
  194.         if (y != NULL)
  195.             y->p = x->p;
  196.     }
  197.     void del(NOD* x)
  198.     {
  199.         if (x->st == NULL)
  200.             Transplant(x, x->dr);
  201.         else
  202.             if (x->dr == NULL)
  203.                 Transplant(x, x->st);
  204.             else
  205.             {
  206.                 NOD* y = succesor(x);
  207.                 if (y != x->dr)
  208.                 {
  209.                     Transplant(y, y->dr);
  210.                     y->dr = x->dr;
  211.                     x->dr->p = y;
  212.                 }
  213.                 Transplant(x, y);
  214.                 y->st = x->st;
  215.                 x->st->p = y;
  216.             }
  217.     }
  218.  
  219.     void print_tree(int opt)
  220.     {
  221.         switch (opt)
  222.         {
  223.         case 1:
  224.             cout << "   Parcurgerea in preordine: ";
  225.             preordine(rad);
  226.             break;
  227.  
  228.         case 2:
  229.             cout << "   Parcurgerea in inordine: ";
  230.             inordine(rad);
  231.             break;
  232.  
  233.         case 3:
  234.             cout << "   Parcurgerea in postordine: ";
  235.             postordine(rad);
  236.             break;
  237.  
  238.         case 4:
  239.             cout << "   Parcurgerea in latime (pe niveluri): ";
  240.             BFS(rad);
  241.             break;
  242.  
  243.         case 5:
  244.             cout << "\n Toate parcurgerile: ";
  245.             cout << "\n Parcurgerea in preordine: "; preordine(rad);
  246.             cout << "\n Parcurgerea in inordine: "; inordine(rad);
  247.             cout << "\n Parcurgerea in postordine: "; postordine(rad);
  248.             cout << "\n Parcurgerea in latime: "; BFS(rad);
  249.             break;
  250.         }
  251.  
  252.         cout << endl;
  253.         afisareArbore(rad, 8);
  254.         cout << endl;
  255.     }
  256.  
  257.     void construct(ARBORE_CAUT& T, vector<int> vect)
  258.     {
  259.         for (auto val : vect)
  260.             T.insert(val);
  261.     }
  262.  
  263.     bool empty()
  264.     {
  265.         return (rad == NULL);
  266.     }
  267.     void clear()
  268.     {
  269.         if (empty())
  270.             return;
  271.  
  272.         queue<NOD*> coada;
  273.         coada.push(rad);
  274.  
  275.         NOD* cap = NULL;
  276.  
  277.         while (!coada.empty())
  278.         {
  279.             cap = coada.front();
  280.             coada.pop();
  281.  
  282.             if (cap->st)
  283.                 coada.push(cap->st);
  284.             if (cap->dr)
  285.                 coada.push(cap->dr);
  286.  
  287.             delete cap;
  288.         }
  289.  
  290.         rad = NULL;
  291.     }
  292. };
  293.  
  294. void main()
  295. {
  296.     ARBORE_CAUT T;
  297.     int opt = 0, val;
  298.  
  299.     do
  300.     {
  301.         system("CLS");
  302.         cout << "-----Comenzi-----\n";
  303.         cout << "1 pentru insertia unui nod\n";
  304.         cout << "2 pentru cautarea unui nod\n";
  305.         cout << "3 stergerea unui nod\n";
  306.         cout << "4 pentru determinarea minimului\n";
  307.         cout << "5 pentru determinarea maximului\n";
  308.         cout << "6 pentru determinarea succesorului unui nod\n";
  309.         cout << "7 pentru determinarea predecesorului unui nod\n";
  310.         cout << "8 pentru afisarea arborelui\n";
  311.         cout << "9 pentru stergerea nodurilor din arbore\n";
  312.         cout << "10 pentru crearea unui arbore pornind de la un vector de intregi\n";
  313.         cout << "0 pentru iesirea din meniu\n\n";
  314.         cout << "Introduceti comanda: ";
  315.         cin >> opt;
  316.  
  317.         switch (opt)
  318.         {
  319.         case 1:
  320.         {
  321.             cout << "   Introduceti valoarea: ";
  322.             cin >> val;
  323.             T.insert(val);
  324.             cout << "   Valoarea a fost introdusa in arbore. \n";
  325.             break;
  326.         }
  327.  
  328.         case 2:
  329.         {
  330.             cout << "   Introduceti valoarea: ";
  331.             cin >> val;
  332.             if (T.search(val))
  333.                 cout << "   Valoarea se afla in arbore. ";
  334.             else
  335.                 cout << "   Valoarea nu se afla in arbore. ";
  336.             cout << endl;
  337.             break;
  338.         }
  339.  
  340.         case 3:
  341.         {
  342.             cout << "   Introduceti valoarea: ";
  343.             cin >> val;
  344.             NOD* gasit = T.search(val);
  345.             if (gasit != NULL)
  346.             {
  347.                 T.del(gasit);
  348.                 cout << "   Am sters nodul cu cheia " << val;
  349.             }
  350.             else
  351.                 cout << "   Nodul cu cheia " << val << " nu a fost gasit. ";
  352.             cout << endl;
  353.             break;
  354.         }
  355.  
  356.         case 4:
  357.         {
  358.             NOD* minim = T.minim(T.rad);
  359.             if (minim != NULL)
  360.                 cout << "   Valoarea minima din arbore: " << minim->info << endl;
  361.             else
  362.                 cout << "   Arborele este vid. \n";
  363.             break;
  364.         }
  365.  
  366.         case 5:
  367.         {
  368.             NOD* maxim = T.maxim(T.rad);
  369.             if (maxim != NULL)
  370.                 cout << "   Valoarea maxima din arbore: " << maxim->info << endl;
  371.             else
  372.                 cout << "   Arborele este vid. \n";
  373.             break;
  374.         }
  375.  
  376.         case 6:
  377.         {
  378.             cout << "   Introduceti valoarea: ";
  379.             cin >> val;
  380.             NOD* gasit = T.search(val);
  381.             if (gasit != NULL)
  382.             {
  383.                 if (gasit != T.maxim(T.rad))
  384.                     cout << "   Succesorul nodului cu cheia " << val << " este: " << T.succesor(gasit)->info;
  385.                 else
  386.                     cout << "   Nodul cu cheia " << val << " reprezinta elementul maxim din arbore. ";
  387.             }
  388.             else
  389.                 cout << "   Nodul cu cheia " << val << " nu a fost gasit. ";
  390.             cout << endl;
  391.             break;
  392.         }
  393.  
  394.         case 7:
  395.         {
  396.             cout << "   Introduceti valoarea: ";
  397.             cin >> val;
  398.             NOD* gasit = T.search(val);
  399.             if (gasit != NULL)
  400.             {
  401.                 if (gasit != T.minim(T.rad))
  402.                     cout << "   Predecesorul nodului cu cheia " << val << " este: " << T.predecesor(gasit)->info;
  403.                 else
  404.                     cout << "   Nodul cu cheia " << val << " reprezinta elementul minim din arbore. ";
  405.             }
  406.             else
  407.                 cout << "   Nodul cu cheia " << val << " nu a fost gasit. ";
  408.             cout << endl;
  409.             break;
  410.         }
  411.  
  412.         case 8:
  413.         {
  414.             cout << "   1 pentru afisarea arborelui in preordine\n";
  415.             cout << "   2 pentru afisarea arborelui in inordine\n";
  416.             cout << "   3 pentru afisarea arborelui in postordine\n";
  417.             cout << "   4 pentru afisarea arborelui pe niveluri\n";
  418.             cout << "   5 pentru afisarea tuturor parcurgerilor\n";
  419.             cout << "   Introduceti comanda: ";
  420.             int afis;
  421.             cin >> afis;
  422.  
  423.             cout << endl;
  424.             T.print_tree(afis);
  425.             cout << endl;
  426.             break;
  427.         }
  428.  
  429.         case 9:
  430.         {
  431.             T.clear();
  432.             cout << "   Am golit arborele. \n";
  433.             break;
  434.         }
  435.  
  436.         case 10:
  437.         {
  438.             int dim;
  439.             vector<int> vect;
  440.  
  441.             cout << "   Introduceti dimensiunea vectorului: ";
  442.             cin >> dim;
  443.  
  444.             cout << "   Introduceti elementele vectorului: ";
  445.             for (int index = 0; index < dim; ++index)
  446.             {
  447.                 int x;
  448.                 cin >> x;
  449.                 vect.push_back(x);
  450.             }
  451.  
  452.             ARBORE_CAUT T_vect;
  453.             T_vect.construct(T_vect, vect);
  454.             T_vect.print_tree(5);
  455.             cout << endl;
  456.  
  457.             vect.clear();
  458.             T_vect.clear();
  459.         }
  460.         }
  461.  
  462.         if (opt != 0)
  463.         {
  464.             cout << endl;
  465.             system("PAUSE");
  466.         }
  467.  
  468.     } while (opt != 0);
  469.  
  470.     if (!T.empty())
  471.         T.clear();
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement