红黑树的应用及增删改查

                                    红黑树的应用及增删改查

我们接触过数据结构的应该二叉树这种数据结构,他是对链表的改良版,防止每次遍历的最大的时间复杂度达到O(n)

理想情况下,二叉树的查找效率是O(log n),但是当数据倾斜的时候,数据是有序的插入到二叉树的时候,这时候二叉树

就退化成了链表,这时候便出现了AVL树,红黑树,B树来改良这种数据结构。今天我们来说说红黑树。

红黑树的应用:

红黑树在jdk1.8种用于hashmap的底层实现,当hashmap的数据<=8时,采用链表,当数据量>8时,用红黑树结构。

红黑树在C++的STL中的map和set。

红黑树在磁盘的文件管理中也有应用。

红黑树的特点:

(1)每个结点要么是红的要么是黑的。

(2)根结点是黑的。 

(3)每个叶结点(叶结点即指树尾端NIL指针或NULL结点)都是黑的。 

(4)如果一个结点是红的,那么它的两个儿子都是黑的。 

(5)对于任意结点而言,其到叶结点树尾端NIL指针的每条路径都包含相同数目的黑结点。

下图是一个红黑树,满足以上五个红黑树的特殊性质:

 

红黑树的增删改查:


public class RBTree<t extends="" t="">> {
     
    //二叉树定义
    private RBTNode<t> mRoot;    // 根结点
 
    private static final boolean RED   = false;
    private static final boolean BLACK = true;
 
    public class RBTNode<t extends="" t="">> {
        boolean color;        // 颜色
        T key;                // 关键字(键值)
        RBTNode<t> left;    // 左孩子
        RBTNode<t> right;    // 右孩子
        RBTNode<t> parent;    // 父结点
 
        public RBTNode(T key, boolean color, RBTNode<t> parent, RBTNode<t> left, RBTNode<t> right) {
            this.key = key;
            this.color = color;
            this.parent = parent;
            this.left = left;
            this.right = right;
        }
 
    }
   //二叉树左旋

 private void leftRotate(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) {
        this.mRoot = 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;
    }

    //二叉树右旋:
private void rightRotate(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) {
        this.mRoot = 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;
    }
   //二叉树插入:
    private void insert(RBTNode<t> node) {
    int cmp;
    RBTNode<t> y = null;
    RBTNode<t> x = this.mRoot;
 
    // 1. 将红黑树当作一颗二叉查找树,将节点添加到二叉查找树中。
    while (x != null) {
        y = x;
        cmp = node.key.compareTo(x.key);
        if (cmp < 0)
            x = x.left;
        else
            x = x.right;
    }
 
    node.parent = y;
    if (y!=null) {
        cmp = node.key.compareTo(y.key);
        if (cmp < 0)
            y.left = node;
        else
            y.right = node;
    } else {
        this.mRoot = node;
    }
 
    // 2. 设置节点的颜色为红色
    node.color = RED;
 
    // 3. 将它重新修正为一颗二叉查找树
    insertFixUp(node);
}
 
/* 
 * 新建结点(key),并将其插入到红黑树中
 *
 * 参数说明:
 *     key 插入结点的键值
 */
public void insert(T key) {
    RBTNode<t> node=new RBTNode<t>(key,BLACK,null,null,null);
 
    // 如果新建结点失败,则返回。
    if (node != null)
        insert(node);
    }
    //插入后重新调整为红黑树:
private void insertFixUp(RBTNode<t> node) {
    RBTNode<t> parent, gparent;
 
    // 若“父节点存在,并且父节点的颜色是红色”
    while (((parent = parentOf(node))!=null) && isRed(parent)) {
        gparent = parentOf(parent);
 
        //若“父节点”是“祖父节点的左孩子”
        if (parent == gparent.left) {
            // Case 1条件:叔叔节点是红色
            RBTNode<t> uncle = gparent.right;
            if ((uncle!=null) && isRed(uncle)) {
                setBlack(uncle);
                setBlack(parent);
                setRed(gparent);
                node = gparent;
                continue;
            }
 
            // Case 2条件:叔叔是黑色,且当前节点是右孩子
            if (parent.right == node) {
                RBTNode<t> tmp;
                leftRotate(parent);
                tmp = parent;
                parent = node;
                node = tmp;
            }
 
            // Case 3条件:叔叔是黑色,且当前节点是左孩子。
            setBlack(parent);
            setRed(gparent);
            rightRotate(gparent);
        } else {    //若“z的父节点”是“z的祖父节点的右孩子”
            // Case 1条件:叔叔节点是红色
            RBTNode<t> uncle = gparent.left;
            if ((uncle!=null) && isRed(uncle)) {
                setBlack(uncle);
                setBlack(parent);
                setRed(gparent);
                node = gparent;
                continue;
            }
 
            // Case 2条件:叔叔是黑色,且当前节点是左孩子
            if (parent.left == node) {
                RBTNode<t> tmp;
                rightRotate(parent);
                tmp = parent;
                parent = node;
                node = tmp;
            }
 
            // Case 3条件:叔叔是黑色,且当前节点是右孩子。
            setBlack(parent);
            setRed(gparent);
            leftRotate(gparent);
        }
    }
 
    // 将根节点设为黑色
    setBlack(this.mRoot);
    }
删除二叉树结点:
private void remove(RBTNode<t> node) {
    RBTNode<t> child, parent;
    boolean color;
 
    // 被删除节点的"左右孩子都不为空"的情况。
    if ( (node.left!=null) && (node.right!=null) ) {
        // 被删节点的后继节点。(称为"取代节点")
        // 用它来取代"被删节点"的位置,然后再将"被删节点"去掉。
        RBTNode<t> replace = node;
 
        // 获取后继节点
        replace = replace.right;
        while (replace.left != null)
            replace = replace.left;
 
        // "node节点"不是根节点(只有根节点不存在父节点)
        if (parentOf(node)!=null) {
            if (parentOf(node).left == node)
                parentOf(node).left = replace;
            else
                parentOf(node).right = replace;
        } else {
            // "node节点"是根节点,更新根节点。
            this.mRoot = replace;
        }
 
        // child是"取代节点"的右孩子,也是需要"调整的节点"。
        // "取代节点"肯定不存在左孩子!因为它是一个后继节点。
        child = replace.right;
        parent = parentOf(replace);
        // 保存"取代节点"的颜色
        color = colorOf(replace);
 
        // "被删除节点"是"它的后继节点的父节点"
        if (parent == node) {
            parent = replace;
        } else {
            // child不为空
            if (child!=null)
                setParent(child, parent);
            parent.left = child;
 
            replace.right = node.right;
            setParent(node.right, replace);
        }
 
        replace.parent = node.parent;
        replace.color = node.color;
        replace.left = node.left;
        node.left.parent = replace;
 
        if (color == BLACK)
            removeFixUp(child, parent);
 
        node = null;
        return ;
    }
 
    if (node.left !=null) {
        child = node.left;
    } else {
        child = node.right;
    }
 
    parent = node.parent;
    // 保存"取代节点"的颜色
    color = node.color;
 
    if (child!=null)
        child.parent = parent;
 
    // "node节点"不是根节点
    if (parent!=null) {
        if (parent.left == node)
            parent.left = child;
        else
            parent.right = child;
    } else {
        this.mRoot = child;
    }
 
    if (color == BLACK)
        removeFixUp(child, parent);
    node = null;
}
 
/* 
 * 删除结点(z),并返回被删除的结点
 *
 * 参数说明:
 *     tree 红黑树的根结点
 *     z 删除的结点
 */
public void remove(T key) {
    RBTNode<t> node; 
 
    if ((node = search(mRoot, key)) != null)
        remove(node);
}
//删除后重新调整为红黑树:
private void removeFixUp(RBTNode<t> node, RBTNode<t> parent) {
    RBTNode<t> other;
 
    while ((node==null || isBlack(node)) && (node != this.mRoot)) {
        if (parent.left == node) {
            other = parent.right;
            if (isRed(other)) {
                // Case 1: x的兄弟w是红色的  
                setBlack(other);
                setRed(parent);
                leftRotate(parent);
                other = parent.right;
            }
 
            if ((other.left==null || isBlack(other.left)) &&
                (other.right==null || isBlack(other.right))) {
                // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的  
                setRed(other);
                node = parent;
                parent = parentOf(node);
            } else {
 
                if (other.right==null || isBlack(other.right)) {
                    // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。  
                    setBlack(other.left);
                    setRed(other);
                    rightRotate(other);
                    other = parent.right;
                }
                // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
                setColor(other, colorOf(parent));
                setBlack(parent);
                setBlack(other.right);
                leftRotate(parent);
                node = this.mRoot;
                break;
            }
        } else {
 
            other = parent.left;
            if (isRed(other)) {
                // Case 1: x的兄弟w是红色的  
                setBlack(other);
                setRed(parent);
                rightRotate(parent);
                other = parent.left;
            }
 
            if ((other.left==null || isBlack(other.left)) &&
                (other.right==null || isBlack(other.right))) {
                // Case 2: x的兄弟w是黑色,且w的俩个孩子也都是黑色的  
                setRed(other);
                node = parent;
                parent = parentOf(node);
            } else {
 
                if (other.left==null || isBlack(other.left)) {
                    // Case 3: x的兄弟w是黑色的,并且w的左孩子是红色,右孩子为黑色。  
                    setBlack(other.right);
                    setRed(other);
                    leftRotate(other);
                    other = parent.left;
                }
 
                // Case 4: x的兄弟w是黑色的;并且w的右孩子是红色的,左孩子任意颜色。
                setColor(other, colorOf(parent));
                setBlack(parent);
                setBlack(other.left);
                rightRotate(parent);
                node = this.mRoot;
                break;
            }
        }
    }
 
    if (node!=null)
        setBlack(node);
    }
}

笔者上述java代码来自博文:java红黑树实现原理及实现源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值