#include <stdio.h>
#include <stdlib.h>
struct node{
int Data;
int colour; //0 for red and 1 for black
struct node* Parent;
struct node* rightChild;
struct node* leftChild;};
struct node* Root;
struct node* insert(int value){
struct node
* p
= (struct node
*)malloc(sizeof(struct node
));
p->Data = value;
p->colour = 0;
p->rightChild = NULL;
p->leftChild = NULL;
p->Parent = NULL;
struct node
* Tracer
= (struct node
*)malloc(sizeof(struct node
)); Tracer = Root;
struct node
* Y
= (struct node
*)malloc(sizeof(struct node
));
if (Root==NULL){
p->Parent = NULL;
Root = p;
}
else{
while(Tracer!=NULL){
Y = Tracer;
if (Tracer->Data >= p->Data)
Tracer = Tracer->leftChild;
else
Tracer = Tracer->rightChild;}
p->Parent = Y;
if (Y->Data >= p->Data)
Y->leftChild = p;
else
Y->rightChild = p;
}
return p;
}
void leftrotate(struct node*X){
struct node* P = X->Parent;
struct node* C = X->rightChild;
C->Parent = P;
if (P==NULL)
Root=C;
else{
if( P->leftChild ==X)
P->leftChild = C;
else
P->rightChild = C;
}
X->rightChild = C->leftChild;
if (C->leftChild!=NULL){
C->leftChild->Parent = X;
}
X->Parent = C;
C->leftChild = X;
}
void rightrotate(struct node*X){
struct node* P= X->Parent;
struct node* C= X->leftChild;
C->Parent = P;
if (P==NULL)
Root =C;
else{
if (P->leftChild ==X)
P->leftChild = C;
else
P->rightChild = C;}
X->leftChild = C->rightChild;
if (C->rightChild != NULL){
C->rightChild->Parent = X;
}
X->Parent = C;
C->rightChild = X;
}
void Fixup(struct node*X){
struct node* P;// = (struct node*)malloc(sizeof(struct node));
P = X->Parent;
while(P!=NULL && P->colour==0 && X->colour==0){
if(P->Parent==NULL)
break;
else if (P->Parent->leftChild ==P){
//if(P->Parent->rightChild==NULL)
if(P->Parent->rightChild!=NULL && P->Parent->rightChild->colour ==0){
P->colour = 1;
P->Parent->colour = 0;
P->Parent->rightChild->colour = 1;
X = P->Parent;
//P = X->Parent;
}
else{
if (P->rightChild ==X){
X = P;
leftrotate(X);
P = X->Parent;
}
//if( P->leftChild==X){
P->colour = 1;
P->Parent->colour = 0;
rightrotate(P->Parent);
}
}
else{
if(P->Parent->leftChild!=NULL && P->Parent->leftChild->colour ==0){
P->colour = 1;
P->Parent->colour = 0;
P->Parent->leftChild->colour = 1;
X = P->Parent;
P = X->Parent;
}
else{
if (P->leftChild ==X){
X = P;
rightrotate(X);
P = X->Parent;
}
//if( P->rightChild==X){
P->colour = 1;
P->Parent->colour = 0;
leftrotate(P->Parent);
}
}
}
Root->colour = 1;
}
void Visit(struct node* X){
if (X!=NULL){
Visit(X->leftChild);
if (X->colour ==0){
if (X->Parent!= NULL)
printf("%d\t R\t %d \n",X
->Data
, X
->Parent
->Data
); else
printf("%d\t R\t NIL \n",X
->Data
); } else{
if (X->Parent!=NULL)
printf("%d\t B\t %d \n",X
->Data
, X
->Parent
->Data
); else
printf("%d\t B\t NIL \n",X
->Data
); }
Visit(X->rightChild);
}
}
int main(void) {
// Building red black tree
int n;
int index;
int temp;
Root
= (struct node
*)malloc(sizeof(struct node
)); struct node
* X
= (struct node
*)malloc(sizeof(struct node
)); Root = NULL;
for (index=1;index<=n;index++){
X = insert(temp);
Fixup(X);
}
if (Root==NULL){
}
else
Visit(Root);
return 0;
}