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

二次信任 提交于 2019-12-06 07:45:23

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.

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.

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