Cod sursa(job #312860)

Utilizator Bogdan_tmmTirca Bogdan Bogdan_tmm Data 7 mai 2009 09:58:09
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#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("algsort.in","r",stdin);
	freopen("algsort.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;
}