红黑树的C++实现

参考文献:红黑树的删除
     红黑树的插入

#include <bits/stdc++.h>

using namespace std;
struct node
{
    int value, color;
    node * L, * R, *parent;
    node(){
        L = R = parent = nullptr;
        color = 1;//1:red 0:black
    }

};

void RotateL(node * &root, node *p)
{
    node * A = p;
    node * B = A->R;
    A->R = B->L;
    if(B->L) B->L->parent = A;
    if(A->parent){
        if(A == A->parent->L) A->parent->L = B;
        else A->parent->R = B;
    }
    B->parent = A->parent;
    B->L = A;
    A->parent = B;
    if(!B->parent) root = B;
}
void RotateR(node * &root, node *p)
{
    node * A = p;
    node * B = A->L;
    A->L = B->R;
    if(B->R) B->R->parent = A;
    if(A->parent){
        if(A == A->parent->L) A->parent->L = B;
        else A->parent->R = B;
    }
    B->parent = A->parent;
    B->R = A;
    A->parent = B;
    if(!B->parent) root = B;
}
void Fix_node(node * &root, node * p, node * parent)
{
    node * brother = nullptr;
    while((p == nullptr || !p->color) && p != root){
        if(p == parent->L){
            brother = parent->R;
            if(brother->color){
                brother->color = 0;
                parent->color = 1;
                RotateL(root, parent);
                brother = parent->R;
            }

            if((!brother->L || !brother->L->color) && (!brother->R || !brother->R->color)){
                if(parent->color){
                    parent->color = 0;
                    brother->color = 1;
                    p = parent;
                    break;
                }
                else{
                    brother->color = 1;
                    p = parent;
                    parent = p->parent;
                }
            }
            else{
                if(brother->L && brother->L->color){
                    brother->L->color = parent->color;
                    parent->color = 0;
                    RotateR(root, brother);
                    RotateL(root, parent);

                }
                else if(brother->R && brother->R->color){
                    brother->color = parent->color;
                    parent->color = 0;
                    brother->R->color = 0;
                    RotateL(root, parent);
                }
                break;
            }
        }
        else{
            brother = parent->L;
            if(brother->color){
                brother->color = 0;
                parent->color = 1;
                RotateR(root, parent);
                brother = parent->L;
            }

            if((!brother->L || !brother->L->color) && (!brother->R || !brother->R->color)){
                if(parent->color){
                    parent->color = 0;
                    brother->color = 1;
                    p = parent;
                    break;
                }
                else{
                    brother->color = 1;
                    p = parent;
                    parent = p->parent;
                }
            }
            else{
                if(brother->R && brother->R->color){
                    brother->R->color = parent->color;
                    parent->color = 0;
                    RotateL(root, brother);
                    RotateR(root, parent);

                }
                else if(brother->L && brother->L->color){
                    brother->color = parent->color;
                    parent->color = 0;
                    brother->L->color = 0;
                    RotateR(root, parent);
                }
                break;
            }
        }
    }
    if(p){
        p->color = 0;
    }
}
void Insert(node * &root, int key)
{
    if(root == nullptr){
        root = new node;
        root->value = key;
        root->color = 0;
        return;
    }
    node *p = root;
    node *parent = nullptr;
    while (p){
        if(key < p->value){
            parent = p;
            p = p->L;
        }
        else{
            parent = p;
            p = p->R;
        }
    }
    p = new node;
    p->value = key;
    if(key < parent->value){
        parent->L = p;
    }
    else{
        parent->R = p;
    }
    p->parent = parent;

    while(parent && parent->color && p->color){
        node * grandparent = parent->parent;
        if(parent == grandparent->L){
            node * uncle = grandparent->R;
            if(uncle && uncle->color){
                parent->color = uncle->color = 0;
                grandparent->color = 1;
                p = grandparent;
                parent = p->parent;
            }
            else{
                if(p == parent->R){
                    RotateL(root, parent);
                    swap(parent, p);
                }
                RotateR(root, grandparent);
                parent->color = 0;
                grandparent->color = 1;
                p = parent;
                parent = p->parent;
            }
        }
        else{
            node *uncle = grandparent->L;
            if(uncle && uncle->color){
                parent->color = uncle->color = 0;
                grandparent->color = 1;
                p = grandparent;
                parent = p->parent;
            }
            else{
                if(p == parent->L){
                    RotateR(root, parent);
                    swap(p, parent);
                }
                RotateL(root, grandparent);
                parent->color = 0;
                grandparent->color = 1;
                p = parent;
                parent = p->parent;
            }
        }
    }
    if(parent == nullptr) root = p;
    root->color = 0;
}
void Delete(node * &root, node * p)
{
    node * fixp = nullptr, * fixp_parent = nullptr;
    if(p->L != nullptr && p->R != nullptr){
        node * replace = p->R;
        while(replace->L) replace = replace->L;
        p->value = replace->value;
        Delete(root, replace);
    }
    else{
        if(p->parent == nullptr){
            if(p->L){
                node * delete_p = root;
                root = p->L;
                root->parent = nullptr;
                root->color = 0;
                delete delete_p;
            }
            else if(p->R){
                node * delete_p = root;
                root = p->R;
                root->parent = nullptr;
                root->color = 0;
                delete delete_p;
            }
            else{
                delete root;
                root = nullptr;
            }
        }
        else{
            node * child = p->L ? p->L : p->R;
            if(p == p->parent->L) p->parent->L = child;
            else p->parent->R = child;
            if(child) child->parent = p->parent;

            fixp = child;
            fixp_parent = p->parent;
            if(!p->color)
                Fix_node(root, fixp, fixp_parent);
            delete p;
        }
    }
}
int flag;
void check(node * p, int count)
{
    if(p == nullptr){
        if(flag == -1){
            flag = count;
        }
        else if(flag != count){
            printf("T.T\n");
        }
        return;
    }
    if(p->L){
        if(p->value < p->L->value) printf("T.T\n");
    }
    if(p->R){
        if(p->value > p->R->value) printf("T.T\n");
    }
    check(p->L, count+(!p->color));
    check(p->R, count+(!p->color));
}
int main()
{

    node * root = nullptr;
    int n ;
    scanf("%d", &n);
    for(int _i = 0; _i < n; _i++){
        int tmp, tp;
        scanf("%d %d", &tp, &tmp);
        if(tp){
            node * p = root;
            while(p){
                if(p->value == tmp) break;
                if(tmp < p->value) p = p->L;
                else p = p->R;
            }
            Delete(root, p);
        }
        else{
            Insert(root, tmp);
        }
        flag = -1;
        check(root, 0);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值