1

I know there's another question asking the same in this website, but answers to that question was not clear; it did not help me.

In this wikipedia page, the 2nd property says "All NIL nodes are considered black".

I want to know why that constraint exists.

I could think (but not sure) that the contradiction (if NIL nodes were allowed to be any color) would arise in one of these two ways:

  1. There exists a certain tree that do not conform atleast one rule out of the other 3 rules (rule 1, 3 and 4 from this page).
  2. There exists a certain tree whose ratio between the the height of the farthest leaf node to the nearest leaf node would be more than 2.

Can you give an example that would result in such contradiction?

Cinverse
  • 113
  • 2

1 Answers1

2

It is not necessary for the NIL nodes to be black however making that choice simplifies the understanding of the insertion and deletion algorithm.

To understand why it is not necessary, consider a red-black tree satisfying the conditions 1, 3 and 4, but not necessarily satisfying the condition 2.

If you now consider the NIL nodes to be real nodes, each with two new dummy NIL nodes colored in black, then the new tree satisfies all 4 conditions.

As to why the choice is made to color those NIL in black, it is because the NIL nodes are often not represented in many implementations, for example, they could be None in Python or NULL pointers in C, and attributing a color to those would require to create a specific data structure.

It is explained in the wikipedia article:

It is not really necessary to attribute a "color" to these end-of-path objects, because the condition "is NIL or BLACK" is implied by the condition "is NIL"

As a remark, some definitions of red-black tree require for the root to be black, and it is also not necessary, and only to simplify some algorithms.

Nathaniel
  • 18,309
  • 2
  • 30
  • 58