Advertisement
Tomonori

Arbori: nr frunze, height, complet, echilibrat

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