Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- struct Factura
- {
- int nrFactura;
- char* detaEmiterii;
- char* numeClient;
- float valoareFactura;
- int nrProduse;
- };
- struct AVL
- {
- Factura info;
- AVL* left;
- AVL* right;
- int GE;
- };
- Factura creareFactura(int nr, const char* data, const char* nume, float valoare, int nrProd)
- {
- Factura f;
- f.nrFactura = nr;
- f.detaEmiterii = (char*)malloc(sizeof(char)*(strlen(data) + 1));
- strcpy(f.detaEmiterii, data);
- f.numeClient = (char*)malloc(strlen(nume) + 1);
- strcpy(f.numeClient, nume);
- f.valoareFactura = valoare;
- f.nrProduse = nrProd;
- return f;
- }
- AVL* createNod(Factura info)
- {
- AVL* nou = (AVL*)malloc(sizeof(AVL));
- nou->info = creareFactura(info.nrFactura, info.detaEmiterii, info.numeClient, info.valoareFactura
- , info.nrProduse);
- nou->left = nou->right = NULL;
- nou->GE = 0;
- return nou;
- }
- int maxim(int a, int b)
- {
- return a > b ? a : b;
- }
- int inaltime(AVL* root)
- {
- if (root == NULL)
- {
- return 0;
- }
- else
- {
- return 1 + maxim(inaltime(root->left), inaltime(root->right)); ////
- }
- }
- int calculGE(AVL* root)
- {
- return inaltime(root->right) - inaltime(root->left);
- }
- AVL* leftRotation(AVL* root)
- {
- AVL* desc = root->right;
- root->right = desc->left;
- desc->left = root;
- root = desc;
- return root;
- }
- AVL* rightRotation(AVL* root)
- {
- AVL* desc = root->left;
- root->left = desc->right;
- desc->right = root;
- root = desc;
- return root;
- }
- void rebalance(AVL*& root)
- {
- AVL* desc = NULL;
- root->GE = calculGE(root);
- if (root->GE == 2)
- {
- desc = root->right;
- if (desc->GE < 0)
- {
- root->right = rightRotation(desc);
- }
- root = leftRotation(root);
- }
- else if (root->GE == -2)
- {
- desc = root->left;
- if (desc->GE > 0)
- {
- root->left = leftRotation(desc);
- }
- root = rightRotation(root);
- }
- }
- void inserareAVL(AVL*& root, Factura f) {
- if (root == NULL)
- {
- root = createNod(f);
- }
- else
- {
- if (f.nrFactura < root->info.nrFactura)
- {
- inserareAVL(root->left, f);
- }
- else {
- if (f.nrFactura > root->info.nrFactura)
- {
- inserareAVL(root->right, f);
- }
- else {
- printf("\nCheie existenta!");
- }
- }
- }
- rebalance(root);
- }
- void stergereLogica(AVL*& root, AVL*& right)
- {
- if (right->left)
- {
- stergereLogica(root, right->left);
- }
- else {
- root->info = right->info;
- AVL* aux = right;
- right = right->right;
- free(aux);
- }
- }
- Factura stergereNod(AVL*& root, int cheie)
- {
- Factura fact;
- if (root) {
- if (cheie < root->info.nrFactura)
- {
- fact = stergereNod(root->left, cheie);
- }
- else if (cheie > root->info.nrFactura)
- {
- fact = stergereNod(root->right, cheie);
- }
- else
- {
- fact = creareFactura(root->info.nrFactura, root->info.detaEmiterii, root->info.numeClient,
- root->info.valoareFactura, root->info.nrProduse);
- if (root->right == NULL && root->left == NULL)
- {
- free(root->info.detaEmiterii);
- free(root->info.numeClient);
- free(root);
- root = NULL;
- }
- else if (root->right == NULL)
- {
- AVL* aux = root;
- root = root->left;
- free(aux->info.detaEmiterii);
- free(aux->info.numeClient);
- free(aux);
- }
- else if (root->left == NULL)
- {
- AVL* aux = root;
- root = root->right;
- free(aux->info.detaEmiterii);
- free(aux->info.numeClient);
- free(aux);
- }
- else {
- stergereLogica(root, root->right);
- }
- }
- }
- return fact;
- }
- void afisareInOrdine(AVL* root)
- {
- if (root)
- {
- afisareInOrdine(root->left);
- printf("\nFactura %d din data %s - client: %s, valoare %5.2f, numar produse: %d",
- root->info.nrFactura, root->info.detaEmiterii, root->info.numeClient,
- root->info.valoareFactura, root->info.nrProduse);
- afisareInOrdine(root->right);
- }
- }
- float facturiDupaData(AVL* root, char* data)
- {
- if (root)
- {
- if (strcmp(root->info.detaEmiterii, data) > 0)
- return root->info.valoareFactura + facturiDupaData(root->left,
- data) + facturiDupaData(root->right, data);
- return facturiDupaData(root->left, data) + facturiDupaData(root->right, data);
- }
- else return 0;
- }
- void main()
- {
- AVL* root = NULL;
- inserareAVL(root, creareFactura(1, "10/10/2010", "MAXUM", 100, 5));
- inserareAVL(root, creareFactura(2, "1/10/2010", "BEIRUT", 200, 5));
- inserareAVL(root, creareFactura(3, "1/9/2018", "SOFIA", 300, 5));
- inserareAVL(root, creareFactura(4, "15/1/2013", "LUISA", 400, 5));
- inserareAVL(root, creareFactura(5, "11/4/2010", "MIHAELA", 500, 5));
- inserareAVL(root, creareFactura(6, "1/10/2010", "COSMIN", 600, 5));
- inserareAVL(root, creareFactura(7, "9/11/1997", "CATALIN", 700, 5));
- Factura f1 = stergereNod(root, 2);
- afisareInOrdine(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement