Advertisement
catalin_stefann11

AVL

Jul 1st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4.  
  5. struct Factura
  6. {
  7.     int nrFactura;
  8.     char* detaEmiterii;
  9.     char* numeClient;
  10.     float valoareFactura;
  11.     int nrProduse;
  12. };
  13.  
  14. struct AVL
  15. {
  16.     Factura info;
  17.     AVL* left;
  18.     AVL* right;
  19.     int GE;
  20. };
  21.  
  22. Factura creareFactura(int nr, const char* data, const char* nume, float valoare, int nrProd)
  23. {
  24.     Factura f;
  25.     f.nrFactura = nr;
  26.     f.detaEmiterii = (char*)malloc(sizeof(char)*(strlen(data) + 1));
  27.     strcpy(f.detaEmiterii, data);
  28.     f.numeClient = (char*)malloc(strlen(nume) + 1);
  29.     strcpy(f.numeClient, nume);
  30.     f.valoareFactura = valoare;
  31.     f.nrProduse = nrProd;
  32.     return f;
  33. }
  34.  
  35. AVL* createNod(Factura info)
  36. {
  37.     AVL* nou = (AVL*)malloc(sizeof(AVL));
  38.     nou->info = creareFactura(info.nrFactura, info.detaEmiterii, info.numeClient, info.valoareFactura
  39.         , info.nrProduse);
  40.     nou->left = nou->right = NULL;
  41.     nou->GE = 0;
  42.     return nou;
  43. }
  44.  
  45. int maxim(int a, int b)
  46. {
  47.     return a > b ? a : b;
  48. }
  49.  
  50. int inaltime(AVL* root)
  51. {
  52.     if (root == NULL)
  53.     {
  54.         return 0;
  55.     }
  56.     else
  57.     {
  58.         return 1 + maxim(inaltime(root->left), inaltime(root->right));  ////
  59.     }
  60. }
  61.  
  62. int calculGE(AVL* root)
  63. {
  64.     return inaltime(root->right) - inaltime(root->left);
  65. }
  66.  
  67. AVL* leftRotation(AVL* root)
  68. {
  69.     AVL* desc = root->right;
  70.     root->right = desc->left;
  71.     desc->left = root;
  72.     root = desc;
  73.     return root;
  74. }
  75.  
  76. AVL* rightRotation(AVL* root)
  77. {
  78.     AVL* desc = root->left;
  79.     root->left = desc->right;
  80.     desc->right = root;
  81.     root = desc;
  82.     return root;
  83. }
  84.  
  85. void rebalance(AVL*& root)
  86. {
  87.     AVL* desc = NULL;
  88.     root->GE = calculGE(root);
  89.  
  90.     if (root->GE == 2)
  91.     {
  92.         desc = root->right;
  93.         if (desc->GE < 0)
  94.         {
  95.             root->right = rightRotation(desc);
  96.         }
  97.         root = leftRotation(root);
  98.     }
  99.     else if (root->GE == -2)
  100.     {
  101.         desc = root->left;
  102.         if (desc->GE > 0)
  103.         {
  104.             root->left = leftRotation(desc);
  105.         }
  106.         root = rightRotation(root);
  107.     }
  108. }
  109.  
  110. void inserareAVL(AVL*& root, Factura f) {
  111.  
  112.     if (root == NULL)
  113.     {
  114.         root = createNod(f);
  115.     }
  116.     else
  117.     {
  118.         if (f.nrFactura < root->info.nrFactura)
  119.         {
  120.             inserareAVL(root->left, f);
  121.         }
  122.         else {
  123.             if (f.nrFactura > root->info.nrFactura)
  124.             {
  125.                 inserareAVL(root->right, f);
  126.             }
  127.             else {
  128.                 printf("\nCheie existenta!");
  129.             }
  130.         }
  131.     }
  132.     rebalance(root);
  133. }
  134.  
  135. void stergereLogica(AVL*& root, AVL*& right)
  136. {
  137.     if (right->left)
  138.     {
  139.         stergereLogica(root, right->left);
  140.     }
  141.     else {
  142.         root->info = right->info;
  143.         AVL* aux = right;
  144.         right = right->right;
  145.         free(aux);
  146.     }
  147. }
  148.  
  149.  
  150. Factura stergereNod(AVL*& root, int cheie)
  151. {
  152.     Factura fact;
  153.     if (root) {
  154.        
  155.         if (cheie < root->info.nrFactura)
  156.         {
  157.             fact = stergereNod(root->left, cheie);
  158.         }
  159.         else if (cheie > root->info.nrFactura)
  160.         {
  161.             fact = stergereNod(root->right, cheie);
  162.         }
  163.         else
  164.         {
  165.             fact = creareFactura(root->info.nrFactura, root->info.detaEmiterii, root->info.numeClient,
  166.                 root->info.valoareFactura, root->info.nrProduse);
  167.            
  168.             if (root->right == NULL && root->left == NULL)
  169.             {
  170.                 free(root->info.detaEmiterii);
  171.                 free(root->info.numeClient);
  172.                 free(root);
  173.                 root = NULL;
  174.             }
  175.             else if (root->right == NULL)
  176.             {
  177.                 AVL* aux = root;
  178.                 root = root->left;
  179.                 free(aux->info.detaEmiterii);
  180.                 free(aux->info.numeClient);
  181.                 free(aux);
  182.             }
  183.             else if (root->left == NULL)
  184.             {
  185.                 AVL* aux = root;
  186.                 root = root->right;
  187.                 free(aux->info.detaEmiterii);
  188.                 free(aux->info.numeClient);
  189.                 free(aux);
  190.             }
  191.             else {
  192.                 stergereLogica(root, root->right);
  193.             }
  194.         }
  195.     }
  196.     return fact;
  197. }
  198.  
  199. void afisareInOrdine(AVL* root)
  200. {
  201.     if (root)
  202.     {
  203.         afisareInOrdine(root->left);
  204.         printf("\nFactura %d din data %s - client: %s, valoare %5.2f, numar produse: %d",
  205.             root->info.nrFactura, root->info.detaEmiterii, root->info.numeClient,
  206.             root->info.valoareFactura, root->info.nrProduse);
  207.         afisareInOrdine(root->right);
  208.     }
  209. }
  210.  
  211. float facturiDupaData(AVL* root, char* data)
  212. {
  213.     if (root)
  214.     {
  215.         if (strcmp(root->info.detaEmiterii, data) > 0)
  216.             return root->info.valoareFactura + facturiDupaData(root->left,
  217.                 data) + facturiDupaData(root->right, data);
  218.         return facturiDupaData(root->left, data) + facturiDupaData(root->right, data);
  219.     }
  220.     else return 0;
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. void main()
  234. {
  235.     AVL* root = NULL;
  236.     inserareAVL(root, creareFactura(1, "10/10/2010", "MAXUM", 100, 5));
  237.     inserareAVL(root, creareFactura(2, "1/10/2010", "BEIRUT", 200, 5));
  238.     inserareAVL(root, creareFactura(3, "1/9/2018", "SOFIA", 300, 5));
  239.     inserareAVL(root, creareFactura(4, "15/1/2013", "LUISA", 400, 5));
  240.     inserareAVL(root, creareFactura(5, "11/4/2010", "MIHAELA", 500, 5));
  241.     inserareAVL(root, creareFactura(6, "1/10/2010", "COSMIN", 600, 5));
  242.     inserareAVL(root, creareFactura(7, "9/11/1997", "CATALIN", 700, 5));
  243.  
  244.     Factura f1 = stergereNod(root, 2);
  245.  
  246.  
  247.     afisareInOrdine(root);
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement