Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "avl.h"
- int n, x[20], use[20];
- int y = 8;
- extern int reechilibrari;
- typedef struct ab{
- int cheie;
- struct ab* st;
- struct ab* dr;
- }AB;
- struct ab* rad;
- void adauga_nod_ab_ord(int x, AB **R) {
- if ((*R) != NULL) {
- if (x <= (*R)->cheie)
- adauga_nod_ab_ord(x, &(*R)->st);
- else adauga_nod_ab_ord(x, &(*R)->dr);
- }
- else
- {
- AB * n;
- n = (AB*)malloc(sizeof(AB));
- n->cheie = x;
- n->st = NULL;
- n->dr = NULL;
- *R = n;
- }
- }
- void tipareste_arbore(struct ab *rad, int h)
- {
- int i;
- if(rad != NULL)
- {
- tipareste_arbore(rad->st, h - 1);
- for(i = 1; i <= h; i++)
- printf("%3s", " ");
- printf("%3d\n", rad->cheie);
- tipareste_arbore(rad->dr, h - 1);
- }
- }
- int inaltime_ab(AB *t)
- {
- if (t != NULL)
- {
- int h_st = 1 + inaltime_ab(t->st);
- int h_dr = 1 + inaltime_ab(t->dr);
- if (h_st > h_dr)
- return h_st;
- else return h_dr;
- }
- else return 0;
- }
- void afisare_sol()
- {
- int i;
- for(i = 1; i <= n; i++)
- printf("%d ", x[i]);
- printf("\n\nArbore AVL:");
- TNodAVL *radacina;
- radacina = NULL; //initializare arbore
- for(i = 1; i <= n; i++)
- radacina = adauga(radacina, i);
- tiparArbore(radacina);
- printf("H = %d\n", inaltime(radacina));
- printf("Numar reechilibrari AVL: %d\n", reechilibrari);
- printf("\nArbore binar ordonat:\n");
- for (i = 1; i <= n; i++)
- adauga_nod_ab_ord(x[i], &rad);
- tipareste_arbore(rad, inaltime_ab(rad)-1);
- printf("H = %d\n", inaltime_ab(rad));
- rad = NULL;
- if (arbori_geom(rad, radacina))
- printf("\nArborii sunt simetrici.\n");
- else printf("\nArborii NU sunt simetrici.\n");
- printf("\n*************************************************************\n");
- }
- void perm(int k)
- {
- int i;
- if(k==n+1)
- afisare_sol();
- else
- for(i=1;i<=n;i++)
- if(!use[i])
- {
- x[k]=i;use[i]=1;
- perm(k+1);
- use[i]=0;
- }
- }
- int arbori_geom(AB *a, TNodAVL *b)
- {
- if(a == NULL && b == NULL)
- return 1;
- if(a != NULL && b != NULL)
- {
- return (
- arbori_geom(a->st, b->stg) && arbori_geom(a->dr, b->dr)
- );
- }
- return 0;
- }
- int main()
- {
- scanf("%d", &n);
- perm(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement