Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Red Black Tree inserting: why make nodes red when inserted?

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)?

like image 907
Indika Anu Avatar asked Mar 16 '13 16:03

Indika Anu


People also ask

Why new node is red in red-black tree?

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 a new node is inserted in a red-black tree it is?

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.

Does a red-black tree need red nodes?

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.

Would inserting a new node to a red-black tree and then immediately deleting it change the tree?

Deleting the same node immediately after inserting will not always result in the original tree.


2 Answers

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!

like image 192
templatetypedef Avatar answered Oct 04 '22 19:10

templatetypedef


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.

like image 41
Knoothe Avatar answered Oct 04 '22 18:10

Knoothe