Alx09

Untitled

Dec 17th, 2020
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. typedef struct pn {
  5.     int valoare;
  6.     struct pn* sting, *drept;
  7. }nod; /* tipul unui nod din arborele binar ordonat */
  8. nod* root = NULL; /* radacina arborelui */
  9.  
  10. nod* adauga(nod* t, int v) {
  11.     if (t == NULL)
  12.         if ((t = (nod*)malloc(sizeof(nod))) == NULL) {
  13.             printf("Eroare: memorie insuficienta\n");
  14.             exit(1);
  15.         }
  16.         else {/* s-a putut crea nodul */
  17.             t->valoare = v;
  18.             t->sting = t->drept = NULL;
  19.         }
  20.     else
  21.         if (t->valoare < v)
  22.             /* inserarea e în subarborele drept */
  23.             t->drept = adauga(t->drept, v);
  24.         else
  25.             if (t->valoare > v)
  26.                 /* inserarea e în subarborele stâng */
  27.                 t->sting = adauga(t->sting, v);
  28.             else
  29.                 printf("Valoarea % d apare in evidenta\n", v);
  30.     return t;
  31. } /* adauga */
  32.  
  33. void tipareste_inordine(nod* t) {
  34.     if (t != NULL) {
  35.         tipareste_inordine(t->sting); /* tipareste subarborele stang */
  36.         printf("%d ", t->valoare);
  37.         tipareste_inordine(t->drept); /* tipareste subarborele drept */
  38.     }
  39. } /* tipareste */
  40.  
  41. void tipareste_preordine(nod* t) {
  42.     if (t != NULL) {
  43.         printf("%d ", t->valoare);
  44.         tipareste_preordine(t->sting); /* tipareste subarborele stang */
  45.  
  46.         tipareste_preordine(t->drept); /* tipareste subarborele drept */
  47.     }
  48. } /* tipareste */
  49. void tipareste_postordine(nod* t) {
  50.     if (t != NULL) {
  51.  
  52.         tipareste_postordine(t->sting); /* tipareste subarborele stang */
  53.  
  54.         tipareste_postordine(t->drept); /* tipareste subarborele drept */
  55.         printf("%d ", t->valoare);
  56.     }
  57. } /* tipareste */
  58. nod* supred(nod* t, nod* p) {
  59.     nod* q, *q_aux;
  60.     q = t;
  61.     if (q->drept != NULL)
  62.         q->drept = supred(q->drept, p); /* gaseste nodul cel mai mare din subarborele indicat de t */
  63.     else { /* pentru nodul cel mai mare din subarborele indicat de t */
  64.     /* muta campurile nodului indicat de q în nodul indicat de p */
  65.         p->valoare = q->valoare;
  66.         q_aux = q;
  67.         q = q->sting; /* fiul stang este returnat */
  68.         free(q_aux); /* elibereaza memoria ocupata de nod */
  69.     }
  70.     return q;
  71. } /* supred */
  72. nod* elimina(nod* p, int v) {
  73.     nod* q;
  74.     if (p == NULL)
  75.         printf("Eroare: %d nu apare in evidenta\n", v);
  76.     else
  77.         if (p->valoare < v)
  78.             /* nodul e in subarborele drept */
  79.             p->drept = elimina(p->drept, v);
  80.         else
  81.             if (p->valoare > v)
  82.                 /* nodul e in subarborele stang */
  83.                 p->sting = elimina(p->sting, v);
  84.             else /* s-a gasit nodul ce trebuie sters */
  85.                 if (p->sting == NULL) { /* daca nu are fiu stang */
  86.                     q = p->drept;
  87.                     free(p);
  88.                     return q;
  89.                 }
  90.                 else
  91.                     if (p->drept == NULL) { /* nu are fiu drept */
  92.                         q = p->sting;
  93.                         free(p);
  94.                         return q;
  95.                     }
  96.                     else /* are ambii fii */
  97.                         p->sting = supred(p->sting, p);
  98.     return p;
  99. } /* elimina */
  100.  
  101. int main() {
  102.     int opt;
  103.     int x;
  104.     do {
  105.         printf("[1] Inserare nod\n");
  106.         printf("[2] Afisare inordine SVD\n");
  107.         printf("[3] Afisare preordine VSD\n");
  108.         printf("[4] Afisare postordine SDV\n");
  109.         printf("Opt:");
  110.         (void)scanf("%d", &opt);
  111.         system("cls");
  112.         switch (opt)
  113.         {
  114.         case 1:
  115.             printf("Valoare :");
  116.             (void)scanf("%d", &x);
  117.             root = adauga(root, x);
  118.             break;
  119.         case 2:
  120.             tipareste_inordine(root);
  121.             printf(" -Inordine de la stanga la drapta\n\n");
  122.             break;
  123.         case 3:
  124.             tipareste_preordine(root);
  125.             printf("preordine varf la stanga  ramura din stanga apoi ramura din dreapta\n\n");
  126.             break;
  127.         case 4:
  128.             tipareste_postordine(root);
  129.             printf("postordine pleaca din ramura din stanga catre ramura dreapta, ultimul afisat este varful\n");
  130.             break;
  131.         default:
  132.             break;
  133.         }
  134.     } while (opt);
  135.     return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment