Advertisement
monyca98

arbori binari inaltime frunze nivel succesor

May 30th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. ifstream f("e:\\info\\arborebinar.txt");
  5. struct nod
  6. {   int info;
  7.     nod *s,*d;
  8. };
  9. nod *r;
  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)//preordine parcurgere radacina stanga dreapta
  23. {   if(r!=NULL)
  24.     {   cout<<r->info<<" ";
  25.         rsd(r->s);
  26.         rsd(r->d);
  27.     }
  28. }
  29. int maxim(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+maxim(inaltime(r->s),inaltime(r->d));
  40. }
  41. int frunze(nod*r)
  42. {   if(r==NULL)
  43.         return 0;
  44.     else
  45.         if(r->s==NULL && r->d==NULL)
  46.             return 1+frunze(r->s)+frunze(r->d);
  47.         else
  48.             return frunze(r->s)+frunze(r->d);
  49. }
  50. int succesor(nod *r)
  51. {   if(r==NULL)
  52.         return 0;
  53.     else
  54.         if(r->s==NULL && r->d!=NULL || r->s!=NULL && r->d==NULL)
  55.             return 1+succesor(r->s)+succesor(r->d);
  56.         else
  57.             return succesor(r->s)+succesor(r->d);
  58. }
  59. void nivel(nod *r,int i,int k)
  60.    {if(r!=NULL)
  61.    {if(i==k)
  62.    cout<<r->info<<" ";
  63.   nivel(r->s,i+1,k);
  64.   nivel(r->d,i+1,k);}}
  65.  
  66.  
  67. int main()
  68. {   int k;
  69.     creare(r);
  70.     cout<<"rsd=";
  71.     rsd(r);
  72.     cout<<endl;
  73.     cout<<"arborele are inaltimea "<<inaltime(r)<<endl;
  74.     cout<<" si are "<< frunze(r)<<" frunze"<<endl;
  75.     cout<<"sunt "<<succesor(r)<<" noduri cu un singur succesor"<<endl;
  76.     cout<<"k=";cin>>k;
  77.     cout<<"pe nivelul "<<k<<" sunt nodurile:";
  78.     nivel(r,0,k);
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement