[英]Converting 2-3-4 to Red-Black Tree
I am familiar with converting individual 2-node, 3-node, and 4-nodes straight to Red-Black trees.我熟悉将单个 2 节点、3 节点和 4 节点直接转换为红黑树。 And this Stackoverflow link is a good explanation 2-3-4 to Red-Black .
这个 Stackoverflow 链接是对 Red-Black 的一个很好的解释 2-3-4 。 However, I have a question about the example given in that link.
但是,我对该链接中给出的示例有疑问。 This is how the Stackoverflow question 2-3-4 to red-black was illustrated 2-3-4 to Red-Black
这就是 Stackoverflow 问题 2-3-4 to red-black 的说明2-3-4 to Red-Black
I highlighted the part that I am questioning.我强调了我所质疑的部分。 Why is it on this guide 4-node connected to 2-node I found and others on the internet, they say when encountering a 4 node connected to a 2 or 3 node, you need to switch the colors around.
为什么在这个指南上我找到了4 节点连接到 2 节点和互联网上的其他人,他们说当遇到连接到 2 或 3 节点的 4 节点时,你需要切换 colors。 But in the StackOverflow example that I highlighted red, they didn't.
但是在我突出显示红色的 StackOverflow 示例中,它们没有。 Thanks
谢谢
The following image does not imply that the colors of the red-back tree need to be swapped:下图并不表示红背树的colors需要交换:
It merely describes the process of splitting a B-tree node, which leads to an alternative B-tree for the same data.它仅描述了拆分 B 树节点的过程,这导致相同数据的替代B 树。
Then it shows how that different shape of the B-tree leads to a different coloring in the corresponding red-black tree, and how that new coloring is also a valid alternative.然后它显示了 B 树的不同形状如何导致相应红黑树中的不同颜色,以及新颜色如何也是有效的替代方案。
But the translation from B-tree to red-black tree follows the rules you referred to:但是从 B 树到红黑树的转换遵循您提到的规则:
If we look at the left side of the image, we see at the bottom layer of the B-tree a 4-node.如果我们查看图像的左侧,我们会在 B 树的底层看到一个 4 节点。 According to the rules, this translates to a black node (c) with two red children (b and d).
根据规则,这将转换为具有两个红色子节点(b 和 d)的黑色节点 (c)。 The 2-node at the root translates to a black node (a).
根处的 2 节点转换为黑色节点 (a)。
If we look at the right side of the image, we see two 2-nodes at the bottom layer of the B-tree.如果我们查看图像的右侧,我们会在 B 树的底层看到两个 2 节点。 These translate each to a back node (b and d).
这些将每个转换为一个后节点(b 和 d)。 The root 3-node is translated to a back node (a) with a red node (c) as child.
根 3 节点被转换为以红色节点 (c) 作为子节点的后节点 (a)。
This is exactly what is depicted at the bottom of the image.这正是图像底部所描绘的。 The point is that these two variants are valid red-black trees for the same data, but derived from different shapes of B-trees.
关键是这两个变体对于相同的数据是有效的红黑树,但源自不同形状的 B 树。
Such a transition from the left to the right version might be needed when inserting a node.插入节点时可能需要这种从左到右版本的转换。 For instance, if an "e" would be added, then it cannot be added as a child of the "d" node in the red-black tree without recoloring.
例如,如果要添加一个“e”,那么它不能在不重新着色的情况下添加为红黑树中“d”节点的子节点。 By switching to the right-side version of the red-black tree, the node can be added as (red) child of node "d".
通过切换到红黑树的右侧版本,可以将节点添加为节点“d”的(红色)子节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.