Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- fstream f("e:\\info\\arborebinar.txt",ios::in);
- struct nod
- { int info;
- nod *s,*d;
- };
- nod *r;
- void creare(nod *&r)
- { int n;
- f>>n;
- if(n==0)
- r=NULL;
- else
- { r=new nod;
- r->info=n;
- creare(r->s);
- creare(r->d);
- }
- }
- void rsd(nod *r)
- { if(r!=NULL)
- { cout<<r->info<<" ";
- rsd(r->s);
- rsd(r->d);
- }
- }
- int max(int a,int b)
- { if(a>b)
- return a;
- else
- return b;}
- int inaltime(nod *r)
- { if(r==NULL)
- return 0;
- else
- return 1+max(inaltime(r->s),inaltime(r->d));
- }
- int numar(nod *r)
- { if(r==NULL)
- return 0;
- else
- return 1+numar(r->s)+numar(r->d);}
- int frunza(nod *r)
- { if(r==NULL)
- return 0;
- else
- if(r->s==NULL && r->d==NULL)
- return 1+frunza(r->s) +frunza(r->d);
- else
- return frunza(r->s) +frunza(r->d);}
- int nivel(nod *r,int i,int k)
- { if(r==NULL)
- return 0;
- else
- if(i==k)
- return 1+nivel(r->s,i+1,k)+nivel(r->d,i+1,k);
- else
- return nivel(r->s,i+1,k) +nivel(r->d,i+1,k);}
- int main()
- { int k;
- creare(r);
- rsd(r);
- cout<<endl;
- cout<<"inaltimea este "<<inaltime(r)-1<<endl;
- cout<<inaltime(r->s)<<endl;
- cout<<inaltime(r->d)<<endl;
- if(abs(inaltime(r->s)-inaltime(r->d))>1)
- cout<<"arbore neechilibrat"<<endl;
- else
- cout<<"arbore echilibrat"<<endl;
- cout<<"numarul de noduri este "<<numar(r)<<endl;
- cout<<numar(r->s)<<endl;
- cout<<numar(r->d)<<endl;
- if(abs(numar(r->s)- numar(r->d))>1)
- cout<<"nu este perfect echilibrat"<<endl;
- else
- cout<<"perfect echilibrat"<<endl;
- cout<<"numarul de frunze "<<frunza(r)<<endl;
- if(frunza(r)*2-1==numar(r))
- cout<<"arbore strict"<<endl;
- else
- cout<<"nu este arbore strict"<<endl;
- k=inaltime(r)-1;
- if(nivel(r,0,k)==frunza(r))
- cout<<"este complet"<<endl;
- else
- cout<<"nu este complet"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement