Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- ifstream f("e:\\info\\arborebinar.txt");
- 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)//preordine parcurgere radacina stanga dreapta
- { if(r!=NULL)
- { cout<<r->info<<" ";
- rsd(r->s);
- rsd(r->d);
- }
- }
- int maxim(int a,int b)
- { if(a>b)
- return a;
- else
- return b;
- }
- int inaltime(nod *r)
- { if(r==NULL)
- return 0;
- else
- return 1+maxim(inaltime(r->s),inaltime(r->d));
- }
- int frunze(nod*r)
- { if(r==NULL)
- return 0;
- else
- if(r->s==NULL && r->d==NULL)
- return 1+frunze(r->s)+frunze(r->d);
- else
- return frunze(r->s)+frunze(r->d);
- }
- int succesor(nod *r)
- { if(r==NULL)
- return 0;
- else
- if(r->s==NULL && r->d!=NULL || r->s!=NULL && r->d==NULL)
- return 1+succesor(r->s)+succesor(r->d);
- else
- return succesor(r->s)+succesor(r->d);
- }
- void nivel(nod *r,int i,int k)
- {if(r!=NULL)
- {if(i==k)
- cout<<r->info<<" ";
- nivel(r->s,i+1,k);
- nivel(r->d,i+1,k);}}
- int main()
- { int k;
- creare(r);
- cout<<"rsd=";
- rsd(r);
- cout<<endl;
- cout<<"arborele are inaltimea "<<inaltime(r)<<endl;
- cout<<" si are "<< frunze(r)<<" frunze"<<endl;
- cout<<"sunt "<<succesor(r)<<" noduri cu un singur succesor"<<endl;
- cout<<"k=";cin>>k;
- cout<<"pe nivelul "<<k<<" sunt nodurile:";
- nivel(r,0,k);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement