1

I am studying red black trees from CLRS. I have 2 questions about the part where properties of red-black trees are discussed. The passage from CLRS is as follows:

A red-black tree is a binary tree that satisfies the following red-black properties:

  1. Every node is either red or black

  2. The root is black

  3. Every leaf(NIL) is black

  4. If a node is red, then both its children are black

  5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes

First of all, it says a red-black tree is a binary tree. Why didn't they say a red-black tree is a binary search tree. I thought the whole purpose of a red-black tree is to maintain the balance in a search tree to insure logN operations. Second, why is the leaf in a red-black tree the NIL?

In regular binary trees, we're used to this:

               4
         5            7
    3        2     Nil Nil
 Nil Nil  Nil Nil

In this case, 3, 2, and 7 are the leaves. Why are the leaves depicted as the Nil's in CLRS?

Ralph
  • 2,959
  • 9
  • 26
  • 49

1 Answers1

1

In the book, they refer to a red-black tree as a binary search tree, it's right at the beginning of the chapter.

Also in the chapter, they indicate that nodes labeled as nilare actual nodes (called sentinels), with the same fields as an ordinary node but with a color field with fixed value "black" (the other fields can be set to arbitrary values); these nodes always appear as leafs in the tree. So it's different from the usual BST where a leaf is a node whose left subtree and right subtree are both nil.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Then why does Cormen and co refer to the leaf as Nil in their description of red black trees properties? – Ralph Nov 08 '15 at 00:39
  • @Ralphyabro I went for my copy of CLRS and corrected my answer, see above. – Óscar López Nov 08 '15 at 00:45
  • Thanks! So the concept of the leaves having two nulls does not exist in red black trees. What terminates a simple path would be a NIL node which is always black I suppose and contains no value? Am I right? – Ralph Nov 08 '15 at 00:49
  • Yes, it depends on how you fill the fields, as mentioned in the book, it's an arbitrary value. You can even have a singleton `nil` node and test for it using an identity comparison. – Óscar López Nov 08 '15 at 00:50
  • @ÓscarLópez so when it says a red-black tree of `n` nodes does it include `NIL` nodes? –  Jun 10 '18 at 08:38
  • 1
    No, NIL nodes are not included when counting the number of nodes in a tree – Óscar López Jun 10 '18 at 08:45
  • Thanks for the quick reply –  Jun 10 '18 at 11:59