左倾红黑树(LLRBT)删除操作及相关性质总结&答疑

Left-leaning Red Black Tree
看算法4(算法 第4版 Algorithms 4th Edition)3.4节时,后面的习题有实现左倾红黑树删除操作的代码,刚开始看得云里雾里的,后来查了不少资料慢慢理解了,发现还是对LLRBT的性质认识得不够深刻,如果认识深刻的话还是可以理解代码的。今天做一个总结,希望能帮到遇到类似问题的朋友,如有错误,还望大家指出。

  1. 总是完美黑色平衡,且所有右链接都是黑链接,只有左链接有可能是红链接。
  2. 旋转操作(rotate)不会影响平衡。
  3. 转换颜色操作(flipColors)不会影响平衡(前提是左右子节点同色,而根节点与子节点们异色)。
    转换颜色:
  4. moveRedLeft往左添红,删除最小节点或当前节点大于需删除节点时使用。
  5. moveRedRight往右添红,删除最大节点或当前节点小于需删除节点时使用。
  6. moveRedLeft和moveRedRight的代码实现中,也只是在不同条件下,使用了不同的旋转操作或者转换颜色操作,所以也不影响平衡。
  7. 当需要删除一个节点时,如果这个节点刚好是红节点(指向它的是红链接),那就直接删除1即可(红节点不影响平衡),但是这是理想状况,现实不可能是理想的,再但是!我们可以追求理想!
  8. 为了追求7中的理想,也就是当前节点永远是红节点,LLRBT的删除操作中有个隐藏约束,那就是确保当前节点或者当前节点的左子节点是红节点,为什么当前节点的左子节点是红节点也行呢,因为这时候我们不是能通过右旋操作把当前节点变成红节点嘛,再为什么是左子节点而不是右子节点呢,因为LLRBT只有左子节点可能是红啊。
  9. 做到8,基本就能达到7中我们所追求的理想状态了。但是又有,如果从root出我们开始时当前节点(此时就是root节点)和它的左子节点都不是红的怎么办(此时的隐藏信息是右子节点也不为红,因为LLRBT所有右链接都为黑),那我们就直接把根节点改为红!(不影响平衡,只是黑高减了1)
  10. 至此,我们就把现实变成了理想的样子。遇到待删除节点,就可以直接(zhanzhe)把它(qian)删除(zhengle)。 2

下面附上涉及到的代码

	// make a left-leaning link lean to the right
    private Node rotateRight(Node h) {
        assert (h != null) && isRed(h.left);
        // assert (h != null) && isRed(h.left) &&  !isRed(h.right);  // for insertion only
        Node x = h.left;
        h.left = x.right;
        x.right = h;
        x.color = x.right.color;
        x.right.color = RED;
        x.size = h.size;
        h.size = size(h.left) + size(h.right) + 1;
        return x;
    }

    // make a right-leaning link lean to the left
    private Node rotateLeft(Node h) {
        assert (h != null) && isRed(h.right);
        // assert (h != null) && isRed(h.right) && !isRed(h.left);  // for insertion only
        Node x = h.right;
        h.right = x.left;
        x.left = h;
        x.color = x.left.color;
        x.left.color = RED;
        x.size = h.size;
        h.size = size(h.left) + size(h.right) + 1;
        return x;
    }

    // flip the colors of a node and its two children
    private void flipColors(Node h) {
        // h must have opposite color of its two children
        // assert (h != null) && (h.left != null) && (h.right != null);
        // assert (!isRed(h) &&  isRed(h.left) &&  isRed(h.right))
        //    || (isRed(h)  && !isRed(h.left) && !isRed(h.right));
        h.color = !h.color;
        h.left.color = !h.left.color;
        h.right.color = !h.right.color;
    }
	/**
     * Removes the smallest key and associated value from the symbol table.
     *
     * @throws NoSuchElementException if the symbol table is empty
     */
    public void deleteMin() {
        if (isEmpty()) throw new NoSuchElementException("BST underflow");

        // if both children of root are black, set root to red
        if (!isRed(root.left) && !isRed(root.right))
            root.color = RED;

        root = deleteMin(root);
        if (!isEmpty()) root.color = BLACK;
        // assert check();
    }
	// delete the key-value pair with the minimum key rooted at h
    private Node deleteMin(Node h) {
        if (h.left == null)
            return null;

        if (!isRed(h.left) && !isRed(h.left.left))
            h = moveRedLeft(h);

        h.left = deleteMin(h.left);
        return balance(h);
    }
    // Assuming that h is red and both h.left and h.left.left
    // are black, make h.left or one of its children red.
    private Node moveRedLeft(Node h) {
        // assert (h != null);
        // assert isRed(h) && !isRed(h.left) && !isRed(h.left.left);

        flipColors(h);
        if (isRed(h.right.left)) {
            h.right = rotateRight(h.right);
            h = rotateLeft(h);
            flipColors(h);
        }
        return h;
    }
    /**
     * Removes the largest key and associated value from the symbol table.
     *
     * @throws NoSuchElementException if the symbol table is empty
     */
    public void deleteMax() {
        if (isEmpty()) throw new NoSuchElementException("BST underflow");

        // if both children of root are black, set root to red
        if (!isRed(root.left) && !isRed(root.right))
            root.color = RED;

        root = deleteMax(root);
        if (!isEmpty()) root.color = BLACK;
        // assert check();
    }

    // delete the key-value pair with the maximum key rooted at h
    private Node deleteMax(Node h) {
        if (isRed(h.left))
            h = rotateRight(h);

        if (h.right == null)
            return null;

        if (!isRed(h.right) && !isRed(h.right.left))
            h = moveRedRight(h);

        h.right = deleteMax(h.right);

        return balance(h);
    }
    // Assuming that h is red and both h.right and h.right.left
    // are black, make h.right or one of its children red.
    private Node moveRedRight(Node h) {
        // assert (h != null);
        // assert isRed(h) && !isRed(h.right) && !isRed(h.right.left);
        flipColors(h);
        if (isRed(h.left.left)) {
            h = rotateRight(h);
            flipColors(h);
        }
        return h;
    }
    /**
     * Removes the specified key and its associated value from this symbol table
     * (if the key is in this symbol table).
     *
     * @param key the key
     * @throws IllegalArgumentException if {@code key} is {@code null}
     */
    public void delete(Key key) {
        if (key == null) throw new IllegalArgumentException("argument to delete() is null");
        if (!contains(key)) return;

        // if both children of root are black, set root to red
        if (!isRed(root.left) && !isRed(root.right))
            root.color = RED;

        root = delete(root, key);
        if (!isEmpty()) root.color = BLACK;
        // assert check();
    }

    // delete the key-value pair with the given key rooted at h
    private Node delete(Node h, Key key) {
        // assert get(h, key) != null;

        if (key.compareTo(h.key) < 0) {
            if (!isRed(h.left) && !isRed(h.left.left))
                h = moveRedLeft(h);
            h.left = delete(h.left, key);
        } else {
            if (isRed(h.left))
                h = rotateRight(h);
            if (key.compareTo(h.key) == 0 && (h.right == null))
                return null;
            if (!isRed(h.right) && !isRed(h.right.left))
                h = moveRedRight(h);
            if (key.compareTo(h.key) == 0) {
                Node x = min(h.right);
                h.key = x.key;
                h.val = x.val;
                // h.val = get(h.right, min(h.right).key);
                // h.key = min(h.right).key;
                h.right = deleteMin(h.right);
            } else h.right = delete(h.right, key);
        }
        return balance(h);
    }

  1. 关于二叉树的通用删除操作:如果待删除节点是叶子节点:直接删除;如果待删除节点有一个自己点:删除,并用它的子节点代替它,此时待删除元素的这唯一一个子节点其实就是它的前驱(左)或者后继(右)节点;如果待删除节点有两个子节点:删除,并用它的后继节点(以待删除节点的右子树为根的子树的最小节点)代替它。 ↩︎

  2. 收尾工作:要把root,再平衡(blance)一下,平衡完后root有可能为红,得把root改为黑。 ↩︎

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值