fork(1) download
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct tnode
{
	int data; char color;
	struct tnode* parent; 
	struct tnode* rightChild;
	struct tnode* leftChild;
};
struct tree
{
	struct tnode* root;
	struct tnode* nil;
};


struct tnode* createnode(int data)
{
	struct tnode *newnode;
	newnode=(struct tnode*)malloc(sizeof(struct tnode));
	newnode->data=data;
	newnode->color='R';
	newnode->leftChild=NULL;
	newnode->parent=NULL;
	newnode->rightChild=NULL;
	return newnode;
}

void leftrotate(struct tree*T, struct tnode*x)
{
	struct tnode * y;
	y=(struct tnode*)malloc(sizeof(struct tnode));
	y=x->rightChild;
	x->rightChild=y->leftChild;
	if(y->leftChild!=T->nil)
		y->leftChild->parent=x;
	else if (x==x->parent->leftChild)
		x->parent->leftChild=y;
	else 
		x->parent->rightChild=y;
	
	y->leftChild=x;
	x->parent=y; 
}
void rightrotate(struct tree*T, struct tnode*x)
{
	struct tnode * y;
	y=(struct tnode*)malloc(sizeof(struct tnode));
	y=x->leftChild;
	x->leftChild=y->rightChild;
	if(y->rightChild!=T->nil)
		y->rightChild->parent=x;
	else if (x==x->parent->rightChild)
		x->parent->rightChild=y;
	else 
		x->parent->leftChild=y;
	
	y->rightChild=x;
	x->parent=y; 
}
void rbinsertfixup(struct tree*T, struct tnode*z)
{
	struct tnode*y;
	y=(struct tnode*)malloc(sizeof(struct tnode));
	
	while(z->parent->color=='R')
	{
		if(z->parent==z->parent->parent->leftChild)
		{
			y=z->parent->parent->rightChild;
			if(y->color=='R')
			{
				z->parent->color='B';
				y->color='B';
				z->parent->parent->color='R';
				z=z->parent->parent;
			}
			else if(z==z->parent->rightChild)
			{
			  z=z->parent;
			  leftrotate(T,z);
			}
			z->parent->color='B';
			z->parent->parent->color='R';
			rightrotate(T,z->parent->parent);
		}
		else
		{
			y=z->parent->parent->leftChild;
			if(y->color=='R')
			{
				z->parent->color='B';
				y->color='B';
				z->parent->parent->color='R';
				z=z->parent->parent;
			}
			else if(z==z->parent->leftChild)
			{
			  z=z->parent;
			  rightrotate(T,z);
			}
			z->parent->color='B';
			z->parent->parent->color='R';
			leftrotate(T,z->parent->parent);	
		}
	}
	T->root->color='B';
}
void insertion(struct tree *T, struct tnode *z)
{
	
	struct tnode * y;
	y=(struct tnode*)malloc(sizeof(struct tnode));
	struct tnode * x;
	x=(struct tnode*)malloc(sizeof(struct tnode));
	y=T->nil; 
	x=T->root;
	while(x!=T->nil)
	{
		y=x;
		if(z->data < x->data)
		x=x->leftChild;
		else 
		x=x->rightChild;
	}
	z->parent=y;
	if(y==T->nil)
	T->root=z;
	else if (z->data< y->data)
	y->leftChild=z;
	else
	y->rightChild=z;
	
	z->leftChild=T->nil;
	z->rightChild=T->nil;
	z->color='R';
	rbinsertfixup(T,z);
	
}

void visit(struct tnode* x)
{
	if(x!=NULL)
	{
		visit(x->leftChild);
		if(x->parent!=NULL)
		printf("%d %c %d \n", x->data, x->color, x->parent->data);
		else
		printf("%d %c NIL \n", x->data, x->color);
			
		
	}
}
void pre_fix_tour (struct tree* T)
{
	if(T->root==NULL)
	printf("empty tree");
	else
	visit(T->root);
	
}


int main()
{
	int a[]={5,4,1,6,10,11};
	int i;
	struct tnode* node;
	struct tree *treen;
	treen=(struct tree*)malloc(sizeof(struct tree));
	treen->nil=NULL;
	treen->root=NULL;
	for(i=0;i<6;i++)
	{
		node=createnode(a[i]);
		insertion(treen, node);
	}
	
pre_fix_tour(treen);
	
	return 0;
	
	
	
}
Runtime error #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
Standard output is empty