main函数稍微有点问题,但是没精力改了…..先这样吧太累了。
#include<iostream>
#include<string>
using namespace std;
struct rb
{
rb*p;
rb*left;
rb*right;
int key;
char color;
};
void right_rotate(rb*x)
{
rb*tem = x;
rb*tem1 = x->left;
rb*tem2 = x->p;
rb*tem3 = x->left->right;
if (x == x->p->left)
{
tem2->left = tem1;
tem1->p = tem2;
tem1->right = tem;
tem->p = tem1;
tem->left = tem3;
tem3->p = tem;
}
else
{
tem2->right = tem1;
tem1->p = tem2;
tem1->right = tem;
tem->p = tem1;
tem->left = tem3;
tem3->p = tem;
}
}
void left_rotate(rb*x)
{
rb*tem = x;
rb*tem1 = x->right;
rb*tem2 = x->p;
rb*tem3 = x->right->left;
if (x == x->p->right)
{
tem2->right = tem1;
tem1->p = tem2;
tem1->left = tem;
tem->p = tem1;
tem->right = tem3;
tem3->p = tem;
}
else
{
tem2->left = tem1;
tem1->p = tem2;
tem1->left = tem;
tem->p = tem1;
tem->right = tem3;
tem3->p = tem;
}
}
void rb_transplant(rb*a, rb*b)
{
rb*tem1 = a;
rb*tem2 = b;
rb*tem3 = a->p;
if (tem1 == tem3->left)
{
tem3->left = tem2;
}
else
{
tem3->right = tem2;
}
tem2->p = tem3;
}
void rb_insert_fixup(rb*t, rb*a)
{
while (a->p->color == 'r')
{
if (a->p->p->left == a->p)
{
if (a->p->p->right->color == 'r')
{
a->p->color = 'b';
a->p->p->right->color = 'b';
a->p->p->color = 'r';
a = a->p->p;
}
else
{
if (a == a->p->right)
{
a = a->p;
left_rotate(a);
}
a->p->p->color = 'r';
a->p->color = 'b';
right_rotate(a->p->p);
}
}
else
{
if (a->p->p->left->color == 'r')
{
a->p->color = 'b';
a->p->p->left->color = 'b';
a->p->p->color = 'r';
a = a->p->p;
}
else
{
if (a == a->p->left)
{
a = a->p;
right_rotate(a);
}
a->p->p->color = 'r';
a->p->color = 'b';
left_rotate(a->p->p);
}
}
}
t->color = 'b';
}
void rb_insert(rb*t, rb*z)
{
rb*head = t;
rb*headp = t->p;
while (head != t->p)
{
if (head->key<z->key)
{
head = head->right;
}
else
{
head = head->right;
}
headp = head->p;
}
z->p = headp;
if (headp->key < z->key)
{
headp->right = z;
}
else
{
headp->left = z;
}
z->color = 'r';
z->right = t->p;
z->left = t->p;
rb_insert_fixup(t, z);
}
void rb_delete_fixup(rb*t, rb*x)
{
while (x != t&&x->color == 'b')
{
if (x->p->left == x)
{
rb*xbro = x->p->right;
if (xbro->color == 'r')
{
xbro->color = 'b';
x->p->color = 'r';
x->color = 'b';
left_rotate(x->p);
rb*xbro = x->p->right;
}
if (xbro->left->color == 'b'&&xbro->right->color == 'b')
{
xbro->color = 'r';
x = x->p;
continue;
}
if (xbro->color == 'b')
{
if (xbro->left->color == 'r'&&xbro->right->color == 'b')
{
xbro->left->color = 'r';
xbro->color = 'b';
right_rotate(xbro);
xbro = x->p->right;
}
if (xbro->right->color = 'r')
{
xbro->color = x->p->color;
x->p->color = 'b';
xbro->right->color = 'b';
left_rotate(x->p);
x = t;
}
}
}
else
{
rb*xbro = x->p->left;
if (xbro->color == 'r')
{
xbro->color = 'b';
x->p->color = 'r';
right_rotate(x->p);
xbro = x->p->left;
}
if (xbro->left->color == 'b'&&xbro->right->color == 'b')
{
xbro->color = 'r';
x = x->p;
continue;
}
if (xbro->color = 'b')
{
if (xbro->left->color == 'b'&&xbro->right->color == 'r')
{
xbro->right->color = 'b';
xbro->color = 'r';
left_rotate(xbro);
xbro = x->p->left;
}
if (xbro->left->color == 'r')
{
xbro->color = x->p->color;
x->p->color = 'b';
xbro->left->color = 'b';
right_rotate(x->p);
x = t;
}
}
}
}
}
rb* minimum(rb*t, rb*z)
{
rb*mini = z;
rb*minip = mini->p;
while (mini != t->p)
{
minip = mini;
mini = mini->left;
}
return minip;
}
void rb_delete(rb*t, rb*z)
{
rb*tem = z;
rb*fix;
char uscolor = z->color;
if (z->left == t->p)
{
fix = z->right;
rb_transplant(z, z->right);
delete tem;
}
else if(z->right==t->p)
{
fix = z->left;
rb_transplant(z, z->left);
delete tem;
}
else
{
tem = minimum(t, z->right);
uscolor = tem->color;
fix = tem->right;
if (tem->p == z)
{
fix->p = tem;
}
else
{
rb_transplant(tem, tem->right);
tem->right = z->right;
tem->right->p = tem;
}
rb_transplant(z, tem);
tem->left = z->left;
tem->left->p = tem;
tem->color = z->color;
delete z;
}
if (uscolor == 'b')
{
rb_delete_fixup(t, fix);
}
}
void rb_print(rb*t,rb*i)
{
if (t != i)
{
rb_print(t->left, i);
cout << t->color << t->key << endl;
rb_print(t->right, i);
}
}
int main()
{
int n;
rb*tail = new rb;
tail->left = tail;
tail->color = 'b';
while (cin.good())
{
cout << "1插入2删除3打印" << endl;
cin >> n;
switch (n)
{
case(1) :
{
cout << "输入你想插入的是多少" << endl;
rb*tem = new rb;
cin >> tem->key;
if (tail->left == tail)
{
tem->p = tail;
tail->left = tem;
tem->color = 'b';
}
else
{
rb_insert(tail->left, tem);
}
break;
}
case(2) :
{
cout << "你想删除那个节点" << endl;
int dele;
cin >> dele;
rb*head = tail->left;
while (head->key == dele)
{
if (head->key > dele)
{
head = head->left;
}
else if (head->key < dele)
{
head = head->right;
}
else
{
rb_delete(tail->left, head);
}
}
break;
}
case(3) :
{
rb_print(tail->left,tail);
break;
}
}
}
return 0;
}