Pagini recente » Cod sursa (job #1567732) | Cod sursa (job #2410432) | Cod sursa (job #1575303) | Cod sursa (job #1110436) | Cod sursa (job #312859)
Cod sursa(job #312859)
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
struct nod {int Key;nod *Back,*Left,*Right;};
int n,i,k;
nod *Rad;
nod *Min(nod *x)
{
while(x->Left)
x=x->Left;
return x;
}
nod *Max(nod *x)
{
while(x->Right)
x=x->Right;
return x;
}
nod *Search(nod *x,int k)
{
while(x&&x->Key!=k)
if(x->Key<k)
x=x->Right;
else
x=x->Left;
return x;
}
nod *Next(nod *x)
{
if(x->Right)
return Min(x->Right);
nod *y=x->Back;
while(y&&x==y->Right)
{
x=y;
y=x->Back;
}
return y;
}
nod *Back(nod *x)
{
if(x->Left)
return Max(x->Left);
nod *y=x->Back;
while(y&&x==y->Left)
{
x=y;
y=x->Back;
}
return y;
}
void Insert(nod *&Rad,nod *x)
{
nod *y=Rad,*rad=0;
while(y)
{
rad=y;
if(x->Key<y->Key)
y=y->Left;
else
y=y->Right;
}
x->Back=rad;
if(rad==0)
Rad=x;
else
if(x->Key<rad->Key)
rad->Left=x;
else
rad->Right=x;
}
void Delete(nod *&Rad,nod *x)
{
nod *y,*z=0;
if(x->Left==0||x->Right==0)
y=x;
else
y=Next(x);
if(y->Left!=0)
z=y->Left;
else
z=y->Right;
if(z)
z->Back=y->Back;
if(y->Back==0)
Rad=z;
else
if(y==y->Back->Left)
y->Back->Left=z;
else
y->Back->Right=z;
if(y!=x)
x->Key=y->Key;
delete y;
}
void List(nod *x)
{
if(x)
{
List(x->Left);
printf("%d ",x->Key);
List(x->Right);
}
}
int main()
{
freopen("bintree.in","r",stdin);
freopen("bintree.out","w",stdout);
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
nod *x=new nod;x->Back=x->Left=x->Right=0;
x->Key=k;
Insert(Rad,x);
}
List(Rad);
return 0;
}