Advertisement
ioana_martin98

Untitled

May 24th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "stdlib.h"
  4. #include "string.h"
  5. using namespace std;
  6. struct HeapItem
  7. {
  8.     char* profession;
  9.     float salary;
  10. };
  11.  
  12. struct BST
  13. {
  14.     HeapItem* info;
  15.     BST* left;
  16.     BST* right;
  17.     int GE;
  18. };
  19.  
  20. BST* createNode(HeapItem*);
  21. void insertNode(BST*&, BST*); //radacina, nod nou
  22. HeapItem* createElement(char*, float);
  23. void printSRD(BST*);
  24. void printRSD(BST*);
  25. void printSDR(BST*);
  26. void AfisareStructura(BST*, int);
  27. void AfisareDescendenti(BST*);
  28. void deleteNode(BST**, float);
  29. void stergereLogica(BST**, BST*&);
  30. void main()
  31. {
  32.     FILE* pFile = fopen("Date.txt", "r");
  33.     BST* root = NULL;
  34.     BST* treeFromHeapRoot = NULL;
  35.     if (pFile)
  36.     {
  37.         char buffer[100]; float salary;
  38.         fscanf(pFile, "%s", buffer);
  39.         while (!feof(pFile))
  40.         {
  41.             fscanf(pFile, "%f", &salary);
  42.  
  43.             //1. create an element of type List*
  44.             HeapItem* newElement = createElement(buffer, salary);
  45.             //2. insert the newly created element
  46.             //enqueue(heap, newElement);
  47.  
  48.             BST* item = createNode(newElement);
  49.             insertNode(root, item);
  50.  
  51.             fscanf(pFile, "%s", buffer);
  52.         }
  53.         fclose(pFile);
  54.     }
  55.     printf("----------AfisareStructura-------------\n");
  56.     AfisareStructura(root, 0);
  57.  
  58.     printf("----------Inordine-------------\n");
  59.     printSRD(root);
  60.     printf("----------Preordine-------------\n");
  61.     printRSD(root);
  62.     printf("----------Postordine-------------\n");
  63.     printSDR(root);
  64.     printf("----------Afisare noduri doar cu descendent in dreapta-------------\n");
  65.     AfisareDescendenti(root);
  66.  
  67.     //stergere frunza
  68.     deleteNode(&root, 1230);
  69.  
  70.     printf("----------AfisareStructura-------------\n");
  71.     AfisareStructura(root, 0);
  72.  
  73.     char buf[] = "Profesie";
  74.     HeapItem* newElement = createElement(buf, 2700);
  75.     BST* item = createNode(newElement);
  76.     insertNode(root, item);
  77.  
  78.     printf("----------AfisareStructura-------------\n");
  79.     AfisareStructura(root, 0);
  80.  
  81. }
  82.  
  83. void stergereLogica(BST** root, BST*& descLeft)
  84. {
  85.     if (descLeft->right != NULL)
  86.         stergereLogica(&(*root), descLeft->right);
  87.     else
  88.     {
  89.         HeapItem* tmp = (*root)->info;
  90.         (*root)->info = descLeft->info;
  91.         BST* nodTmp = descLeft;
  92.         descLeft = descLeft->left;
  93.         free(tmp->profession);
  94.         free(tmp);
  95.         free(nodTmp);
  96.     }
  97. }
  98.  
  99. void deleteNode(BST** root, float cheie)
  100. {
  101.     if ((*root) != NULL)
  102.     {
  103.         //apel recursiv (descendent stang/drept)
  104.         if ((*root)->info->salary > cheie)
  105.             deleteNode(&(*root)->left, cheie);
  106.         else if ((*root)->info->salary < cheie)
  107.             deleteNode(&(*root)->right, cheie);
  108.         else
  109.         {
  110.             //stergere nod identificat dupa cheie
  111.             //stergere nod frunza
  112.             if ((*root)->left == NULL && (*root)->right == NULL)
  113.             {
  114.                 BST* tmp = (*root);
  115.                 *root = NULL;
  116.                 free(tmp->info->profession);
  117.                 free(tmp->info);
  118.                 free(tmp);
  119.             }
  120.             //stergere nod cu 1 descendent stang
  121.             else if ((*root)->left != NULL && (*root)->right == NULL)
  122.             {
  123.                 BST* tmp = (*root);
  124.                 *root = (*root)->left;
  125.                 free(tmp->info->profession);
  126.                 free(tmp->info);
  127.                 free(tmp);
  128.             }
  129.             //stergere nod cu 1 descendent drept
  130.             else if ((*root)->left == NULL && (*root)->right != NULL)
  131.             {
  132.                 BST* tmp = (*root);
  133.                 *root = (*root)->right;
  134.                 free(tmp->info->profession);
  135.                 free(tmp->info);
  136.                 free(tmp);
  137.             }
  138.             //stergere nod cu 2 descendenti
  139.             else
  140.             {
  141.                 stergereLogica(&(*root), (*root)->left);
  142.             }
  143.         }
  144.     }
  145. }
  146. void AfisareDescendenti(BST* root) //inordine
  147. {
  148.     if (root != NULL)
  149.     {
  150.         //left
  151.         AfisareDescendenti(root->left);
  152.         //root
  153.         if (root->left == NULL && root->right != NULL)
  154.             printf("Salary: %f\n", root->info->salary);
  155.         //right
  156.         AfisareDescendenti(root->right);
  157.     }
  158. }
  159.  
  160.  
  161. void printSRD(BST* root) //inordine
  162. {
  163.     if (root != NULL)
  164.     {
  165.         //left
  166.         printSRD(root->left);
  167.         //root
  168.         printf("Salary: %f\n", root->info->salary);
  169.         //right
  170.         printSRD(root->right);
  171.     }
  172. }
  173. void printRSD(BST* root) //preordine
  174. {
  175.     if (root != NULL)
  176.     {
  177.         //root
  178.         printf("Salary: %f\n", root->info->salary);
  179.         //left
  180.         printRSD(root->left);
  181.         //right
  182.         printRSD(root->right);
  183.     }
  184. }
  185. void AfisareStructura(BST* root, int nivel)
  186. {
  187.     if (root != NULL)
  188.     {
  189.         for (int i = 0; i < nivel; i++)
  190.             printf("\t");
  191.         printf("%f\n", root->info->salary);
  192.         AfisareStructura(root->left, nivel + 1);
  193.         AfisareStructura(root->right, nivel + 1);
  194.     }
  195.     else
  196.     {
  197.         for (int i = 0; i < nivel; i++)
  198.             printf("\t");
  199.         printf("NULL\n");
  200.     }
  201. }
  202. void printSDR(BST* root) //postordine
  203. {
  204.     if (root != NULL)
  205.     {
  206.         //left
  207.         printSDR(root->left);
  208.         //right
  209.         printSDR(root->right);
  210.         //root
  211.         printf("Salary: %f\n", root->info->salary);
  212.     }
  213. }
  214. int max(int a, int b)
  215. {
  216.     return a > b ? a : b;
  217. }
  218. int calculInaltime(BST* root)
  219. {
  220.     if (root != NULL)
  221.     {
  222.         return 1 + max(calculInaltime(root->left), calculInaltime(root->right));
  223.     }
  224.     else
  225.         return 0;
  226. }
  227. int calculGE(BST* root)
  228. {
  229.     int hsleft = 0, hsright = 0;
  230.     if (root->right != NULL)
  231.         hsright = calculInaltime(root->right);
  232.     if (root->left != NULL)
  233.         hsleft = calculInaltime(root->left);
  234.     return hsright - hsleft;
  235. }
  236.  
  237. BST* RotireDreapta(BST* pivot)
  238. {
  239.     BST* desc = pivot->left;
  240.     pivot->left = desc->right;
  241.     desc->right = pivot;
  242.     pivot = desc;
  243.     return pivot;
  244. }
  245.  
  246. BST* RotireStanga(BST* pivot)
  247. {
  248.     BST* desc = pivot->right;
  249.     pivot->right = desc->left;
  250.     desc->left = pivot;
  251.     pivot = desc;
  252.     return pivot;
  253. }
  254.  
  255. BST* reechilibrare(BST* root)
  256. {
  257.     root->GE = calculGE(root);
  258.     if (root->GE == -2)
  259.     {
  260.         BST* desc = root->left;
  261.         if (desc->GE == -1)
  262.         {
  263.             //rotire la dreapta in pivot
  264.             root = RotireDreapta(root);
  265.         }
  266.         else
  267.         {
  268.             //rotire la stanga in desc
  269.             root->left = RotireStanga(desc);
  270.             //rotire la dreapta in pivot
  271.             root = RotireDreapta(root);
  272.         }
  273.     }
  274.     else if (root->GE == 2)
  275.     {
  276.         BST* desc = root->right;
  277.         if (desc->GE == 1)
  278.         {
  279.             //simpla rotire la stanga in pivot
  280.             root = RotireStanga(root);
  281.         }
  282.         else
  283.         {
  284.             //simpla rotire la dreapta in desc
  285.             root->right = RotireDreapta(desc);
  286.             //simpla rotire la stanga in pivot
  287.             root = RotireStanga(root);
  288.         }
  289.     }
  290.     return root;
  291. }
  292.  
  293. void insertNode(BST*& root, BST* node)
  294. {
  295.     //iteratia n
  296.     if (root == NULL)
  297.     {
  298.         root = node;
  299.     }
  300.     //iteratia n-1
  301.     else
  302.     {
  303.         if (root->info->salary > node->info->salary)
  304.             insertNode(root->left, node);
  305.         else if (root->info->salary < node->info->salary)
  306.             insertNode(root->right, node);
  307.     }
  308.     root = reechilibrare(root);
  309. }
  310. BST* createNode(HeapItem* info)
  311. {
  312.     BST* node = (BST*)malloc(sizeof(BST));
  313.     node->info = info;
  314.     node->GE = 0;
  315.     node->left = node->right = NULL;
  316.     return node;
  317. }
  318.  
  319. HeapItem* createElement(char* buffer, float salary)
  320. {
  321.     //1.allocate memory for the new element
  322.     HeapItem* newElement = (HeapItem*)malloc(sizeof(HeapItem));
  323.     //2.initialize it with the params
  324.     //(the connection attributes should remain NULL)
  325.     newElement->salary = salary;
  326.     newElement->profession = (char*)malloc(strlen(buffer) + 1);
  327.     strcpy(newElement->profession, buffer);
  328.     //3.return the new element
  329.     return newElement;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement