Advertisement
catalin_stefann11

AVL + BST

Jul 1st, 2019
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.46 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include<iostream>
  4. #include<malloc.h>
  5. using namespace std;
  6.  
  7. struct Vacanta {
  8.     char* destinatie;
  9.     float pret;
  10. };
  11.  
  12. struct nod {
  13.     Vacanta info;
  14.     nod* st;
  15.     nod*dr;
  16. };
  17.  
  18. struct nodLista {
  19.     Vacanta info;
  20.     nodLista* next;
  21. };
  22.  
  23. Vacanta creareVacanta(const char* dest, const float pret) {
  24.     Vacanta v;
  25.     v.destinatie = (char*)malloc(sizeof(char)*(strlen(dest) + 1));
  26.     strcpy_s(v.destinatie, strlen(dest) + 1, dest);
  27.     v.pret = pret;
  28.  
  29.     return v;
  30. }
  31. nodLista* inserareInceput(nodLista* cap, Vacanta v) {
  32.     nodLista*nou = (nodLista*)malloc(sizeof(nodLista));
  33.     nou->info = creareVacanta(v.destinatie, v.pret);
  34.     nou->next = cap;
  35.     return nou;
  36. }
  37.  
  38. void copiereInLista(nod*root, nodLista** cap, char litera) {
  39.     if (root) {
  40.         copiereInLista(root->st, cap, litera);
  41.         if (root->info.destinatie[0] == litera) {
  42.             *cap = inserareInceput(*cap, root->info);
  43.         }
  44.         copiereInLista(root->dr, cap, litera);
  45.     }
  46. }
  47.  
  48.  
  49. void afisareVacanta(Vacanta v) {
  50.     printf("Vacanta din %s costa %5.2f lei\n", v.destinatie, v.pret);
  51. }
  52.  
  53. nod* inserareInArbore(nod* root, Vacanta v) {
  54.     if (root) {
  55.         if (v.pret < root->info.pret) {
  56.             root->st = inserareInArbore(root->st, v);
  57.         }
  58.         else if (v.pret>root->info.pret) {
  59.             root->dr = inserareInArbore(root->dr, v);
  60.         }
  61.         return root;
  62.     }
  63.     else {
  64.         nod* nou = (nod*)malloc(sizeof(nod));
  65.         nou->info = v;
  66.         nou->dr = NULL;
  67.         nou->st = NULL;
  68.         return nou;
  69.     }
  70. }
  71.  
  72. void afisareSRD(nod* root) {
  73.     if (root) {
  74.         afisareSRD(root->st);
  75.         afisareVacanta(root->info);
  76.         afisareSRD(root->dr);
  77.     }
  78. }
  79.  
  80.  
  81. void afisareRSD(nod* root) {
  82.     if (root) {
  83.         afisareVacanta(root->info);
  84.         afisareRSD(root->st);
  85.         afisareRSD(root->dr);
  86.     }
  87. }
  88.  
  89. Vacanta cautareVacantaDupaPret(nod*root, float pret) {
  90.     if (root) {
  91.         if (root->info.pret > pret) {
  92.             return cautareVacantaDupaPret(root->st, pret);
  93.         }
  94.         else if (root->info.pret < pret) {
  95.             return cautareVacantaDupaPret(root->dr, pret);
  96.         }
  97.         else {
  98.             Vacanta rezultat = creareVacanta(root->info.destinatie, root->info.pret);
  99.             return rezultat;
  100.         }
  101.     }
  102.     else {
  103.         Vacanta rezultat = creareVacanta("", -1);
  104.         return rezultat;
  105.     }
  106. }
  107.  
  108. void stergereArbore(nod** root) {
  109.     if (*root) {
  110.         stergereArbore(&(*root)->st);
  111.         stergereArbore(&(*root)->dr);
  112.         free((*root)->info.destinatie);
  113.         free(*root);
  114.         *root = NULL;
  115.     }
  116. }
  117.  
  118.  
  119. int maxim(int a, int b) {
  120.     return (a < b) ? b : a;
  121. }
  122.  
  123. int inaltimeArbore(nod*root) {
  124.     if (root) {
  125.         return 1 + maxim(inaltimeArbore(root->st), inaltimeArbore(root->dr));
  126.     }
  127.     else {
  128.         return 0;
  129.     }
  130. }
  131.  
  132. void afisareNivelDinArbore(nod*root, int nivelCautat, int *nivelCurent) {
  133.     if (root) {
  134.         if (*nivelCurent == nivelCautat) {
  135.             afisareVacanta(root->info);
  136.         }
  137.         else {
  138.             (*nivelCurent)++;
  139.             afisareNivelDinArbore(root->st, nivelCautat, nivelCurent);
  140.  
  141.             (*nivelCurent)--;
  142.             afisareNivelDinArbore(root->dr, nivelCautat, nivelCurent);
  143.         }
  144.     }
  145. }
  146.  
  147. int gradEchilibru(nod* root) {
  148.     return inaltimeArbore(root->dr) - inaltimeArbore(root->st);
  149. }
  150.  
  151. nod* rotireStanga(nod* root) {
  152.     nod* aux = root->dr;
  153.     root->dr = aux->st;
  154.     aux->st = root;
  155.     return aux;
  156. }
  157.  
  158. nod* rotireDreapta(nod* root) {
  159.     nod* temp = root->st;
  160.     root->st = temp->dr;
  161.     temp->dr = root;
  162.     return temp;
  163. }
  164.  
  165. nod* inserareAVL(nod* root, Vacanta v) {
  166.     if (root) {
  167.         if (v.pret < root->info.pret) {
  168.             root->st = inserareAVL(root->st, v);
  169.         }
  170.         else {
  171.             root->dr = inserareAVL(root->dr, v);
  172.         }
  173.         int GE = gradEchilibru(root);
  174.         if (GE == 2) {
  175.             if (gradEchilibru(root->dr) == -1) {
  176.                 root->dr = rotireDreapta(root->dr);
  177.             }
  178.             root = rotireStanga(root);
  179.         }
  180.         if (GE == -2) {
  181.             if (gradEchilibru(root->st) == -1) {
  182.                 root = rotireDreapta(root);
  183.             }
  184.             else {
  185.                 root->st = rotireStanga(root->st);
  186.                 root = rotireDreapta(root);
  187.             }
  188.         }
  189.  
  190.         return root;
  191.     }
  192.     else {
  193.         nod* nou = (nod*)malloc(sizeof(nod));
  194.         nou->info = v;
  195.         nou->st = NULL;
  196.         nou->dr = NULL;
  197.         return nou;
  198.     }
  199. }
  200.  
  201. void main()
  202. {
  203.     nod* root = NULL;
  204.     root = inserareAVL(root, creareVacanta("Dubai", 1));
  205.     root = inserareAVL(root, creareVacanta("Ibiza", 2));
  206.     root = inserareAVL(root, creareVacanta("Monaco", 3));
  207.     root = inserareAVL(root, creareVacanta("Barcelona", 4));
  208.     root = inserareAVL(root, creareVacanta("Tenerife", 5));
  209.     root = inserareAVL(root, creareVacanta("Coasta de azur", 6));
  210.     root = inserareAVL(root, creareVacanta("Coasta de azur", 7));
  211.  
  212.     afisareRSD(root);
  213.  
  214.     nodLista* cap = NULL;
  215.     copiereInLista(root, &cap, 'c');
  216.     printf("\n\n");
  217.  
  218.     stergereArbore(&root);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement