Advertisement
Tucancitto

Lab3 - Pb6

Apr 10th, 2021 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. constexpr auto SPATIU = 5;
  9.  
  10. struct VECTOR
  11. {
  12.     vector<int> sir;
  13.     void citire(string fisier)
  14.     {
  15.         ifstream fin(fisier);
  16.         if (fin.peek() == EOF) {
  17.             cout << "Fisierul este vid. \n";
  18.             return;
  19.         }
  20.  
  21.         int val;
  22.         while (fin >> val)
  23.             sir.push_back(val);
  24.     }
  25.  
  26.     void afisare()
  27.     {
  28.         for (auto val : sir)
  29.             cout << val << ' ';
  30.         cout << endl;
  31.     }
  32.  
  33.     void eliberareMemorie()
  34.     {
  35.         sir.clear();
  36.         sir.shrink_to_fit();
  37.     }
  38. };
  39.  
  40. struct ARBORE
  41. {
  42.     struct NOD
  43.     {
  44.         int info = INT_MIN;
  45.         NOD* st = NULL, * dr = NULL;
  46.     };
  47.  
  48.     NOD* radacina = new NOD;
  49.  
  50.     bool empty()
  51.     {
  52.         if (radacina == NULL)
  53.             return true;
  54.         return false;
  55.     }
  56.  
  57.     NOD* formareArbore(VECTOR preOrdine, VECTOR inOrdine, int& indexPreordine, int start, int end)
  58.     {
  59.         if (start > end)
  60.             return NULL;
  61.  
  62.         NOD* nod = new NOD;
  63.         nod->info = preOrdine.sir[indexPreordine++];
  64.  
  65.         int indexInordine = pozInInordine(nod->info, inOrdine, start, end);
  66.  
  67.         nod->st = formareArbore(preOrdine, inOrdine, indexPreordine, start, indexInordine - 1);
  68.         nod->dr = formareArbore(preOrdine, inOrdine, indexPreordine, indexInordine + 1, end);
  69.  
  70.         return nod;
  71.     }
  72.  
  73.     int pozInInordine(int val, VECTOR vect, int start, int end)
  74.     {
  75.         for (int index = start; index <= end; ++index)
  76.             if (vect.sir[index] == val)
  77.                 return index;
  78.         return -1;
  79.     }
  80.  
  81.     int inaltime(NOD* rad)
  82.     {
  83.         if (rad == NULL)
  84.             return -1;
  85.  
  86.         int inaltimeSt = inaltime(rad->st);
  87.         int inaltimeDr = inaltime(rad->dr);
  88.  
  89.         return max(inaltimeSt, inaltimeDr) + 1;
  90.     }
  91.  
  92.     void afisareNivel(NOD* rad, int nivel)
  93.     {
  94.         if (rad == NULL)
  95.             return;
  96.  
  97.         if (nivel == 0)
  98.             cout << rad->info << ' ';
  99.         else
  100.         {
  101.             afisareNivel(rad->st, nivel - 1);
  102.             afisareNivel(rad->dr, nivel - 1);
  103.         }
  104.     }
  105.  
  106.     void BFS()
  107.     {
  108.         int h = inaltime(radacina);
  109.         for (int nivel = 0; nivel <= h; ++nivel)
  110.             afisareNivel(radacina, nivel);
  111.         cout << endl;
  112.     }
  113.  
  114.     void afisareArbore(NOD* rad, int spatiu)
  115.     {
  116.         if (rad == NULL)
  117.             return;
  118.         spatiu += SPATIU;
  119.  
  120.         afisareArbore(rad->dr, spatiu);
  121.         cout << endl;
  122.  
  123.         for (int index = SPATIU; index < spatiu; cout << ' ', ++index);
  124.  
  125.         cout << rad->info;
  126.         afisareArbore(rad->st, spatiu);
  127.     }
  128.  
  129.     void stergereArbore(NOD*& rad)
  130.     {
  131.         if (rad == NULL)
  132.             return;
  133.  
  134.         stergereArbore(rad->st);
  135.         stergereArbore(rad->dr);
  136.  
  137.         delete rad;
  138.         rad = nullptr;
  139.     }
  140. };
  141.  
  142. int main()
  143. {
  144.     VECTOR preOrdine, inOrdine;
  145.     preOrdine.citire("preordine.in");
  146.     inOrdine.citire("inordine.in");
  147.  
  148.     cout << "Parcurgerea in preordine: ";
  149.     preOrdine.afisare();
  150.     cout << "Parcurgerea in inordine: ";
  151.     inOrdine.afisare();
  152.  
  153.     ARBORE arbore;
  154.  
  155.     int indexPreordine = 0;
  156.     arbore.radacina = arbore.formareArbore(preOrdine, inOrdine, indexPreordine, 0, preOrdine.sir.size() - 1);
  157.     arbore.afisareArbore(arbore.radacina, 0);
  158.  
  159.     cout << "\n\nParcurgerea in latime: ";
  160.     arbore.BFS();
  161.  
  162.     preOrdine.eliberareMemorie();
  163.     inOrdine.eliberareMemorie();
  164.     arbore.stergereArbore(arbore.radacina);
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement