【问题标题】:Deletion in Left Leaning Red Black Trees左倾红黑树的删除
【发布时间】:2012-11-01 20:38:10
【问题描述】:

我正在了解Left Leaning Red Black Trees

在论文中提出的删除算法中,如果一个节点的键匹配并且该节点的右子树为NULL,则该节点被删除。但是也可能有一个左子树没有被考虑。

我无法理解为什么左子树也会为 NULL。删除最小值或最大值时也会执行类似的操作。有人可以指导我吗?

【问题讨论】:

    标签: data-structures binary-search-tree red-black-tree


    【解决方案1】:

    看来你说的是这段代码:

    if (isRed(h.left))
      h = rotateRight(h);
    if (key.compareTo(h.key) == 0 && (h.right == null))
      return null;
    

    这里的左后代不能是“红色”,因为前面的代码会将它向右旋转。

    同样,左后代不能是“黑色”,因为在这种情况下,h 左侧的路径至少包含一个“黑色”节点,而其右侧的路径没有任何“黑色”节点。但是在RB-tree中,每条路径上的黑色节点的数量必须相同。

    这意味着根本没有左后代,节点h是一个叶子节点。

    deleteMin函数中,如果左子树为空,则无需检查右子树,因为LLRB树的右子树不能大于对应的左子树。

    【讨论】:

    【解决方案2】:

    关于左倾红黑树是否真的比以前的实现更好甚至更简单,有一个有趣的分析。文章Left-Leaning Red Black Trees Considered Harmful 由Harvard Comp Sci 教授Eddie Kohler 撰写。他写道:

    Tricky writing
    
    Sedgewick’s paper is tricky. As of 2013, the insert section presents 2–3–4 trees as 
    the default and describes 2–3 trees as a variant. The delete implementation, however,
    only works for 2–3 trees. If you implement the default variant of insert and the   
    only variant of delete,your tree won’t work. The text doesn’t highlight the switch 
    from   2–3–4 to 2–3: not kind.
    

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 2016-09-26
      • 2011-08-23
      • 2020-09-19
      • 2016-08-03
      • 2021-01-03
      • 2014-07-05
      • 2013-11-11
      相关资源
      最近更新 更多