Is it ever possible for a red parent node to have just ONE black child node? I have been playing around with the Red/Black Tree simulator online and I can't manage to get a case of this to happen.
The reason behind asking this is I believe I have an unnecessary IF in my code...
if (temp_node->color == BLACK && node->color == RED)
{
node->color = BLACK;
global_violation = false;
}
Thanks for any Feedback!!
No, this isn't possible.
Remember, that in a red/black tree, all paths from the root of the tree off of the tree must pass through the same number of black nodes (that's one of the red/black tree invariants).
If you have a red node x
with one black child y
, it cannot have another red child (since that breaks the red/black invariant that red nodes can't have red children).
This means that a path through x
to the missing child will pass through at least one fewer black node than the path through x
, then to y
, and then off the tree from there, breaking the red/black tree invariants.
来源:https://stackoverflow.com/questions/35262353/red-black-tree-1-child-deletes