我正在学习Left Leaning Red Black Trees。
在本文提出的删除算法中,如果密钥与节点匹配且该节点的右子树为NULL,则删除该节点。但是可能还有一个左子树也没有被考虑过。
我无法理解为什么左子树也是NULL 。删除最小值或最大值时也会执行类似的操作。有人可以指导我吗?
答案 0 :(得分:3)
您似乎在谈论这段代码:
if (isRed(h.left))
h = rotateRight(h);
if (key.compareTo(h.key) == 0 && (h.right == null))
return null;
此处左侧后代不能为“红色”,因为前面的代码会将其向右旋转。
左后代也不能是“黑色”,因为在这种情况下,h
的左边有一条路径,其中包含至少一个“黑色”节点,而右边的路径没有任何“黑色”节点。但在RB树中,每条路径上的黑色节点数必须相同。
这意味着根本没有左后代,节点h
是叶子节点。
在deleteMin
函数中,如果左子树为空,则无需检查右子树,因为LLRB树的右子树不能大于相应的左子树。
答案 1 :(得分:-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.