World's most popular travel blog for travel bloggers.

[Solved]: Why should leaf nodes in a red-black tree be black?

, , No Comments
Problem Detail: 

From the property of Red-Black Trees we know that:

  • All leaves (NIL) are black. (All leaves are same color as the root.)(Comren et al "Introduction to Algorithms")

An example of a red–black tree. From Wikipedia

But what is the reason that we should enforce them as Black, even though they're NILL's?

Asked By : Sayat Stb

Answered By : user1218927

Take a uncolored leaf node, now you can color it as either red or black. If you colored it as red then you may have chance that your immediate ancestor is also red which is contradicting(according to basic principle). If you color it as black then no problem even though the immediate ancestor is red. And also no change in the number of black nodes from root to leaf paths(i.e every path get +1). This may be the possible reason behind that.

Best Answer from StackOverflow

Question Source :

3.2K people like this

 Download Related Notes/Documents


Post a Comment

Let us know your responses and feedback