Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<bits/stdc++.h>
- using namespace std;
- ifstream fin("arbore.in");
- int n,t[20],i,r,h,niv,x,a,j;
- int radacina()
- {
- for(i=1;i<=n;i++)
- if(t[i]==0)
- {
- return i;
- }
- }
- void frunze()
- {
- int j,ok;
- for(i=1;i<=n;i++)
- {
- ok=0;
- for(j=1;j<=n;j++)
- if(t[j]==i)
- {
- ok=1;
- break;
- }
- if(ok==0)
- cout<<i<<" ";
- }
- }
- void neterminale()
- {
- int j,ok;
- for(i=1;i<=n;i++)
- {
- ok=0;
- for(j=1;j<=n;j++)
- if(t[j]==i)
- {
- ok=1;
- break;
- }
- if(ok==1)
- cout<<i<<" ";
- }
- }
- void inaltime(int x,int l)
- {
- int i;
- if(l>h)
- h=l;
- for(i=1;i<=n;i++)
- if(t[i]==x )
- inaltime(i,l+1);
- }
- void nivele(int x,int l,int a)
- {
- int i;
- if(l==a)
- cout<<x<<" ";
- for(i=1;i<=n;i++)
- if(t[i]==x )
- nivele(i,l+1,a);
- }
- void descendenti_directi(int x)
- {
- for(i=1;i<=n;i++)
- if(t[i]==x)
- cout<<i<<" ";
- }
- void descendentii(int x)
- {
- int i;
- for(i=1;i<=n;i++)
- if(t[i]==x )
- {
- cout<<i<<" ";
- descendentii(i);
- }
- }
- void ascendentii(int x)
- {
- int i;
- if(t[x]!=0)
- {
- cout<<t[x]<<" ";
- ascendentii(t[x]);
- }
- }
- int main()
- {
- fin>>n;
- for(i=1;i<=n;i++)
- fin>>t[i];
- r=radacina();
- cout<<"Radacina este "<<r<<endl;
- cout<<"Frunzele sunt ";
- frunze();
- cout<<endl;
- cout<<"Nodurile neterminale sunt ";
- neterminale();
- cout<<endl;
- inaltime(r,0);
- cout<<"Inaltimea arborelui este "<<h<<endl;
- for(j=0;j<=h;j++)
- {
- cout<<endl;
- cout<<"Nivelul "<<j<<" : ";
- nivele(r,0,j);
- }
- cout<<endl<<"Nodul este ";
- cin>>x;
- cout<<"Descendentii directi a lui "<<x<<" sunt ";
- descendenti_directi(x);
- cout<<endl;
- cout<<"Toti descendentii lui "<<x<<" sunt ";
- descendentii(x);
- cout<<endl<<"Ascendentii lui "<<x<<" sunt ";
- ascendentii(x);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement