红黑树

转载:https://blog.csdn.net/tanrui519521/article/details/80980135

转载:https://www.cnblogs.com/liyuan989/p/4071942.html

转载:https://www.cnblogs.com/alantu2018/p/8462017.html

红黑树与AVL树的比较:

1.AVL树的时间复杂度虽然优于红黑树,但是对于现在的计算机,cpu太快,可以忽略性能差异
2.红黑树的插入删除比AVL树更便于控制操作
3.红黑树整体性能略优于AVL树(红黑树旋转情况少于AVL树)

红黑树的性质:

红黑树是一棵二叉搜索树,它在每个节点增加了一个存储位记录节点的颜色,可以是RED,也可以是BLACK;通过任意一条从根到叶子简单路径上颜色的约束,红黑树保证最长路径不超过最短路径的二倍,因而近似平衡。

红黑树左右子树高差有可能大于 1,所以红黑树不是严格意义上的平衡二叉树(AVL)。

具体性质如下:

  1.     根节点为黑色。
  2.     每个节点不是黑色就是红色。
  3.     每个红色节点的两个儿子一定是黑色。
  4.     所有的叶子节点都是黑色。(注意:这里叶子节点,是指为空的叶子节点!)
  5.     一个红黑树的中任取一个节点,从它所在位置到其他任何叶子节点的简单路径上所经过的黑色节点数相同。

那么为什么当满足以上性质时,就能保证最长路径不超过最短路径的二倍了呢?我们分析一下:

你的最短路径就是全黑节点,最长路径就是一个红节点一个黑节点,最后黑色节点相同时,最长路径刚好是最短路径的两倍

enum RBTColor{RED, BLACK};

template <class T>
class RBTNode{
    public:
        RBTColor color;    // 颜色
        T key;            // 关键字(键值)
        RBTNode *left;    // 左孩子
        RBTNode *right;    // 右孩子
        RBTNode *parent; // 父结点

        RBTNode(T value, RBTColor c, RBTNode *p, RBTNode *l, RBTNode *r):
            key(value),color(c),parent(),left(l),right(r) {}
};

template <class T>
class RBTree {
    private:
        RBTNode<T> *mRoot;    // 根结点

    public:
        RBTree();
        ~RBTree();

        // 前序遍历"红黑树"
        void preOrder();
        // 中序遍历"红黑树"
        void inOrder();
        // 后序遍历"红黑树"
        void postOrder();

        // (递归实现)查找"红黑树"中键值为key的节点
        RBTNode<T>* search(T key);
        // (非递归实现)查找"红黑树"中键值为key的节点
        RBTNode<T>* iterativeSearch(T key);

        // 查找最小结点:返回最小结点的键值。
        T minimum();
        // 查找最大结点:返回最大结点的键值。
        T maximum();

        // 找结点(x)的后继结点。即,查找"红黑树中数据值大于该结点"的"最小结点"。
        RBTNode<T>* successor(RBTNode<T> *x);
        // 找结点(x)的前驱结点。即,查找"红黑树中数据值小于该结点"的"最大结点"。
        RBTNode<T>* predecessor(RBTNode<T> *x);

        // 将结点(key为节点键值)插入到红黑树中
        void insert(T key);

        // 删除结点(key为节点键值)
        void remove(T key);

        // 销毁红黑树
        void destroy();

        // 打印红黑树
        void print();
    private:
        // 前序遍历"红黑树"
        void preOrder(RBTNode<T>* tree) const;
        // 中序遍历"红黑树"
        void inOrder(RBTNode<T>* tree) const;
        // 后序遍历"红黑树"
        void postOrder(RBTNode<T>* tree) const;

        // (递归实现)查找"红黑树x"中键值为key的节点
        RBTNode<T>* search(RBTNode<T>* x, T key) const;
        // (非递归实现)查找"红黑树x"中键值为key的节点
        RBTNode<T>* iterativeSearch(RBTNode<T>* x, T key) const;

        // 查找最小结点:返回tree为根结点的红黑树的最小结点。
        RBTNode<T>* minimum(RBTNode<T>* tree);
        // 查找最大结点:返回tree为根结点的红黑树的最大结点。
        RBTNode<T>* maximum(RBTNode<T>* tree);

        // 左旋
        void leftRotate(RBTNode<T>* &root, RBTNode<T>* x);
        // 右旋
        void rightRotate(RBTNode<T>* &root, RBTNode<T>* y);
        // 插入函数
        void insert(RBTNode<T>* &root, RBTNode<T>* node);
        // 插入修正函数
        void insertFixUp(RBTNode<T>* &root, RBTNode<T>* node);
        // 删除函数
        void remove(RBTNode<T>* &root, RBTNode<T> *node);
        // 删除修正函数
        void removeFixUp(RBTNode<T>* &root, RBTNode<T> *node, RBTNode<T> *parent);

        // 销毁红黑树
        void destroy(RBTNode<T>* &tree);

        // 打印红黑树
        void print(RBTNode<T>* tree, T key, int direction);

#define rb_parent(r)   ((r)->parent)
#define rb_color(r) ((r)->color)
#define rb_is_red(r)   ((r)->color==RED)
#define rb_is_black(r)  ((r)->color==BLACK)
#define rb_set_black(r)  do { (r)->color = BLACK; } while (0)
#define rb_set_red(r)  do { (r)->color = RED; } while (0)
#define rb_set_parent(r,p)  do { (r)->parent = (p); } while (0)
#define rb_set_color(r,c)  do { (r)->color = (c); } while (0)
};

左转:

/* 
 * 对红黑树的节点(x)进行左旋转
 *
 * 左旋示意图(对节点x进行左旋):
 *      px                              px
 *     /                               /
 *    x                               y                
 *   /  \      --(左旋)-->           / \                #
 *  lx   y                          x  ry     
 *     /   \                       /  \
 *    ly   ry                     lx  ly  
 *
 *
 */
template <class T>
void RBTree<T>::leftRotate(RBTNode<T>* &root, RBTNode<T>* x)
{
    // 设置x的右孩子为y
    RBTNode<T> *y = x->right;

    // 将 “y的左孩子” 设为 “x的右孩子”;
    // 如果y的左孩子非空,将 “x” 设为 “y的左孩子的父亲”
    x->right = y->left;
    if (y->left != NULL)
        y->left->parent = x;

    // 将 “x的父亲” 设为 “y的父亲”
    y->parent = x->parent;

    if (x->parent == NULL)
    {
        root = y;            // 如果 “x的父亲” 是空节点,则将y设为根节点
    }
    else
    {
        if (x->parent->left == x)
            x->parent->left = y;    // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子”
        else
            x->parent->right = y;    // 如果 x是它父节点的左孩子,则将y设为“x的父节点的左孩子”
    }
    
    // 将 “x” 设为 “y的左孩子”
    y->left = x;
    // 将 “x的父节点” 设为 “y”
    x->parent = y;
}

右转

/* 
 * 对红黑树的节点(y)进行右旋转
 *
 * 右旋示意图(对节点y进行左旋):
 *            py                               py
 *           /                                /
 *          y                                x                  
 *         /  \      --(右旋)-->            /  \                     #
 *        x   ry                           lx   y  
 *       / \                                   / \                   #
 *      lx  rx                                rx  ry
 * 
 */
template <class T>
void RBTree<T>::rightRotate(RBTNode<T>* &root, RBTNode<T>* y)
{
    // 设置x是当前节点的左孩子。
    RBTNode<T> *x = y->left;

    // 将 “x的右孩子” 设为 “y的左孩子”;
    // 如果"x的右孩子"不为空的话,将 “y” 设为 “x的右孩子的父亲”
    y->left = x->right;
    if (x->right != NULL)
        x->right->parent = y;

    // 将 “y的父亲” 设为 “x的父亲”
    x->parent = y->parent;

    if (y->parent == NULL) 
    {
        root = x;            // 如果 “y的父亲” 是空节点,则将x设为根节点
    }
    else
    {
        if (y == y->parent->right)
            y->parent->right = x;    // 如果 y是它父节点的右孩子,则将x设为“y的父节点的右孩子”
        else
            y->parent->left = x;    // (y是它父节点的左孩子) 将x设为“x的父节点的左孩子”
    }

    // 将 “y” 设为 “x的右孩子”
    x->right = y;

    // 将 “y的父节点” 设为 “x”
    y->parent = x;
}

插入

口诀:祖宗根节点必黑,允许黑连黑,不允许红连红;新增红色,爸叔通红就变色,爸红叔黑就旋转,那黑往那旋。

首先约定插入的新节点的颜色都为红色。然后将该节点插入的按二叉查找树的规则插入到树中。这个节点后文称为N

1.  根节点为空。这种情况,将N的颜色改为黑色即可。

2.  N的父节点为黑色。这种情况不需要做修改。

3.  N的父节点为红色(根据性质3,N的祖父节点必为黑色)。

a.爸叔通红就变色:N的叔父节点为红色。这种情况,将N的父节点和叔父节点的颜色都改为黑色,若祖父节点是跟节点就将其改为黑色,否则将其颜色改为红色,并以祖父节点为插入的目标节点从情况1开始递归检测。

b.爸红叔黑就旋转,那黑往那旋:N的叔父节点为黑色, 且N和N的父节点在同一边(即父节点为祖父的左儿子时,N也是父节点的左儿子。父节点为祖父节点的右儿子时。N也是父节点的右儿子)。以父节点为祖父节的左儿子为例,将父节点改为黑色,祖父节点改为红色(左左),然后以祖父节点为基准右旋。(N为父节点右儿子时(右右)做相应的左旋。)(和平衡二叉树的左左和右右不平衡情况相似)

c.爸红叔黑就旋转,那黑往那旋:N的叔父节点为黑色,切N和N的父节点不在同一边(即父节点为祖父的左儿子时,N是父节点的右儿子。父节点为祖父节点的右儿子时,N也是父节点左儿子)。以父节点为祖父节点的左儿子为例(左右)。以父节点为基准,进行左旋,然后以父节点作为目标插入节点(此时P节点是插入节点N)进入步骤b操作(也就是进行右旋(左左)操作)。如果父节点为祖父节点的右儿子时,N也是父节点左儿子(右左)。就进行右旋,然后进入步骤b操作(也就是进行左旋(右右)操作)。(和平衡二叉树的左右和右左不平衡情况相似)

/* 
 * 将结点(key为节点键值)插入到红黑树中
 *
 * 参数说明:
 *     tree 红黑树的根结点
 *     key 插入结点的键值
 */
template <class T>
void RBTree<T>::insert(T key)
{
    RBTNode<T> *z=NULL;

    // 如果新建结点失败,则返回。
    if ((z=new RBTNode<T>(key,BLACK,NULL,NULL,NULL)) == NULL)
        return ;

    insert(mRoot, z);
}


/* 
 * 将结点插入到红黑树中
 *
 * 参数说明:
 *     root 红黑树的根结点
 *     node 插入的结点        // 对应《算法导论》中的node
 */
template <class T>
void RBTree<T>::insert(RBTNode<T>* &root, RBTNode<T>* node)
{
    RBTNode<T> *y = NULL;//插入节点的位置
    RBTNode<T> *x = root;

    // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
    while (x != NULL)//找到插入的位置,y
    {
        y = x;//记录当前遍历的位置,直到当前节点没有孩子
        if (node->key < x->key)//x是遍历的当前节点,小于向左遍历
            x = x->left;
        else     //向右遍历
            x = x->right;
    }
    //将node节点插入到yj节点下面
    node->parent = y;
    if (y!=NULL)
    {
        if (node->key < y->key)//node节点插入到y的左孩子
            y->left = node;
        else  //右孩子
            y->right = node;
    }
    else //根节点
        root = node;

    // 2. 设置节点的颜色为红色
    node->color = RED;

    // 3. 将它重新修正为一颗二叉查找树
    insertFixUp(root, node);
}

/*
 * 红黑树插入修正函数
 *
 * 在向红黑树中插入节点之后(失去平衡),再调用该函数;
 * 目的是将它重新塑造成一颗红黑树。
 *
 * 参数说明:
 *     root 红黑树的根
 *     node 插入的结点        // 对应《算法导论》中的z
 */
template <class T>
void RBTree<T>::insertFixUp(RBTNode<T>* &root, RBTNode<T>* node)
{
    RBTNode<T> *parent, *gparent;

    // 若“父节点存在,并且父节点的颜色是红色”
    while ((parent = rb_parent(node)) && rb_is_red(parent))
    {
        gparent = rb_parent(parent);

        //若“父节点”是“祖父节点的左孩子”
        if (parent == gparent->left)
        {
            // Case 1条件:叔叔节点是红色
            {
                RBTNode<T> *uncle = gparent->right;
                if (uncle && rb_is_red(uncle))
                {
                    rb_set_black(uncle);
                    rb_set_black(parent);
                    rb_set_red(gparent);
                    node = gparent; //祖父节点作为插入节点,继续向上染色
                    continue;
                }
            }

            // Case 2条件:叔叔是黑色,且当前节点是右孩子
            if (parent->right == node) //左右,先左转,变成左左,再右转
            {
                RBTNode<T> *tmp;
                leftRotate(root, parent);
                tmp = parent;
                parent = node;//旋转后,父节点变成了子节点,子节点变成了父节点
                node = tmp;//父节点作为插入节点,继续向上染色
            }

            // Case 3条件:叔叔是黑色,且当前节点是左孩子。
            //左左,右转
            rb_set_black(parent);
            rb_set_red(gparent);
            rightRotate(root, gparent);
        } 
        else//若“z的父节点”是“z的祖父节点的右孩子”
        {
            // Case 1条件:叔叔节点是红色
            {
                RBTNode<T> *uncle = gparent->left;
                if (uncle && rb_is_red(uncle))
                {
                    rb_set_black(uncle);
                    rb_set_black(parent);
                    rb_set_red(gparent);
                    node = gparent; //祖父节点作为插入节点,继续向上染色
                    continue;
                }
            }

            // Case 2条件:叔叔是黑色,且当前节点是左孩子
            if (parent->left == node)//右左,先右转,变成右右,再左转
            {
                RBTNode<T> *tmp;
                rightRotate(root, parent);
                tmp = parent;
                parent = node;//旋转后,父节点变成了子节点,子节点变成了父节点
                node = tmp;//父节点作为插入节点,继续向上染色
            }

            // Case 3条件:叔叔是黑色,且当前节点是右孩子。
            //右右,左转
            rb_set_black(parent);
            rb_set_red(gparent);
            leftRotate(root, gparent);
        }
    }

    // 将根节点设为黑色
    rb_set_black(root);
}

删除

删除的节点有两个儿子时,可以转化为删除的节点只有一个儿子时的问题。对于二叉查找树,在删除带有两个非叶子儿子的节点的时候,我们找到要么在它的左子树中的最大元素、要么在它的右子树中的最小元素,并把它的值转移到要删除的节点中。我们接着删除我们从中复制出值的那个节点,它必定有少于两个非叶子的儿子。因为只是复制了一个值,不违反任何性质,这就把问题简化为如何删除最多有一个儿子的节点的问题。它不关心这个节点是最初要删除的节点还是我们从中复制出值的那个节点。

那么所有情况都可以转化为删除只有一个儿子的节点的情况,我们约定这个要删除的节点为N(若N“没有”儿子节点,并用他的任意一个为叶子节点的儿子节点顶替即可)

1.  N为红色节点时。直接删除N,用它的黑色儿子代替它的位置。

2.  N为黑色节点,且父节点为红色。直接删除N,用它的儿子节点代替它的位置,并将该儿子节点改为黑色。

3.  N为黑色节点,且父节点为黑色。我们之间删除N,用它的儿子节点代替它,该儿子节点成为N',将N’的颜色改为黑色。

a.N’的兄弟节点和兄弟节点的2个儿子都为黑色。交换兄弟节点和父节点的颜色即可

b.N‘的兄弟节点为黑色、且兄弟节点的红色儿子和兄弟节点在一边(即兄弟节点为左儿子时,红色儿子也为左儿子。兄弟节点为右儿子时,红色儿子也为右儿子)。我们以兄弟节点为右儿子为例。将祖父节点和兄弟节点的颜色互换,并将红色右儿子的颜色改为黑色,然后以祖父节点为基准左旋。(若兄弟节点为左儿子,则相应的右旋)

c.N‘的兄弟节点为黑色、且兄弟节点的红儿子和兄弟节点不在一边(即兄弟节点为左儿子时,红色儿子也为右儿子。兄弟节点为右儿子时,红色儿子也为左儿子)。我们以兄弟结点为右儿子为例。将兄弟节点和它的红色儿子的颜色互换,然后以兄弟节点为基准右旋。此时对于N’来说就进入了上文b情况。(若兄弟节点为右儿子,则相应的左旋)

d.N‘的兄弟节点为红色。以兄弟节点为右儿子为例,将父节点和兄弟节点的颜色互换,然后以父节点为基准左旋(若兄弟节点为左儿子则相应的右旋),此N’有一个黑色的兄弟节点,接下来N就可以进入a、b、c三种情况分别操作了。

e.N‘的兄弟节为黑色,父节点也为黑色。此时将兄弟节点的颜色改为红色。然后以父节点为目标插入节点从头开始依次判断。


/* 
 * 删除红黑树中键值为key的节点
 *
 * 参数说明:
 *     tree 红黑树的根结点
 */
template <class T>
void RBTree<T>::remove(T key)
{
    RBTNode<T> *node; 

    // 查找key对应的节点(node),找到的话就删除该节点
    if ((node = search(mRoot, key)) != NULL)
        remove(mRoot, node);
}


/* 
 * 删除结点(node),并返回被删除的结点
 *
 * 参数说明:
 *     root 红黑树的根结点
 *     node 删除的结点
 */
template <class T>
void RBTree<T>::remove(RBTNode<T>* &root, RBTNode<T> *node)
{
    RBTNode<T> *child, *parent;
    RBTColor color;

    // 被删除节点的"左右孩子都不为空"的情况。
    if ( (node->left!=NULL) && (node->right!=NULL) ) 
    {
        // 被删节点的后继节点。(称为"取代节点")
        // 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。
        RBTNode<T> *replace = node;

        // 获取后继节点
        replace = replace->right;//先将最小节点定义为删除节点右孩子
        while (replace->left != NULL) //在删除节点的右节点中找到最小节点
            replace = replace->left;

        // "node节点"不是根节点(只有根节点不存在父节点)
        if (rb_parent(node))
        {
            if (rb_parent(node)->left == node)//删除节点是父节点的左孩子,那么将父节点的左孩子指向替代节点
                rb_parent(node)->left = replace;
            else//删除节点是父节点的右孩子,那么将父节点的右孩子指向替代节点
                rb_parent(node)->right = replace;
        } 
        else 
            // "node节点"是根节点,更新根节点。
            root = replace;

        // child是"取代节点"的右孩子,也是需要"调整的节点"。
        // "取代节点"肯定不存在左孩子!因为它是一个后继节点。
        child = replace->right;//从替代节点的右孩子开始调整
        parent = rb_parent(replace);
        // 保存"取代节点"的颜色
        color = rb_color(replace);

        // "被删除节点"是"它的后继节点的父节点"
        if (parent == node)//删除节点是替代节点的父节点,直接用
        {
            parent = replace;//父节点就是替代节点
        } 
        else//删除节点不是替代节点的父节点
        {
            // child不为空
            if (child)
                rb_set_parent(child, parent);//替代节点的右孩子的父节点是替代节点的父节点
            parent->left = child;//替代节点的父节点的左孩子是替代节点的右孩子

            replace->right = node->right;//替代节点的右孩子是删除节点的左孩子
            rb_set_parent(node->right, replace);//删除节点的右孩子的父节点是替代节点
        }

        replace->parent = node->parent;//替代节点的父节点是删除节点的父节点
        replace->color = node->color;//替带节点的颜色是删除节点的颜色
        replace->left = node->left;//替代节点的左孩子是删除接机点的左孩子
        node->left->parent = replace;//删除节点的左节点的父节点是替代节点

        if (color == BLACK)//替代节点是黑色,要重新染色
            removeFixUp(root, child, parent);

        delete node;
        return ;
    }

    if (node->left !=NULL) //删除节点的右孩子为空,保存左孩子
        child = node->left;
    else //删除节点的左孩子为空,保存右孩子
        child = node->right;

    parent = node->parent;//保存父节点
    // 保存"取代节点"的颜色
    color = node->color;

    if (child)
        child->parent = parent; //孩子节点的父节点是删除节点的父节点

    // "node节点"不是根节点
    if (parent)
    {
        if (parent->left == node)//删除节点的父节点的左孩子等于删除节点,那么父节点的左孩子就是删除节点的孩子
            parent->left = child;
        else //删除节点的父节点的右孩子等于删除节点,那么父节点的右孩子就是删除节点的孩子
            parent->right = child;
    }
    else //是根节点,根节点就是子节点
        root = child;

    if (color == BLACK) //删除节点是黑色,要重新染色
        removeFixUp(root, child, parent);
    delete node;
}

/*
 * 红黑树删除修正函数
 *
 * 在从红黑树中删除插入节点之后(红黑树失去平衡),再调用该函数;
 * 目的是将它重新塑造成一颗红黑树。
 *
 * 参数说明:
 *     root 红黑树的根
 *     node 待修正的节点(当前节点)
 */
template <class T>
void RBTree<T>::removeFixUp(RBTNode<T>* &root, RBTNode<T> *node, RBTNode<T> *parent)
{
    RBTNode<T> *other;

    //当前节点不是根节点,并且当前节点是黑色节点
    //或者当前节点不是根节点,当前节点是空节点
    while ((!node || rb_is_black(node)) && node != root)
    {
        if (parent->left == node)//当前节点是父节点的左节点
        {
            other = parent->right;//父节点的右节点,也就是当前节点的兄弟节点
            if (rb_is_red(other))
            {
                // Case d: x的兄弟w是红色的 ,兄弟节点是右孩子 
                rb_set_black(other);//兄弟节点设置为黑色
                rb_set_red(parent);//父节点设置为红色
                leftRotate(root, parent);//右右,父节点
                other = parent->right;
            }
            if ((!other->left || rb_is_black(other->left)) &&
                (!other->right || rb_is_black(other->right)))
            {
                // Case a: x的兄弟w是黑色,且w的俩个孩子也都是黑色的  
                rb_set_red(other);
                node = parent;
                parent = rb_parent(node);
            }
            else
            {
                if (!other->right || rb_is_black(other->right))
                {
                    // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。  
                    rb_set_black(other->left);
                    rb_set_red(other);
                    rightRotate(root, other);
                    other = parent->right;
                }
                // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
                rb_set_color(other, rb_color(parent));
                rb_set_black(parent);
                rb_set_black(other->right);
                leftRotate(root, parent);
                node = root;
                break;
            }
        }
        else  //当前节点是父节点的右孩子
        {
            other = parent->left;
            if (rb_is_red(other))
            {
                // Case 1: x的兄弟w是红色的  
                rb_set_black(other);
                rb_set_red(parent);
                rightRotate(root, parent);
                other = parent->left;
            }
            if ((!other->left || rb_is_black(other->left)) &&
                (!other->right || rb_is_black(other->right)))
            {
                // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的  
                rb_set_red(other);
                node = parent;
                parent = rb_parent(node);
            }
            else
            {
                if (!other->left || rb_is_black(other->left))
                {
                    // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。  
                    rb_set_black(other->right);
                    rb_set_red(other);
                    leftRotate(root, other);
                    other = parent->left;
                }
                // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
                rb_set_color(other, rb_color(parent));
                rb_set_black(parent);
                rb_set_black(other->left);
                rightRotate(root, parent);
                node = root;
                break;
            }
        }
    }
    if (node)//直接设置当前节点为黑色
        rb_set_black(node);
}

完整源码:

#ifndef RBTREE_H_
#define RBTREE_H_

#include<iostream>
#include<iomanip>
using namespace std;

enum RBTColor{RED, BLACK};

template <class T>
class RBTNode
{
public:
    T key; //关键字
    RBTColor color;//color
    RBTNode *parent;
    RBTNode *left;
    RBTNode *right;
    RBTNode(T value, RBTColor c, RBTNode *p, RBTNode *l, RBTNode *r):
            key(value),color(c),parent(),left(l),right(r) {}
};

template <class T>
class RBTree
{
private:
    RBTNode<T> *mRoot;
public:
    RBTree();
    ~RBTree();
    //前序遍历
    void preOrder();
    //中序遍历
    void inOrder();
    //后序遍历
    void postOrder();
    //递归实现键值key 的查找
    RBTNode<T>* search(T key);
    //非递归实现键值key的查找
    RBTNode<T>* iterativeSearch(T key);

    //查找最小结点:返回最小结点的键值
    T minimum();
    //查找最大结点: 返回最大结点的键值
    T maximum();

    //找结点的后继结点
    RBTNode<T>* successor(RBTNode<T> *x);
    //找结点的前驱结点
    RBTNode<T>* predecessor(RBTNode<T> *x);

    //将结点插入到红黑树中
    void insert(T key);

    //删除结点
    void remove(T key);

    //销毁红黑树
    void destroy();

    //打印红黑树
    void print();
private:
    //前序遍历红黑树
    void preOrder(RBTNode<T> *root) const;
    //中序遍历红黑树
    void inOrder(RBTNode<T> *root) const;
    //后序遍历红黑树
    void postOrder(RBTNode<T> *root) const;

    //递归实现键值的查找
    RBTNode<T>* search(RBTNode<T> *root,T key) const;
    //非递归实现键值的查找
    RBTNode<T>* iterativeSearch(RBTNode<T> *root,T key) const;

    //查找最小值结点
    RBTNode<T>* minimum(RBTNode<T> *root);
    //查找最大值结点
    RBTNode<T>* maximum(RBTNode<T> *root);

    //左旋
    void leftRotate(RBTNode<T> *&root,RBTNode<T>* x);
    //右旋
    void rightRotate(RBTNode<T> *&root,RBTNode<T> *x);
    //插入函数
    void insert(RBTNode<T> *&root,RBTNode<T>* node);
    //删除函数
    void remove(RBTNode<T> *&root,RBTNode<T>* node);

    //插入修正
    void insertFixUp(RBTNode<T> *&root,RBTNode<T>* node);
    //删除修正
    void removeFixUp(RBTNode<T> *&root,RBTNode<T>* node);

    //销毁红黑树
    void destroy(RBTNode<T> *&root);
    //打印红黑树
    void print(RBTNode<T> *root,T key,int direction);

#define rb_parent(r)   ((r)->parent)
#define rb_color(r) ((r)->color)
#define rb_is_red(r)   ((r)->color==RED)
#define rb_is_black(r)  ((r)->color==BLACK)
#define rb_set_black(r)  do { (r)->color = BLACK; } while (0)
#define rb_set_red(r)  do { (r)->color = RED; } while (0)
#define rb_set_parent(r,p)  do { (r)->parent = (p); } while (0)
#define rb_set_color(r,c)  do { (r)->color = (c); } while (0)
};

template<class T>
RBTree<T>::RBTree():mRoot(NULL)
{
    mRoot=NULL;
}

template<class T>
RBTree<T>::~RBTree()
{
    destroy();
}

template<class T>
void RBTree<T>::preOrder(RBTNode<T> *tree) const
{
    if(tree)
    {
        cout<<tree->key<<" ";
        preOrder(tree->left);
        preOrder(tree->right);
    }
}

template<class T>
void RBTree<T>::preOrder()
{
    preOrder(mRoot);
}

template<class T>
void RBTree<T>::inOrder(RBTNode<T> *tree) const
{
    if(tree)
    {
        inOrder(tree->left);
        cout<<tree->key<<" ";
        inOrder(tree->right);
    }
}

template<class T>
void RBTree<T>::inOrder()
{
    inOrder(mRoot);
}

template<class T>
void RBTree<T>::postOrder(RBTNode<T> *tree) const
{
    if(tree)
    {
        postOrder(tree->left);
        postOrder(tree->right);
        cout<<tree->key<<" ";
    }
}
template<class T> void RBTree<T>::inOrder() { inOrder(mRoot); } template<class T> void RBTree<T>::postOrder(RBTNode<T> *tree) const { if(tree) { postOrder(tree->left); postOrder(tree->right); cout<<tree->key<<" "; } } template<class T> void RBTree<T>::postOrder() { postOrder(mRoot); } template<class T> RBTNode<T>* RBTree<T>::search(RBTNode<T> *x,T key) const { if(x==NULL||x->key==key) return x; if(key<x->key) return search(x->left,key); else return search(x->right,key); } template<class T> RBTNode<T>* RBTree<T>::search(T key) { search(mRoot,key); } template<class T> RBTNode<T>* RBTree<T>::iterativeSearch(RBTNode<T> *x,T key) const { while(x&&x->key!=key) { if(key<x->key) x=x->left; else x=x->right; } return x; } template<class T> RBTNode<T>* RBTree<T>::iterativeSearch(T key) { iterativeSearch(mRoot,key); } template<class T> RBTNode<T>* RBTree<T>::minimum(RBTNode<T> *tree) { if(tree==NULL) return NULL; while(tree->left) { tree=tree->left; } return tree; } template<class T> T RBTree<T>::minimum() { RBTNode<T> *p=minimum(mRoot); if(p!=NULL) return p->key; return (T)NULL; } template<class T> RBTNode<T>* RBTree<T>::maximum(RBTNode<T> *tree) { if(tree==NULL) return NULL; while(tree->right) tree=tree->right; return tree; } template<class T> T RBTree<T>::maximum() { RBTNode<T> *p=maximum(mRoot); if(p!=NULL) return p->key; return (T)NULL; } template<class T> RBTNode<T>* RBTree<T>::successor(RBTNode<T> *x) { if(x->right) return minimum(x->right); RBTNode<T> *y=x->parent; while(y&&y->right==x) { x=y; y=y->parent; } return y; } template<class T> RBTNode<T>* RBTree<T>::predecessor(RBTNode<T> *x) { if(x->left) return maximum(x->left); RBTNode<T> *y=x->parent; while(y&&y->left==x) { x=y; y=y->parent; } return y; } template<class T> void RBTree<T>::leftRotate(RBTNode<T> *&root,RBTNode<T> *x) { RBTNode<T> *y=x->right; x->right=y->left; if(y->left) y->left->parent=x; y->parent=x->parent; if(x->parent==NULL) root=y; else { if(x=x->parent->left) x->parent->left=y; else x->parent->right=y; } x->parent=y; y->left=x; } template<class T> void RBTree<T>::rightRotate(RBTNode<T> *&root,RBTNode<T> *x) { RBTNode<T> *y=x->left; x->left=y->right; if(y->right) y->right->parent=x; y->parent=x->parent; if(x->parent==NULL) root=y; else { if(x==x->parent->left) x->parent->left=y; else x->parent->right=y; } y->right=x; x->parent=y; } template<class T> void RBTree<T>::insertFixUp(RBTNode<T> *&root,RBTNode<T>* node) { RBTNode<T> *parent,*gparent; while((parent=rb_parent(node))&&rb_is_red(parent)) { gparent=rb_parent(parent); if(parent==gparent->left) { RBTNode<T> *uncle=gparent->right; if(uncle&&rb_is_red(uncle)) { rb_set_black(parent); rb_set_black(uncle); rb_set_red(gparent); node=gparent; continue; } else { if(node==parent->right) { leftRotate(root,parent); RBTNode<T> *tmp=parent; parent=node; node=tmp; } rb_set_black(parent); rb_set_red(gparent); rightRotate(root,gparent); } } else { RBTNode<T> *uncle=gparent->left; if(uncle&&rb_is_red(uncle)) { rb_set_black(parent); rb_set_black(uncle); rb_set_red(gparent); node=gparent; continue; } else { if(node==parent->left) { rightRotate(root,parent); RBTNode<T> *tmp=parent; parent=node; node=tmp; } rb_set_black(parent); rb_set_red(gparent); leftRotate(root,gparent); } } } rb_set_black(root); } template <class T> void RBTree<T>::insert(RBTNode<T>* &root, RBTNode<T>* node) { RBTNode<T> *y = NULL; RBTNode<T> *x = root; // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。 while (x != NULL) { y = x; if (node->key < x->key) x = x->left; else x = x->right; } node->parent = y; if (y!=NULL) { if (node->key < y->key) y->left = node; else y->right = node; } else root = node; // 2. 设置节点的颜色为红色 node->color = RED; // 3. 将它重新修正为一颗二叉查找树 insertFixUp(root, node); } template <class T> void RBTree<T>::insert(T key) { RBTNode<T> *z=NULL; // 如果新建结点失败,则返回。 if ((z=new RBTNode<T>(key,BLACK,NULL,NULL,NULL)) == NULL) return ; insert(mRoot, z); } template<class T> void RBTree<T>::removeFixUp(RBTNode<T> *&root,RBTNode<T>* node) { while(node!=root&&rb_is_black(node)) { RBTNode<T> *parent=rb_parent(node); if(node==parent->left) { RBTNode<T> *other=parent->right; if(rb_is_red(other)) { rb_set_black(other); rb_set_red(parent); rightRotate(root,parent); other=parent->right; } if(rb_is_black(other->left)&&rb_is_black(other->right)) { rb_set_red(other); node=parent; parent=rb_parent(node); } else { if(rb_is_black(other->right)) { rb_set_black(other->left); rb_set_red(other); rightRotate(root,other); other=parent->right; } rb_set_color(other,rb_color(parent)); rb_set_black(other->right); rb_set_black(parent); leftRotate(root,parent); node=root; } } else { RBTNode<T> *other=parent->left; if(rb_is_red(other)) { rb_set_black(other); rb_set_red(parent); rightRotate(root,parent); other=parent->left; } if(rb_is_black(other->left)&&rb_is_black(other->right)) { rb_set_red(other); node=parent; parent=rb_parent(node); } else { if(rb_is_black(other->left)) { rb_set_black(other->right); rb_set_red(other); leftRotate(root,other); other=parent->left; } rb_set_color(other,rb_color(parent)); rb_set_black(other->left); rb_set_black(parent); rightRotate(root,parent); node=root; } } } rb_set_black(node); } template<class T> void RBTree<T>::remove(RBTNode<T> *&root,RBTNode<T> *node) { RBTNode<T> *pnode,*rnode; if(node->left==NULL||node->right==NULL) pnode=node; else pnode=successor(node); if(pnode->left) rnode=pnode->left; else rnode=pnode->right; rnode->parent=pnode->parent; if(pnode->parent==NULL) root=rnode; else { if(pnode==pnode->parent->left) pnode->parent->left=rnode; else pnode->parent->right=rnode; } if(node!=pnode) { node->key=pnode->key; } if(pnode->color==BLACK) removeFixUp(root,rnode); delete pnode; return; } template<class T> void RBTree<T>::remove(T key) { RBTNode<T> *node; if((node=search(mRoot,key))!=NULL) remove(mRoot,node); } template<class T> void RBTree<T>::destroy(RBTNode<T> *&tree) { if(tree==NULL) return; if(tree->left!=NULL) return destroy(tree->left); if(tree->right!=NULL) return destroy(tree->left); delete tree; tree=NULL; } template <class T> void RBTree<T>::destroy() { destroy(mRoot); } template<class T> void RBTree<T>::print(RBTNode<T> *tree,T key,int direction) { if(tree!=NULL) { if(direction==0) cout<<setw(2)<<tree->key<<"(B) is root"<<endl; else cout << setw(2) << tree->key << (rb_is_red(tree)?"(R)":"(B)") << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") << endl; print(tree->left, tree->key, -1); print(tree->right,tree->key, 1); } } template<class T> void RBTree<T>::print() { if(mRoot) print(mRoot,mRoot->key,0); } #endif // RBTREE_H_
template<class T> void RBTree<T>::inOrder() { inOrder(mRoot); } template<class T> void RBTree<T>::postOrder(RBTNode<T> *tree) const { if(tree) { postOrder(tree->left); postOrder(tree->right); cout<<tree->key<<" "; } } template<class T> void RBTree<T>::postOrder() { postOrder(mRoot); } template<class T> RBTNode<T>* RBTree<T>::search(RBTNode<T> *x,T key) const { if(x==NULL||x->key==key) return x; if(key<x->key) return search(x->left,key); else return search(x->right,key); } template<class T> RBTNode<T>* RBTree<T>::search(T key) { search(mRoot,key); } template<class T> RBTNode<T>* RBTree<T>::iterativeSearch(RBTNode<T> *x,T key) const { while(x&&x->key!=key) { if(key<x->key) x=x->left; else x=x->right; } return x; } template<class T> RBTNode<T>* RBTree<T>::iterativeSearch(T key) { iterativeSearch(mRoot,key); } template<class T> RBTNode<T>* RBTree<T>::minimum(RBTNode<T> *tree) { if(tree==NULL) return NULL; while(tree->left) { tree=tree->left; } return tree; } template<class T> T RBTree<T>::minimum() { RBTNode<T> *p=minimum(mRoot); if(p!=NULL) return p->key; return (T)NULL; } template<class T> RBTNode<T>* RBTree<T>::maximum(RBTNode<T> *tree) { if(tree==NULL) return NULL; while(tree->right) tree=tree->right; return tree; } template<class T> T RBTree<T>::maximum() { RBTNode<T> *p=maximum(mRoot); if(p!=NULL) return p->key; return (T)NULL; } template<class T> RBTNode<T>* RBTree<T>::successor(RBTNode<T> *x) { if(x->right) return minimum(x->right); RBTNode<T> *y=x->parent; while(y&&y->right==x) { x=y; y=y->parent; } return y; } template<class T> RBTNode<T>* RBTree<T>::predecessor(RBTNode<T> *x) { if(x->left) return maximum(x->left); RBTNode<T> *y=x->parent; while(y&&y->left==x) { x=y; y=y->parent; } return y; } template<class T> void RBTree<T>::leftRotate(RBTNode<T> *&root,RBTNode<T> *x) { RBTNode<T> *y=x->right; x->right=y->left; if(y->left) y->left->parent=x; y->parent=x->parent; if(x->parent==NULL) root=y; else { if(x=x->parent->left) x->parent->left=y; else x->parent->right=y; } x->parent=y; y->left=x; } template<class T> void RBTree<T>::rightRotate(RBTNode<T> *&root,RBTNode<T> *x) { RBTNode<T> *y=x->left; x->left=y->right; if(y->right) y->right->parent=x; y->parent=x->parent; if(x->parent==NULL) root=y; else { if(x==x->parent->left) x->parent->left=y; else x->parent->right=y; } y->right=x; x->parent=y; } template<class T> void RBTree<T>::insertFixUp(RBTNode<T> *&root,RBTNode<T>* node) { RBTNode<T> *parent,*gparent; while((parent=rb_parent(node))&&rb_is_red(parent)) { gparent=rb_parent(parent); if(parent==gparent->left) { RBTNode<T> *uncle=gparent->right; if(uncle&&rb_is_red(uncle)) { rb_set_black(parent); rb_set_black(uncle); rb_set_red(gparent); node=gparent; continue; } else { if(node==parent->right) { leftRotate(root,parent); RBTNode<T> *tmp=parent; parent=node; node=tmp; } rb_set_black(parent); rb_set_red(gparent); rightRotate(root,gparent); } } else { RBTNode<T> *uncle=gparent->left; if(uncle&&rb_is_red(uncle)) { rb_set_black(parent); rb_set_black(uncle); rb_set_red(gparent); node=gparent; continue; } else { if(node==parent->left) { rightRotate(root,parent); RBTNode<T> *tmp=parent; parent=node; node=tmp; } rb_set_black(parent); rb_set_red(gparent); leftRotate(root,gparent); } } } rb_set_black(root); } template <class T> void RBTree<T>::insert(RBTNode<T>* &root, RBTNode<T>* node) { RBTNode<T> *y = NULL; RBTNode<T> *x = root; // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。 while (x != NULL) { y = x; if (node->key < x->key) x = x->left; else x = x->right; } node->parent = y; if (y!=NULL) { if (node->key < y->key) y->left = node; else y->right = node; } else root = node; // 2. 设置节点的颜色为红色 node->color = RED; // 3. 将它重新修正为一颗二叉查找树 insertFixUp(root, node); } template <class T> void RBTree<T>::insert(T key) { RBTNode<T> *z=NULL; // 如果新建结点失败,则返回。 if ((z=new RBTNode<T>(key,BLACK,NULL,NULL,NULL)) == NULL) return ; insert(mRoot, z); } template<class T> void RBTree<T>::removeFixUp(RBTNode<T> *&root,RBTNode<T>* node) { while(node!=root&&rb_is_black(node)) { RBTNode<T> *parent=rb_parent(node); if(node==parent->left) { RBTNode<T> *other=parent->right; if(rb_is_red(other)) { rb_set_black(other); rb_set_red(parent); rightRotate(root,parent); other=parent->right; } if(rb_is_black(other->left)&&rb_is_black(other->right)) { rb_set_red(other); node=parent; parent=rb_parent(node); } else { if(rb_is_black(other->right)) { rb_set_black(other->left); rb_set_red(other); rightRotate(root,other); other=parent->right; } rb_set_color(other,rb_color(parent)); rb_set_black(other->right); rb_set_black(parent); leftRotate(root,parent); node=root; } } else { RBTNode<T> *other=parent->left; if(rb_is_red(other)) { rb_set_black(other); rb_set_red(parent); rightRotate(root,parent); other=parent->left; } if(rb_is_black(other->left)&&rb_is_black(other->right)) { rb_set_red(other); node=parent; parent=rb_parent(node); } else { if(rb_is_black(other->left)) { rb_set_black(other->right); rb_set_red(other); leftRotate(root,other); other=parent->left; } rb_set_color(other,rb_color(parent)); rb_set_black(other->left); rb_set_black(parent); rightRotate(root,parent); node=root; } } } rb_set_black(node); } template<class T> void RBTree<T>::remove(RBTNode<T> *&root,RBTNode<T> *node) { RBTNode<T> *pnode,*rnode; if(node->left==NULL||node->right==NULL) pnode=node; else pnode=successor(node); if(pnode->left) rnode=pnode->left; else rnode=pnode->right; rnode->parent=pnode->parent; if(pnode->parent==NULL) root=rnode; else { if(pnode==pnode->parent->left) pnode->parent->left=rnode; else pnode->parent->right=rnode; } if(node!=pnode) { node->key=pnode->key; } if(pnode->color==BLACK) removeFixUp(root,rnode); delete pnode; return; } template<class T> void RBTree<T>::remove(T key) { RBTNode<T> *node; if((node=search(mRoot,key))!=NULL) remove(mRoot,node); } template<class T> void RBTree<T>::destroy(RBTNode<T> *&tree) { if(tree==NULL) return; if(tree->left!=NULL) return destroy(tree->left); if(tree->right!=NULL) return destroy(tree->left); delete tree; tree=NULL; } template <class T> void RBTree<T>::destroy() { destroy(mRoot); } template<class T> void RBTree<T>::print(RBTNode<T> *tree,T key,int direction) { if(tree!=NULL) { if(direction==0) cout<<setw(2)<<tree->key<<"(B) is root"<<endl; else cout << setw(2) << tree->key << (rb_is_red(tree)?"(R)":"(B)") << " is " << setw(2) << key << "'s " << setw(12) << (direction==1?"right child" : "left child") << endl; print(tree->left, tree->key, -1); print(tree->right,tree->key, 1); } } template<class T> void RBTree<T>::print() { if(mRoot) print(mRoot,mRoot->key,0); } #endif // RBTREE_H_class T>
void RBTree<T>::postOrder()
{
    postOrder(mRoot);
}

template<class T>
RBTNode<T>* RBTree<T>::search(RBTNode<T> *x,T key) const
{
    if(x==NULL||x->key==key)
        return x;
    if(key<x->key)
        return search(x->left,key);
    else
        return search(x->right,key);
}

template<class T>
RBTNode<T>* RBTree<T>::search(T key)
{
    search(mRoot,key);
}

template<class T>
RBTNode<T>* RBTree<T>::iterativeSearch(RBTNode<T> *x,T key) const
{
    while(x&&x->key!=key)
    {
        if(key<x->key)
            x=x->left;
        else
            x=x->right;
    }
    return x;
}

template<class T>
RBTNode<T>* RBTree<T>::iterativeSearch(T key)
{
    iterativeSearch(mRoot,key);
}

template<class T>
RBTNode<T>* RBTree<T>::minimum(RBTNode<T> *tree)
{
    if(tree==NULL)
        return NULL;
    while(tree->left)
    {
        tree=tree->left;
    }
    return tree;
}

template<class T>
T RBTree<T>::minimum()
{
    RBTNode<T> *p=minimum(mRoot);
    if(p!=NULL)
        return p->key;
    return (T)NULL;
}

template<class T>
RBTNode<T>* RBTree<T>::maximum(RBTNode<T> *tree)
{
    if(tree==NULL)
        return NULL;
    while(tree->right)
        tree=tree->right;
    return tree;
}

template<class T>
T RBTree<T>::maximum()
{
    RBTNode<T> *p=maximum(mRoot);
    if(p!=NULL)
        return p->key;
    return (T)NULL;
}

template<class T>
RBTNode<T>* RBTree<T>::successor(RBTNode<T> *x)
{
    if(x->right)
        return minimum(x->right);
    RBTNode<T> *y=x->parent;
    while(y&&y->right==x)
    {
        x=y;
        y=y->parent;
    }
    return y;
}

template<class T>
RBTNode<T>* RBTree<T>::predecessor(RBTNode<T> *x)
{
    if(x->left)
        return maximum(x->left);
    RBTNode<T> *y=x->parent;
    while(y&&y->left==x)
    {
        x=y;
        y=y->parent;
    }
    return y;
}

template<class T>
void RBTree<T>::leftRotate(RBTNode<T> *&root,RBTNode<T> *x)
{
    RBTNode<T> *y=x->right;
    x->right=y->left;
    if(y->left)
        y->left->parent=x;
    y->parent=x->parent;
    if(x->parent==NULL)
        root=y;
    else
    {
        if(x=x->parent->left)
            x->parent->left=y;
        else
            x->parent->right=y;
    }
    x->parent=y;
    y->left=x;
}

template<class T>
void RBTree<T>::rightRotate(RBTNode<T> *&root,RBTNode<T> *x)
{
    RBTNode<T> *y=x->left;
    x->left=y->right;
    if(y->right)
        y->right->parent=x;
    y->parent=x->parent;
    if(x->parent==NULL)
        root=y;
    else
    {
        if(x==x->parent->left)
            x->parent->left=y;
        else
            x->parent->right=y;
    }
    y->right=x;
    x->parent=y;
}

template<class T>
void RBTree<T>::insertFixUp(RBTNode<T> *&root,RBTNode<T>* node)
{
    RBTNode<T> *parent,*gparent;
    while((parent=rb_parent(node))&&rb_is_red(parent))
    {
        gparent=rb_parent(parent);
        if(parent==gparent->left)
        {
            RBTNode<T> *uncle=gparent->right;
            if(uncle&&rb_is_red(uncle))
            {
                rb_set_black(parent);
                rb_set_black(uncle);
                rb_set_red(gparent);
                node=gparent;
                continue;
            }
            else
            {
                if(node==parent->right)
                {
                    leftRotate(root,parent);
                    RBTNode<T> *tmp=parent;
                    parent=node;
                    node=tmp;
                }
                rb_set_black(parent);
                rb_set_red(gparent);
                rightRotate(root,gparent);
            }
        }
        else
        {
            RBTNode<T> *uncle=gparent->left;
            if(uncle&&rb_is_red(uncle))
            {
                rb_set_black(parent);
                rb_set_black(uncle);
                rb_set_red(gparent);
                node=gparent;
                continue;
            }
            else
            {
                if(node==parent->left)
                {
                    rightRotate(root,parent);
                    RBTNode<T> *tmp=parent;
                    parent=node;
                    node=tmp;
                }
                rb_set_black(parent);
                rb_set_red(gparent);
                leftRotate(root,gparent);
            }
        }
    }
    rb_set_black(root);
}

template <class T>
void RBTree<T>::insert(RBTNode<T>* &root, RBTNode<T>* node)
{
    RBTNode<T> *y = NULL;
    RBTNode<T> *x = root;

    // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
    while (x != NULL)
    {
        y = x;
        if (node->key < x->key)
            x = x->left;
        else
            x = x->right;
    }
    node->parent = y;
    if (y!=NULL)
    {
        if (node->key < y->key)
            y->left = node;
        else
            y->right = node;
    }
    else
        root = node;

    // 2. 设置节点的颜色为红色
    node->color = RED;

    // 3. 将它重新修正为一颗二叉查找树
    insertFixUp(root, node);
}


template <class T>
void RBTree<T>::insert(T key)
{
    RBTNode<T> *z=NULL;

    // 如果新建结点失败,则返回。
    if ((z=new RBTNode<T>(key,BLACK,NULL,NULL,NULL)) == NULL)
        return ;

    insert(mRoot, z);
}

template<class T>
void RBTree<T>::removeFixUp(RBTNode<T> *&root,RBTNode<T>* node)
{
    while(node!=root&&rb_is_black(node))
    {
        RBTNode<T> *parent=rb_parent(node);
        if(node==parent->left)
        {
            RBTNode<T> *other=parent->right;
            if(rb_is_red(other))
            {
                rb_set_black(other);
                rb_set_red(parent);
                rightRotate(root,parent);
                other=parent->right;
            }
            if(rb_is_black(other->left)&&rb_is_black(other->right))
            {
                rb_set_red(other);
                node=parent;
                parent=rb_parent(node);
            }
            else
            {
                if(rb_is_black(other->right))
                {
                    rb_set_black(other->left);
                    rb_set_red(other);
                    rightRotate(root,other);
                    other=parent->right;
                }
                rb_set_color(other,rb_color(parent));
                rb_set_black(other->right);
                rb_set_black(parent);
                leftRotate(root,parent);
                node=root;
            }
        }
        else
        {
            RBTNode<T> *other=parent->left;
            if(rb_is_red(other))
            {
                rb_set_black(other);
                rb_set_red(parent);
                rightRotate(root,parent);
                other=parent->left;
            }
            if(rb_is_black(other->left)&&rb_is_black(other->right))
            {
                rb_set_red(other);
                node=parent;
                parent=rb_parent(node);
            }
            else
            {
                if(rb_is_black(other->left))
                {
                    rb_set_black(other->right);
                    rb_set_red(other);
                    leftRotate(root,other);
                    other=parent->left;
                }
                rb_set_color(other,rb_color(parent));
                rb_set_black(other->left);
                rb_set_black(parent);
                rightRotate(root,parent);
                node=root;
            }
        }
    }
    rb_set_black(node);
}

template<class T>
void RBTree<T>::remove(RBTNode<T> *&root,RBTNode<T> *node)
{
    RBTNode<T> *pnode,*rnode;
    if(node->left==NULL||node->right==NULL)
        pnode=node;
    else
        pnode=successor(node);
    if(pnode->left)
        rnode=pnode->left;
    else
        rnode=pnode->right;
    rnode->parent=pnode->parent;
    if(pnode->parent==NULL)
        root=rnode;
    else
    {
        if(pnode==pnode->parent->left)
            pnode->parent->left=rnode;
        else
            pnode->parent->right=rnode;
    }
    if(node!=pnode)
    {
        node->key=pnode->key;
    }
    if(pnode->color==BLACK)
        removeFixUp(root,rnode);
    delete pnode;
    return;
}

template<class T>
void RBTree<T>::remove(T key)
{
    RBTNode<T> *node;
    if((node=search(mRoot,key))!=NULL)
        remove(mRoot,node);
}

template<class T>
void RBTree<T>::destroy(RBTNode<T> *&tree)
{
    if(tree==NULL)
        return;
    if(tree->left!=NULL)
        return destroy(tree->left);
    if(tree->right!=NULL)
        return destroy(tree->left);
    delete tree;
    tree=NULL;
}

template <class T>
void RBTree<T>::destroy()
{
    destroy(mRoot);
}

template<class T>
void RBTree<T>::print(RBTNode<T> *tree,T key,int direction)
{
    if(tree!=NULL)
    {
        if(direction==0)
            cout<<setw(2)<<tree->key<<"(B) is root"<<endl;
        else
            cout << setw(2) << tree->key <<  (rb_is_red(tree)?"(R)":"(B)") << " is " << setw(2)
            << key << "'s "  << setw(12) << (direction==1?"right child" : "left child") << endl;
        print(tree->left, tree->key, -1);
        print(tree->right,tree->key,  1);
    }
}

template<class T>
void RBTree<T>::print()
{
    if(mRoot)
        print(mRoot,mRoot->key,0);
}
#endif // RBTREE_H_

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

You can do more

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值