Advertisement
catalin_stefann11

BST

Jul 1st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.22 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include<iostream>
  4. #include<malloc.h>
  5. using namespace std;
  6.  
  7. struct Localitate
  8. {
  9.     int id;
  10.     char* denumire;
  11. };
  12.  
  13. struct Nod
  14. {
  15.     Localitate info;
  16.     Nod* left;
  17.     Nod* right;
  18. };
  19.  
  20. Localitate creareLocalitate(int id, char* denumire)
  21. {
  22.     Localitate l;
  23.     l.id = id;
  24.     l.denumire = (char*)malloc(sizeof(char)*(strlen(denumire) + 1));
  25.     strcpy(l.denumire, denumire);
  26.  
  27.     return l;
  28. }
  29.  
  30. void afisareLocalitate(Localitate l)
  31. {
  32.     printf("Localitatea cu id %d, are denumirea %s.\n", l.id, l.denumire);
  33. }
  34.  
  35. Nod* creareNod(Localitate info, Nod* left, Nod* right)
  36. {
  37.     Nod* nou = (Nod*)malloc(sizeof(Nod));
  38.     nou->info = creareLocalitate(info.id, info.denumire);
  39.     nou->left = left;
  40.     nou->right = right;
  41.  
  42.     return nou;
  43. }
  44.  
  45. Nod* inserareArbore(Nod* root, Localitate info)
  46. {
  47.     if (root)
  48.     {
  49.         if (info.id < root->info.id)
  50.         {
  51.             root->left = inserareArbore(root->left, info);
  52.         }
  53.         else if (info.id > root->info.id)
  54.         {
  55.             root->right = inserareArbore(root->right, info);
  56.         }
  57.         return root;
  58.        
  59.     }
  60.     else
  61.     {
  62.         Nod* nou = creareNod(info, NULL, NULL);
  63.         return nou;
  64.     }
  65. }
  66.  
  67. void afiarePreordine(Nod* root)
  68. {
  69.     if (root)
  70.     {
  71.         afisareLocalitate(root->info);
  72.         afiarePreordine(root->left);
  73.         afiarePreordine(root->right);
  74.        
  75.     }
  76. }
  77.  
  78. void afiareInordine(Nod* root)
  79. {
  80.     if (root)
  81.     {
  82.         afiareInordine(root->left);
  83.         afisareLocalitate(root->info);
  84.         afiareInordine(root->right);
  85.  
  86.     }
  87. }
  88.  
  89. void afiarePostordine(Nod* root)
  90. {
  91.     if (root)
  92.     {
  93.         afiareInordine(root->left);
  94.         afiareInordine(root->right);
  95.         afisareLocalitate(root->info);
  96.  
  97.     }
  98. }
  99.  
  100. int max(int a, int b)
  101. {
  102.     if (a > b)
  103.         return a;
  104.     else
  105.         return b;
  106. }
  107.  
  108. int inaltimeArbore(Nod* root)
  109. {
  110.     if (root)
  111.     {
  112.         return 1 + max(inaltimeArbore(root->left), inaltimeArbore(root->right));
  113.     }
  114.     else
  115.     {
  116.         return 0;
  117.     }
  118. }
  119.  
  120. void afisareNivelArbore(Nod* root, int nivelCautat, int nivelCurent)
  121. {
  122.     if (root)
  123.     {
  124.         if (nivelCurent == nivelCautat)
  125.         {
  126.             afisareLocalitate(root->info);
  127.         }
  128.         else
  129.         {
  130.             (nivelCurent)++;
  131.             afisareNivelArbore(root->left, nivelCautat, nivelCurent);
  132.             (nivelCurent)--;
  133.             afisareNivelArbore(root->right, nivelCautat, nivelCurent);
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139. Nod* cautaNodArbore(Nod* radacina, int cod)
  140. {
  141.     if (radacina)
  142.     {
  143.         if (radacina->info.id == cod)
  144.         {
  145.             return radacina;
  146.         }
  147.         else
  148.         {
  149.             if (radacina->info.id > cod)
  150.             {
  151.                 return cautaNodArbore(radacina->left, cod);
  152.             }
  153.             else
  154.             {
  155.                 return cautaNodArbore(radacina->right, cod);
  156.             }
  157.         }
  158.     }
  159.     else
  160.     {
  161.         return NULL;
  162.     }
  163.  
  164. }
  165.  
  166. void stergereArbore(Nod** root) {
  167.     if (*root) {
  168.         stergereArbore(&(*root)->left);
  169.         stergereArbore(&(*root)->right);
  170.         free((*root)->info.denumire);
  171.         free(*root);
  172.         *root = NULL;
  173.     }
  174. }
  175.  
  176.  
  177. void main()
  178. {
  179.     //Localitate l = creareLocalitate(15, "Bucuresti");
  180.     //afisareLocalitate(l);
  181.  
  182.     Localitate l1 = creareLocalitate(15, "Bucuresti");
  183.     Localitate l2 = creareLocalitate(10, "Bucuresti");
  184.     Localitate l3 = creareLocalitate(20, "Bucuresti");
  185.     Localitate l4 = creareLocalitate(8, "Bucuresti");
  186.     Localitate l5 = creareLocalitate(12, "Bucuresti");
  187.     Localitate l6 = creareLocalitate(17, "Bucuresti");
  188.     Localitate l7 = creareLocalitate(25, "Bucuresti");
  189.  
  190.     //afisareLocalitate(l1);
  191.     //afisareLocalitate(l2);
  192.     //afisareLocalitate(l3);
  193.     //afisareLocalitate(l4);
  194.     //afisareLocalitate(l5);
  195.     //afisareLocalitate(l6);
  196.     //afisareLocalitate(l7);
  197.  
  198.     Nod* arbore = NULL;
  199.  
  200.     arbore=inserareArbore(arbore, l1);
  201.     arbore = inserareArbore(arbore, l2);
  202.     arbore = inserareArbore(arbore, l3);
  203.     arbore = inserareArbore(arbore, l4);
  204.     arbore = inserareArbore(arbore, l5);
  205.     arbore = inserareArbore(arbore, l6);
  206.     arbore = inserareArbore(arbore, l7);
  207.  
  208.     cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
  209.     afiarePreordine(arbore);
  210.     cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
  211.     afiareInordine(arbore);
  212.     cout << "\n==============AFISARE ARBORE PREORDINE================\n\n";
  213.     afiarePostordine(arbore);
  214.  
  215.     int inaltime =inaltimeArbore(arbore);
  216.     printf("\nArborele nostru are %d nivele.", inaltime);
  217.    
  218.  
  219.     afisareNivelArbore(arbore, 3, 0);
  220.  
  221.     if (cautaNodArbore(arbore, 20))
  222.     {
  223.         printf("\nNodul exista!\n");
  224.     }
  225.     else
  226.     {
  227.         printf("\nNodul nu exista!\n");
  228.     }
  229.  
  230.     stergereArbore(&arbore);
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement