In Red Black Tree Properties doesn't contain any rule for insert node color, how ever always we are inserting red color nodes.
If we insert black color node does it violate any Properties( any rule out of 4)?
This is because, if the new red node is attached to a black one, no rule is broken. It doesn't create a situation in which there are two red nodes together which breaks rule of red parent black children, and it doesn't change the black height(number of black nodes from root to leaf) in any of the paths.
When the first element is inserted it is inserted as a root node and as root node has black colour so it acquires the colour black. The new element is always inserted with a red colour and as 21 > 3 so it becomes the part of the right subtree of the root node.
A quick glance at the properties of a red-black tree shows that there is no requirement for any node to be red. The only way red nodes come about is through property 5: Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
Deleting the same node immediately after inserting will not always result in the original tree.
Yep! Suppose you have a single node in your tree:
5 (black)
Now insert a new black node into the tree:
5 (black)
\
9 (black)
Now the invariant that every root-null path in the tree has the same number of black nodes is broken, since the path from the root left has one black node and the paths from the root right each have two.
Hope this helps!
You seem to be asking two questions
1) Why make nodes red when inserted (in title)?
2) Does inserting as black violate any properies?
You also seem to be under the mistaken impression that a Yes answer for 2) is an automatic justification for 1).
It is not so! Inserting a node as red also can violate one of the RB Tree properties. For instance, if you make the red node a child of another red node, you have just violated the property that red nodes can only have black children.
The reason for making it red, is that it is 'easier' to fix up the child node properties (by rotations and repainting parent/grandparents), rather than trying to fix up the path length properties. Perhaps another reason is that, that is what the authors came up with.
It might also be possible to fix up the tree by inserting a black node and not repainting red. Perhaps no one has given thought to it yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With