问题
The rules for a Red-Black Tree:
- Every node is either red or black.
- The root is black.
- Every leaf (NIL) is black.
- If a node is red, then both its children are black.
- 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