【问题标题】:deleteMin Left Leaning Red Black Tree needs more explanationdeleteMin 左倾红黑树需要更多解释
【发布时间】:2020-09-19 20:41:18
【问题描述】:

我正在阅读 Robert Sedgewick 的《算法第 4 版中的左倾红黑树》。我花了几天时间试图理解 deleteMin 作为理解 delete 的热身,这是我关于 deleteMin 的最后一个问题。

    public void deleteMin()
    {
        root = deleteMin(root);
        root.color = BLACK;
    }
    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 fixUp(h);
    }

当 h.left 和 h.left.left 都是黑色时,调用 h=moveRedLeft(h);
问题是,我们如何断言节点 b 是红色,如图所示?

【问题讨论】:

    标签: algorithm red-black-tree


    【解决方案1】:

    问题是how can we assert that the node b is red?

    答案很简单we don't。在moveRedLeft 中,我们只检查条件。

    参考论文here,第7页,我们有以下代码:

    private Node moveRedLeft(Node h)
    {
      colorFlip(h);
      if (isRed(h.right.left))
      {
        h.right = rotateRight(h.right);
        h = rotateLeft(h);
        colorFlip(h);
      }
      return h;
    }
    

    显然,第 4 行检查 b 是否为红色。

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 2021-05-22
      • 2016-09-26
      • 1970-01-01
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      • 2013-07-12
      相关资源
      最近更新 更多