//菜鸟制作,大神勿喷
//VC6.0通过
#include <iostream>
using namespace std;
const int R = 1;
const int B = 0;
//红黑树结构体
typedef struct RB_TREE
{
bool color;
int data;
RB_TREE *left, *right;
RB_TREE *parent;
}*RB_T;
//左旋操作
void left_rotate(RB_T &T, RB_T x)
{
RB_T y = x->right;
x->right = y->left;
if(y->left)
y->left->parent = x;
y->parent = x->parent;
if(x->parent == NULL)
T = y;
else
{
if(x == x->parent->left)
x->parent->left = y;
else x->parent->right = y;
}
y->left = x;
x->parent = y;
}
//右旋操作
void right_rotate(RB_T &T, RB_T x)
{
RB_T y = x->left;
x->left = y->right;
if(y->right)
y->right->parent = x;
y->parent = x->parent;
if(x->parent == NULL)
T = y;
else
{
if(x == x->parent->right)
x->parent->right = y;
else x->parent->left = y;
}
y->right = x;
x->parent = y;
}
//求最大值
RB_T minimum(RB_T p)
{
while(p->left)
p = p->left;
return p;
}
//求节点的后继
RB_T successor(RB_T p)
{
if(p->right)
return minimum(p);
RB_T y = p->parent;
while(y != NULL && p == p->right)
{
p = y;
y = y->parent;
}
return y;
}
///对违反红黑性质的插入操作进行修复
void RB_INSERT_FIXUP(RB_T &T,RB_T z)
{
while(z->parent && z->parent->color == R && z->parent->parent)
{
//此处必须判断一下z->parent是否为空,因为经过循环z->parent不确定是否会存在,必须注意这一点
RB_T y = NULL;
if(z->parent == z->parent->parent->left)
{
y = z->parent->parent->right;
if(y && y->color == R)
{
//情况一:唯一可能进入下次while的情况
y->color = B;
z->parent->color = B;
z->parent->parent->color = R;
z = z->parent->parent;
}
else
{
if(z == z->parent->right)//若有情况二,则将情况二 转化为情况三
{
z = z->parent;//此处必须这么做,否则将会出现与情况三不相符的现象
left_rotate(T, z);
}
//直接进入情况三:
z->parent->color = B;
z->parent->parent->color = R;
right_rotate(T, z->parent->parent);
}
}
else //z->parent == z->parent->parent->right
{
y = z->parent->parent->left;
if(y && y->color == R)//情况一
{
y->color = B;
z->parent->color = B;
z = z->parent->parent;
}
else
{
if(z == z->parent->left)//若有情况二,则将情况二 转化为情况三
{
z = z->parent;//此处必须这么做,否则将会出现与情况三不相符的现象
right_rotate(T, z);
}
//直接进入情况三:
z->parent->color = B;
z->parent->parent->color = R;
left_rotate(T, z->parent->parent);
}
}
}
T->color = B;
}
//插入操作
void RB_T_INSERT(RB_T &T, RB_T z)
{
RB_T y = NULL;//插入结点的双亲结点
RB_T x = T;
while(x)
{
y = x;
if(x->data > z->data)
x = x->left;
else x = x->right;
}
z->parent = y;
if(y == NULL)
{
T = z;
}
else
{
if(z->data < y->data)
y->left = z;
else y->right = z;
RB_INSERT_FIXUP(T, z);
}
}
//删除操作的修复
void RB_T_DELETE_FIXUP(RB_T &T, RB_T x)
{
while(x != T && x->color == B)
{
RB_T w;
if(x == x->parent->left)
{
w = x->parent->right;
if(w->color == R)
{
w->color = B;
x->parent->color = R;
left_rotate(T, x->parent);
w = x->parent->right;
}
if(w->left->color == B && w->right->color == B)
{
w->color = R;
x = x->parent;
}
else
{
if(w->right->color == B)
{
w->left->color = B;
w->color = R;
right_rotate(T, w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = B;
w->right->color = B;
left_rotate(T, x->parent);
x = T;
}
}
else
{//x == x->parent->right
w = x->parent->left;
if(w->color == R)
{
w->color = B;
x->parent->color = R;
right_rotate(T, x->parent);
w = x->parent->left;
}
if(w->left->color == B && w->right->color == B)
{
w->color = R;
x = x->parent;
}
else
{
if(w->left->color == B)
{
w->right->color = B;
w->color = R;
left_rotate(T, w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = B;
w->left->color = B;
right_rotate(T, x->parent);
x = T;
}
}
}
}
//删除操作
void RB_T_DELETE(RB_T &T, RB_T z)
{
RB_T x, y;//y为删除结点,x为删除结点的孩子(左孩子或是右孩子)
if(z->left == NULL || z->right == NULL)
y = z;
else y = successor(z);
if(y->left)
x = y->left;
else x = y->right;
if(x)
x->parent = y->parent;
if(y->parent == NULL)
T = x;
else
{
if(y == y->parent->left)
y->parent->left = x;
else y->parent->right = x;
}
if(y != z)
{
int t = y->data;
y->data = z->data;
z->data = t;
}
if(y->color == B)
RB_T_DELETE_FIXUP(T, x);
}
//中序遍历
void inorder(RB_T T)
{
if(T)
{
inorder(T->left);
cout<<"data: "<<T->data<<" color: "<<T->color<<endl;
inorder(T->right);
}
}
//查找函数
RB_T search(RB_T T,int key)
{
while(T->data != key)//为了简便起见,我们假设该元素一定会在树中
{
if(T->data > key)
T = T->left;
else T = T->right;
}
return T;
}
int main()
{
int n;
cout<<"please the element of RB_T that you want enter:"<<endl;
while(cin >> n)
{
RB_T T = NULL;
cout<<"please enter the element in order:"<<endl;
for(int i = 0; i < n; i++)
{
RB_T s = new RB_TREE;//开辟插入节点空间
s->left = s->right = s->parent = NULL;
s->color = R;//插入结点定义为红色
cin>>s->data;
RB_T_INSERT(T, s);
}
//trave the RB_TREE
cout<<endl<<"START!!"<<endl;
cout<<"the result of traving:"<<endl;
inorder(T);
cout<<endl<<"END!!"<<endl;
int ele_delete;
cout<<"please enter the element thar you want to delete:"<<endl;
cin>>ele_delete;
RB_T p = search(T, ele_delete);
RB_T_DELETE(T, p);
cout<<endl<<"START!!!"<<endl;
cout<<"after element deleted:"<<endl;
inorder(T);
cout<<endl<<"END!!"<<endl;
cout<<"please the element of RB_T that you want enter:"<<endl;
}
return 0;
}