首页 > 代码库 > 红黑树

红黑树

照着算导抄了一下!(删除未验证)

#include<iostream>
//#define BLACK 0
//#define RED 1
enum Color{
    BLACK, RED
}; 
struct Node{
    Color color;// 1 == Red ,0 == Black
    int key;
  //int num;   保存key的个数 
    Node *left, *right, *p;
};
struct RBTree{
    Node *nil, *root;
}T;
void Init(RBTree &T)
{
    T.nil = new Node;
    T.nil->color = BLACK;
    T.nil->left = T.nil->right = NULL;
    T.root = T.nil;
}
void Left_Rotate(RBTree &T, Node *x)
{
    Node *y = x->right;
    x->right = y->left;
    if(y->left != T.nil) y->left->p = x;
    y->p = x->p;
    if(x->p == T.nil) T.root = y;
    else if(x == x->p->left) x->p->left = y;
    else x->p->right = y;
    y->left = x;
    x->p = y;
}
void Right_Rotate(RBTree &T, Node *x)
{
    Node *y = x->left;
    x->left = y->right;
    if(y->right != T.nil) y->right->p = x;
    y->p = x->p;
    if(y->p==T.nil) T.root = y;
    else if(x == x->p->right) x->p->right = y;
    else x->p->left = y;
    y->right = x;
    x->p = y;
}
void RB_Insert_Fixup(RBTree &T, Node *z)
{
    while( z->p->color == RED ) // z->p->color==1
    {
        Node *y;
        if( z->p == z->p->p->left)
        {
            y = z->p->p->right;
            if( y->color== RED )//y->color == 1
            {
                z->p->color = BLACK;
                y->color = BLACK;
                z->p->p->color = RED;
                z = z->p->p;
            }
            else 
            {
                if(z == z->p->right)
                {
                    z = z->p;
                    Left_Rotate(T, z);
                }
                z->p->color = BLACK;
                z->p->p->color = RED;
                Right_Rotate(T, z->p->p);
            }
        }
        else
        {
            y = z->p->p->left;
            if( y->color == RED )//y->color == 1
            {
                z->p->color = BLACK;
                y->color = BLACK;
                z->p->p->color = RED;
                z = z->p->p;
            }
            else 
            {
                if(z == z->p->left)
                {
                    z = z->p;
                    Right_Rotate(T, z);
                }
                z->p->color = BLACK;
                z->p->p->color = RED;
                Left_Rotate(T, z->p->p);
            }
        }
    }
    T.root->color = BLACK;
}
void RB_Insert(RBTree &T, Node *z)
{
    Node *y = T.nil;
    Node *x = T.root;
    while( x != T.nil )
    {
        y = x;
        if( z->key < x->key) x = x->left;
        else x = x->right;
    }
    z->p = y;
    if( y == T.nil) T.root = z;
    else if( z->key < y->key) y->left = z;
    else y->right = z;
    z->left = z->right = T.nil;
    z->color = RED;
    RB_Insert_Fixup(T,z);
}
void RB_Print(RBTree T, Node *node)
{
    if(node != T.nil)
    {
        RB_Print(T, node->left );
        std::cout << node->key <<  ;
        if( node->color == BLACK ) std::cout << "黑\n";
        else std::cout << "红\n";
        RB_Print(T, node->right );
    }
}

void RB_Transplant(RBTree &T, Node *u, Node *v)
{
    if(u->p == T.nil)  T.root = v;
    else if( u == u->p->left) u->p->left = v;
    else u->p->right = v;
    v->p = u->p;
}

Node* RB_Tree_Minimum(RBTree &T, Node *x)
{
    while(x->left != T.nil) x = x->left;
    return x;
}

void RB_Delete_Fixup(RBTree &T, Node *x)
{
    while( x != T.root && x->color == BLACK )
    {
        if( x == x->p->left )
        {
            Node *w = x->p->right;
            if( w->color == RED )
            {
                w->color = BLACK;
                x->p->color = RED;
                Left_Rotate(T, x->p);
                w = x->p->right;
            }
            if( w->left->color == BLACK && w->right.color == BLACK )
            {
                w->color = RED;
                x = x->p;
            }
            else if( w->right->color == BLACK )
            {
                W->left->color = BLACK;
                w->color = RED;
                Right_Rotate(T, w);
                w = x->p->right;
            }
            w->color = x->p->right;
            x->p->color = BLACK;
            w->right->color = BLACK;
            Left_Rotate(T, x->p);
            x = T.root;
        }
        else
        {
            Node *w = x->p->left;
            if( w->color == RED )
            {
                w->color = BLACK;
                x->p->color = RED;
                Right_Rotate(T, x->p);
                w = x->p->left;
            }
            if( w->right->color == BLACK && w->left.color == BLACK )
            {
                w->color = RED;
                x = x->p;
            }
            else if( w->left->color == BLACK )
            {
                W->right->color = BLACK;
                w->color = RED;
                Left_Rotate(T, w);
                w = x->p->left;
            }
            w->color = x->p->left;
            x->p->color = BLACK;
            w->left->color = BLACK;
            Right_Rotate(T, x->p);
            x = T.root;
        }
    }
}

void RB_Delete(RBTree &T, Node *z)
{
    Node *y = z;
    Node *x;
    Color y_original_color = y->color;
    if( z->left == T.nil)
    {
        x = z->right;
        RB_Transplant(T, z, z->right);
    }
    else if( z->right == T.nil)
    {
        x = z->left;
        RB_Transplant(T, z, z->left);
    }
    else
    {
        y = RB_Tree_Minimum(T, z->right);
        y_original_color = y->color;
        x = y->right;
        if(y->p == z)
        {
            x->p == y;
        }
        else 
        {
            RB_Transplant(T, y, y->right);
            y->right = z->right;
            y->right->p = y;
        }
        
//        if(y->p != z)
//        {    
//            RB_Transplant(T, y, y->right);
//            y->right = z->right;
//            y->right->p = y;
//        }

        RB_Transplant(T, z, y);
        y->left = z->left;
        y->left->p = y;
        y->color = z->color;
    }
    if( y_original_color == BLACK )
    {
        RB_Delete_Fixup(T, x);
    }
}
int main (void)
{
    Init(T);
    int a;
    while(std::cin>>a, a)
    {
        Node *z = new Node;
        z->key = a;
        RB_Insert(T, z);
    }
    RB_Print(T, T.root);
    return 0;
}
/*
1
2
3
4
5
6
7
8
9
10
*/ 

 

红黑树