Pagini recente » Cod sursa (job #2042576) | Cod sursa (job #2520182) | Cod sursa (job #421445) | Cod sursa (job #2812137) | Cod sursa (job #1266174)
#include <stdio.h>
int n;
struct nod
{
nod *st, *dr;//*t;
int x;//,h;
}*rad,*r,*p,*nou;
/*
void rot(nod *asd, int a)
{
r=asd->t;
if(r->st==asd)
if(a)
r->st=r->st->st;
else
r->st=r->st->dr;
else
if(a)
r->dr=r->dr->st;
else
r->dr=r->dr->dr;
}*/
void inserare(int a)
{
if(rad)
{
r = rad;
p=NULL;
while(r)
{
// r->h++;
p=r;
if(a>r->x)
r=r->dr;
else
r=r->st;
}
nou = new nod;
nou->x = a;
nou->st = NULL;
nou->dr = NULL;
// nou->t=p;
if(p->x>a)
p->st=nou;
else
p->dr=nou;
r=nou;
}
else
{
rad = new nod;
// rad->t=NULL;
rad->x=a;
rad->st=NULL;
rad->dr=NULL;
}
// while(r)
// {
//
// }
}
void afisare(nod *asd)
{
if(asd)
{
afisare(asd->st);
printf("%d ", asd->x);
afisare(asd->dr);
}
}
int main()
{
int i,q;
freopen("algsort.in", "r", stdin);
freopen("algsort.out", "w", stdout);
scanf("%d", &n);
for(i=0;i<n;i++)
{
scanf("%d", &q);
inserare(q);
}
afisare(rad);
return 0;
}