红黑树(RBTree) (Update 12.30 仅实现插入)

红黑树是一种自平衡二叉查找树,它在进行插入操作时能保持较好的平衡状态,确保了搜索效率。本文详细介绍了红黑树的插入过程,包括节点颜色规则、旋转操作以及插入修复等关键步骤。
摘要由CSDN通过智能技术生成
#include <bits/stdc++.h>
using namespace std;
struct Node;
typedef Node* RBNode;
struct Node{
    RBNode parent,lson,rson;
    int key;    bool RBcolor;
    Node():parent(nullptr),lson(nullptr),rson(nullptr),key(0),RBcolor(true){}
};
struct RBTree{
    RBNode root, nil;
    RBTree():root(){nil = new Node;   root = nil; }
};
inline RBNode tree_minimum(const RBTree &T,RBNode x){
    while(x != T.nil){
        x = x->lson;
    }
    return x;
}
inline RBNode tree_maxnimum(const RBTree &T,RBNode x){
    while(x != T.nil){
        x = x->rson;
    }
    return x;
}
inline void LeftRotate(RBTree &T, RBNode x){
    RBNode y = x -> rson;
    x -> rson = y -> lson;
    if(y->lson!=T.nil)    y->lson->parent = x;
    y -> parent = x -> parent;
    if(x->parent==T.nil)  T.root = y;
    else if(x == x->parent->lson)
        x->parent->lson = y;
    else x->parent->rson = y;
    y -> lson = x;
    x -> parent = y;
}
inline void RightRotate(RBTree &T, RBNode y){
    RBNode x = y -> lson;
    y -> lson = x -> rson;
    if(x -> rson !=T.nil) x->rson->parent = y;
    x -> parent = y -> parent;
    if(y->parent==T.nil)  T.root = x;
    else if(y->parent->lson == y)   y->parent->lson = x;
    else y->parent->rson = x;
    x -> rson = y;
    y -> parent = x;
}
inline void Insert_Fix(RBTree &T, RBNode z){
    RBNode parent,gparent;
    while( (parent = z->parent)!=T.nil && parent->RBcolor){
        gparent = parent->parent;
        if(parent == gparent->lson){
            RBNode y = gparent->rson;
            if(y!=T.nil && y->RBcolor){   // case 1:
                y->RBcolor = false;
                parent->RBcolor = false;
                gparent->RBcolor = true;
                z = gparent;
            }else if(z==parent->rson){  //  case 2: z字形
                LeftRotate(T,parent);
                swap(z,parent);
            }
            parent->RBcolor = false;    //  case 3: 直线型
            gparent->RBcolor = true;
            RightRotate(T,gparent);
        }else{  //另一种情况是对称的
            RBNode y = gparent->lson;
            if(y!=T.nil && y->RBcolor){
                y->RBcolor = false;
                parent->RBcolor = false;
                gparent->RBcolor = true;
                z = gparent;
            }else if(z==parent->lson){
                RightRotate(T,parent);
                swap(z,parent);
            }
            parent->RBcolor = false;
            gparent->RBcolor = true;
            LeftRotate(T,gparent);
        }
    }
    T.root->RBcolor = false;    //  修改根结点的颜色
}
inline void Insert(RBTree &T, RBNode z){
    RBNode y = T.nil, x = T.root;
    while(x!=T.nil){
        y = x;
        if(z -> key < x -> key) x = x->lson;
        else x = x->rson;
    }
    z -> parent = y;
    if(y==T.nil)  T.root = z;
    else if(z->key < y->key)    y->lson = z;
    else y->rson = z;
    z->lson = T.nil,z->rson = T.nil;
    Insert_Fix(T,z);
}
inline void transplant(RBTree &T, RBNode u,RBNode v){
    //  删除的结点是u
    if(u->parent == T.nil){
        T.root = v;
    }else if(u==u->parent->lson)    u->parent->lson = v;
    else if(u==u->parent->rson) u->parent->rson = v;
    v -> parent = u -> parent;
}
inline void Delete(RBTree &T,RBNode z){
    RBNode y = z;
    bool y_original_color = y -> RBcolor;
    if(z -> lson == T.nil){
        RBNode x = z -> rson;
        transplant(T,z,z->rson);
    }else if(z -> rson == T.nil){
        RBNode x = z -> lson;
        transplant(T,z,z->lson);
    }else y = tree_minimum(T, z->rson);
}
inline void dfs(const RBTree &T, RBNode root){
    if(root==T.nil)   return;
    dfs(T, root -> lson);
    cout << root->key << ' ';
    dfs(T, root -> rson);
}
int main()
{
   // ios::sync_with_stdio(false);    cin.tie(0); cout.tie(0);
    int n;  cin >> n;
    RBTree tr;
    for(int i = 0; i < n; ++i){
        RBNode newNode = new Node;  cin >> newNode->key;
        Insert(tr,newNode);
        dfs(tr,tr.root);
        cout << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值