Advertisement
monyca98

arbore binar strict echilibrat perfect echilibrat complet

May 30th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include<fstream>
  2. #include<iostream>
  3. #include<stdlib.h>
  4. using namespace std;
  5. fstream f("e:\\info\\arborebinar.txt",ios::in);
  6. struct nod
  7.     { int info;
  8.       nod *s,*d;
  9.       };
  10.       nod *r;
  11. void creare(nod *&r)
  12. {   int n;
  13.      f>>n;
  14.      if(n==0)
  15.         r=NULL;
  16.      else
  17.     {   r=new nod;
  18.         r->info=n;
  19.         creare(r->s);
  20.         creare(r->d);
  21.     }
  22. }
  23. void rsd(nod *r)
  24.  {  if(r!=NULL)
  25.     {   cout<<r->info<<" ";
  26.         rsd(r->s);
  27.         rsd(r->d);
  28.     }
  29. }
  30. int max(int a,int b)
  31. {   if(a>b)
  32.         return a;
  33.     else
  34.         return b;}
  35. int inaltime(nod *r)
  36. {   if(r==NULL)
  37.        return 0;
  38.     else
  39.        return 1+max(inaltime(r->s),inaltime(r->d));
  40. }
  41. int numar(nod *r)
  42. {   if(r==NULL)
  43.         return 0;
  44.     else
  45.         return 1+numar(r->s)+numar(r->d);}
  46. int frunza(nod *r)
  47. {   if(r==NULL)
  48.        return 0;
  49.     else
  50.        if(r->s==NULL && r->d==NULL)
  51.             return 1+frunza(r->s) +frunza(r->d);
  52.         else
  53.             return frunza(r->s) +frunza(r->d);}
  54. int nivel(nod *r,int i,int k)
  55. {   if(r==NULL)
  56.        return 0;
  57.     else
  58.        if(i==k)
  59.             return 1+nivel(r->s,i+1,k)+nivel(r->d,i+1,k);
  60.         else
  61.             return nivel(r->s,i+1,k) +nivel(r->d,i+1,k);}
  62.  
  63.  
  64.  
  65.  int main()
  66. {   int k;
  67.     creare(r);
  68.     rsd(r);
  69.     cout<<endl;
  70.     cout<<"inaltimea este "<<inaltime(r)-1<<endl;
  71.     cout<<inaltime(r->s)<<endl;
  72.      cout<<inaltime(r->d)<<endl;
  73.      if(abs(inaltime(r->s)-inaltime(r->d))>1)
  74.         cout<<"arbore neechilibrat"<<endl;
  75.      else
  76.         cout<<"arbore echilibrat"<<endl;
  77.      cout<<"numarul de noduri este "<<numar(r)<<endl;
  78.      cout<<numar(r->s)<<endl;
  79.      cout<<numar(r->d)<<endl;
  80.      if(abs(numar(r->s)- numar(r->d))>1)
  81.         cout<<"nu este perfect echilibrat"<<endl;
  82.      else
  83.         cout<<"perfect echilibrat"<<endl;
  84.     cout<<"numarul de frunze "<<frunza(r)<<endl;
  85.      if(frunza(r)*2-1==numar(r))
  86.         cout<<"arbore strict"<<endl;
  87.      else
  88.         cout<<"nu este arbore strict"<<endl;
  89.      k=inaltime(r)-1;
  90.      if(nivel(r,0,k)==frunza(r))
  91.         cout<<"este complet"<<endl;
  92.      else
  93.         cout<<"nu este complet"<<endl;
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement