You are on page 1of 6

#include<iostream.

h>
#include<conio.h>
#define RED 'R'
#define BLACK 'B'
struct node
{
struct node *left;
struct node *right;
struct node *parent;
int data;
char color;
}root;
void llrotate(struct node *v)
{
struct node *p=v->parent;
struct node *g=p->parent;
struct node *gp=g->parent;
g->left=p->right;
g->parent=p;
p->right=g;
if(gp)
{
if(gp->left==gp)
{
gp->left=p;
p->parent=gp;
}
else
{
gp->right=p;
p->parent=gp;
}
else
{
root=p;
}
}
void rrrotate(struct node *v)
{
struct node *p=v->parent;
struct node *g=p->parent;
struct node *gp=g->parent;
g->right=p->left;
g->parent=p;
p->left=g;
if(gp)
{
if(gp->left==gp)
{
gp->left=p;
p->parent=gp;
}
else
{
gp->right=p;
p->parent=gp;
}
else
{
root=p;
}
}
void lrrotate(struct node *v)
{
struct node *p=v->parent;
struct node *g=p->parent;
struct node *gp=g->parent;
p->right=v->left;
g->left=v->right;
g->parent=v;
p->parent=v;
v->left=p;
v->right=g;
if(gp)
{
if(gp->left==gp)
{
gp->left=v;
v->parent=gp;
}
else
{
gp->right=v;
v->parent=gp;
}
else
{
root=p;
}
}

void rlrotate(struct node *v)


{
struct node *p=v->parent;
struct node *g=p->parent;
struct node *gp=g->parent;
p->left=v->right;
g->right=v->left;
g->parent=v;
p->parent=v;
v->left=g;
v->right=p;
if(gp)
{
if(gp->left==gp)
{
gp->left=v;
v->parent=gp;
}
else
{
gp->right=v;
v->parent=gp;
}
else
{
root=p;
}
}
void insert(int d)
{
struct node *h=root;
struct node *hp=h;
while(h)
{
hp=h;
if((d<h->data)
h=h->left;
else
h=h->right;
}
struct node *r=new node;
r->data=d;
r->color=RED;
r->left=NULL;
r->right=NULL;
r->parent=NULL;
if(root==NULL)
{root=r; return ;}
else
if (d<hp->data)
{
hp->left=r;
r->parent=hp;
}
else
{
hp->right=r;
r->parent=hp;
}
struct node *v=r;
struct node *p=hp;
changetreecolor(p,r);
display(root);
return;
}
struct node * sibling(struct node *n)
{
if (n == n->parent->left)
return n->parent->right;
else
return n->parent->left;
}
void changetreecolor(strcut node *p, struct node *v)
{
if(p->color==BLACK)
return;
elseif(p->color=RED && p==root)
{ p->color=BLACK;
return;
}
else
change1a(p,v);
return;
}
void change1a(struct node *p,struct node *v)
{
struct node *u=sibling(v);
strcut node *g=p->parent;
if (u->color==RED && u!=NULL)
{
while(u->color==RED && u!=NULL)
{
g->color=RED;
p->color=u->color=BLACK;
if(g->parent->color==BLACK || g->parent==NULL)
return;
else
{ v=p;
p=g;
g=g->parent;
u=sibling(v);
}
}
change1b(p,v);
}
else
change1b(p,v);
}
void change1b(struct node *p,struct node *v)
{
if (u->color==BLACK && p->left==v && g->left==p)
{
llrotation(v);
char t;
t=g->color;
g->color=p->color;
p->color=g;
}
else if (u->color==BLACK && p->right==v && g->right==p)
{
rrrotation(v);
char t;
t=g->color;
g->color=p->color;
p->color=g;
}
if (u->color==BLACK && p->right==v && g->left==p)
{
lrrotation(v);
char t;
t=g->color;
g->color=v->color;
v->color=g;
}
else if (u->color==BLACK && p->left==v && g->right==p)
{
rlrotation(v);
char t;
t=g->color;
g->color=v->color;
v->color=g;
}
return;
}
display(struct node *p)
{
if(p)
{
display(p->left);
cout<<p->data<<"\n";
display(p->right);
}
}
void main()
{
struct node rbt;
rbt=new node;
int i,d;
for(i=0; i<6;i++)
{
cout<<"enter node value ";
cin>>d;
rbt.insert(d);
}
}

You might also like