arbori de cautare

Arbori de cautare

#include<iostream.h>
#include<conio.h>
struct nod
{int nr;
nod *as,*ad;
};
nod *v,*man;
int k;

void inserare(nod*& c,int k)
{
if(c)
if(c->nr==k)
cout<<“nr inserat”<<endl;
else
if(c->nr<k) inserare(c->ad,k);
else inserare(c->as,k);
else
{c=new nod;c->as=c->ad=0;
c->nr=k;}
}

void parcurg(nod* c)
{if(c)
{parcurg(c->as);
cout<<c->nr<<endl;
parcurg(c->ad);
}
}

void main()
{v=0;
do{
cout<<“k=”;cin>>k;

inserare(v,k);}

while(k!=0);
parcurg(v);
getch();
}

Leave a Comment