numarul de nivele ale unui arbore

Numarul  de nivele ale unui arbore

 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct nod
{int nr;
nod* st,*dr;
};
nod *c;
int coada[20],s[20],i,sf;
nod *r[20];

nod *arb()
{int n;
nod *c;
cout<<“n=”;cin>>n;
if(n)
{
c=new nod;
c->nr=n;
c->st=arb();
c->dr=arb();
return c;
}
else return 0;
}

int max(int x,int y)
{if (x>y) return x;
else return y;
}

int h(nod *r)
{
if (r==0) return 0;
else return 1+max(h(r->st),h(r->dr));
}

int h1(nod *r)
{if(r==0) return 0;
else return 1+h1(r->st);
}

int h2(nod *r)
{if(r==0) return 0;
else return 1+h2(r->dr);
}

void main()
{int a,b;
//clrscr();
c=arb();
cout<<h(c)<<endl;
cout<<h1(c)<<endl;
cout<<h2(c)<<endl;
getch();
}

Leave a Comment