紅黑樹(RBTree) (Update 12.30 僅實現插入)

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章