Advertisement
Al3X044

main.c

Mar 21st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "avl.h"
  4.  
  5. int n, x[20], use[20];
  6. int y = 8;
  7.  
  8. extern int reechilibrari;
  9.  
  10. typedef struct ab{
  11.     int cheie;
  12.     struct ab* st;
  13.     struct ab* dr;
  14. }AB;
  15. struct ab* rad;
  16.  
  17. void adauga_nod_ab_ord(int x, AB **R) {
  18.     if ((*R) != NULL) {
  19.         if (x <= (*R)->cheie)
  20.             adauga_nod_ab_ord(x, &(*R)->st);
  21.         else adauga_nod_ab_ord(x, &(*R)->dr);
  22.     }
  23.     else
  24.     {
  25.         AB * n;
  26.         n = (AB*)malloc(sizeof(AB));
  27.         n->cheie = x;
  28.         n->st = NULL;
  29.         n->dr = NULL;
  30.         *R = n;
  31.     }
  32. }
  33.  
  34. void tipareste_arbore(struct ab *rad, int h)
  35. {
  36.     int i;
  37.     if(rad != NULL)
  38.     {
  39.         tipareste_arbore(rad->st, h - 1);
  40.         for(i = 1; i <= h; i++)
  41.             printf("%3s", " ");
  42.         printf("%3d\n", rad->cheie);
  43.         tipareste_arbore(rad->dr, h - 1);
  44.     }
  45. }
  46.  
  47. int inaltime_ab(AB *t)
  48. {
  49.     if (t != NULL)
  50.     {
  51.         int h_st = 1 + inaltime_ab(t->st);
  52.         int h_dr = 1 + inaltime_ab(t->dr);
  53.  
  54.         if (h_st > h_dr)
  55.             return h_st;
  56.         else return h_dr;
  57.     }
  58.     else return 0;
  59. }
  60.  
  61. void afisare_sol()
  62. {
  63.     int i;
  64.     for(i = 1; i <= n; i++)
  65.         printf("%d ", x[i]);
  66.  
  67.     printf("\n\nArbore AVL:");
  68.     TNodAVL *radacina;
  69.  
  70.     radacina = NULL; //initializare arbore
  71.     for(i = 1; i <= n; i++)
  72.         radacina = adauga(radacina, i);
  73.     tiparArbore(radacina);
  74.     printf("H = %d\n", inaltime(radacina));
  75.  
  76.     printf("Numar reechilibrari AVL: %d\n", reechilibrari);
  77.  
  78.     printf("\nArbore binar ordonat:\n");
  79.     for (i = 1; i <= n; i++)
  80.         adauga_nod_ab_ord(x[i], &rad);
  81.     tipareste_arbore(rad, inaltime_ab(rad)-1);
  82.     printf("H = %d\n", inaltime_ab(rad));
  83.     rad = NULL;
  84.  
  85.     if (arbori_geom(rad, radacina))
  86.         printf("\nArborii sunt simetrici.\n");
  87.     else printf("\nArborii NU sunt simetrici.\n");
  88.  
  89.     printf("\n*************************************************************\n");
  90. }
  91. void perm(int k)
  92. {
  93.     int i;
  94.     if(k==n+1)
  95.         afisare_sol();
  96.     else
  97.         for(i=1;i<=n;i++)
  98.             if(!use[i])
  99.             {
  100.                 x[k]=i;use[i]=1;
  101.                 perm(k+1);
  102.                 use[i]=0;
  103.             }
  104. }
  105.  
  106. int arbori_geom(AB *a, TNodAVL *b)
  107. {
  108.     if(a == NULL && b == NULL)
  109.         return 1;
  110.     if(a != NULL && b != NULL)
  111.     {
  112.         return (
  113.             arbori_geom(a->st, b->stg) && arbori_geom(a->dr, b->dr)
  114.         );
  115.     }
  116.     return 0;
  117. }
  118.  
  119. int main()
  120. {
  121.     scanf("%d", &n);
  122.     perm(1);
  123.  
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement