Pagini recente » Cod sursa (job #2311135) | Cod sursa (job #442723) | Cod sursa (job #2533823) | Cod sursa (job #2188194) | Cod sursa (job #1261139)
#include<cstdio>
typedef struct nod nod;
struct nod{
int info;
nod *st,*dr;
};
nod *arb;
int n,i,j,k,x;
void insert(int x,nod *&arbore)
{
nod *p=new nod;
p->info=x;
p->st=NULL;
p->dr=NULL;
if (arbore==NULL) {arb=p;return;}
nod *q=arbore;
while (q!=NULL)
{
if (x>=q->info)
{
if (q->dr==NULL) {q->dr=p;return;}
q=q->dr;}
if (x<q->info)
{
if (q->st==NULL) {q->st=p;return;}
q=q->st;
}
}
}
int search(int x,nod *arb)
{
nod *p=arb;
while (p!=NULL)
{
if (p->info==x) return 1;
if (x > p->info) p=p->dr ;else
p=p->st;
}
return 0;
}
void SRD(nod *p)
{
if (p==NULL) return;else
{
SRD(p->st);
printf("%d ",p->info);
SRD(p->dr);
}
}
int main()
{
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
scanf("%d",&n);
arb=NULL;
for (i=1;i<=n;i++)
{
scanf("%d",&x);
insert(x,arb);
}
nod *p=arb;
SRD(p);
return 0;
}