Can a red node have just 1 black child in a red-black tree?

痞子三分冷 提交于 2019-12-10 11:31:51

问题


The rules for a Red-Black Tree:

  1. Every node is either red or black.
  2. The root is black.
  3. Every leaf (NIL) is black.
  4. If a node is red, then both its children are black.
  5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

Rule 4 mentions that red nodes need both black childs but what if there is just one child to begin with? Is there an argument to prove or disprove this?


回答1:


No,a red node cannot have one child,consider the following cases:- 1.If the single child it has is red...this cannot happen because no two consecutive nodes can be red. 2.If the child is black...again this cannot happen because this would violate the 'Black Height Rule'...this case would give one extra black node in a path which is not correct according to the rule.




回答2:


Yes, it is possible. Following is a case wherein you CAN have a red node with a single black child without breaking any rules:

Step 1) Insert: 10, 7, 40, 3, 8, 30, 45, 1, 5, 20, 35, 60, 25. Following shows the red-black tree BEFORE deletion of node 30:

Step 2) Delete 30 (a degree 2 red node). Following shows the red-black tree AFTER deletion of node 30:

Assuming replacement strategy as least in the right subtree, node that replaces 30 is 35.

Now, when you do replace a node, I believe the replacement node will inherit the color of the replaced node as well. This means, 35 is red.

This yields a tree wherein, 35 is a red node with a single black child.



来源:https://stackoverflow.com/questions/35546108/can-a-red-node-have-just-1-black-child-in-a-red-black-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!