15

It seems the definition on wiki is not precise:

http://en.wikipedia.org/wiki/Red-black_tree#Properties

Is a tree with all black nodes a red black tree?

UPDATE

With the definition of rbtree not so strict,how do we decide whether to print the children of a black node as red or black?

cpuer
  • 7,413
  • 14
  • 35
  • 39

7 Answers7

10

Yes, a tree with all nodes black can be a red-black tree. The tree has to be a perfect binary tree (all leaves are at the same depth or same level, and in which every parent has two children) and so, it is the only tree whose Black height equals to its tree height.

Community
  • 1
  • 1
Suriya Chaudary
  • 136
  • 1
  • 5
10

A red-black tree is simply a binary-tree representation of a 2-3-4 tree. Any red node in a red-black tree corresponds to a piece of its parent node in the analagous 2-3-4 tree. For example:

           [black 5]
          /         \
      [red 3]     [black 6]
     /       \
[black 2] [black 4]

is a representation of the 2-3-4 tree

    [3 | 5]
   /   |   \
 [2]  [4]  [6]

If a red-black tree has only black nodes, that means it represents a 2-3-4 tree with only 2-nodes (single entries), not 3-nodes (such as [3 | 5] in the example) or 4-nodes. Notice this is basically just a plain binary search tree.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 5
    Side-trivia: a node like `[3 | 5]` isn't called a 3-node because it has 3 elements (which it doesn't), but rather because it can have three children. – jtbandes Jun 20 '11 at 03:59
  • @jtbandes,how do we decide whether to print the children of a black node as red or black? – cpuer Jun 20 '11 at 05:34
  • @cpuer: if a black node's children are red, that means they're actually part of the same 2-3-4 node. I had `[red 3]` because it is actually `[3 | 5]` in the 2-3-4 version. – jtbandes Jun 20 '11 at 05:36
  • @jtbandes,your version is totally different from the wiki page,why is it not mentioned at all? – cpuer Jun 20 '11 at 05:39
  • It is mentioned briefly: "For every 2-4 tree, there are corresponding red–black trees with data elements in the same order. The insertion and deletion operations on 2-4 trees are also equivalent to color-flipping and rotations in red–black trees. This makes 2-4 trees an important tool for understanding the logic behind red–black trees, and this is why many introductory algorithm texts introduce 2-4 trees just before red–black trees, even though 2-4 trees are not often used in practice." (Note a 2-4 tree is the same as a 2-3-4 tree) – jtbandes Jun 20 '11 at 05:42
  • This is just the way I learned it in my computer science class last semester. It makes more sense than trying to remember the rules for red-black trees alone. – jtbandes Jun 20 '11 at 05:43
  • @jtbandes,but practically,how do we decide whether the children of a black node should be red or black?? – cpuer Jun 20 '11 at 05:48
  • Read the section [Operations](http://en.wikipedia.org/wiki/Red-black_tree#Operations) in the Wikipedia article. – jtbandes Jun 20 '11 at 05:51
  • @jtbandes,the operation to whether to printthe children of a black node as red or black can NOT be inferenced from the 5 properties above,right? – cpuer Jun 20 '11 at 05:53
  • Yes it can theoretically. When performing an insertion or removal, you just have to ensure that the properties are still true. What do you mean by "whether to print.."? Each node should know its color. – jtbandes Jun 20 '11 at 05:56
  • @jtbandes ,it seems to me that it always hold true whether we print the children of a black node as red or black. – cpuer Jun 20 '11 at 05:57
  • @jtbandes,so you mean when using rbtree,the color is already known in advance? So we just need to find a position to insert the new node? – cpuer Jun 20 '11 at 05:58
  • No, the new node's color depends where you insert it, and sometimes you have to change the color of other nodes too. The Operations section of the article and the other answers here explain how to decide. – jtbandes Jun 20 '11 at 06:00
3

Here is an example to show that all nodes of a red-black tree are black:

First, insert {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} in increasing order into red-black tree. Then, delete {10, 9, 8} in decreasing order from the red-black tree.

Finally all nodes of this red-black tree are black.

A red-black tree whose nodes are all black is the same as the B- tree (m=4) whose nodes all have only one key.

shudeng wu
  • 141
  • 1
  • 6
3

It is possible to have a proper red-black tree that has all black nodes. Trivially, a RBTree with only one node, or with the only leaf nodes being direct children of the root, will be all back nodes.

jwismar
  • 12,164
  • 3
  • 32
  • 44
  • so the definition of rbtree is not strict,how do you define your exact rbtree? – cpuer Jun 20 '11 at 03:57
  • @cpuer - I don't understand your question. A single-node tree (only a root) must be black. If the root has children that are leaves, those leaves must be black. It's only interior nodes that can end up being red - specifically, every node you add is colored red, until you determine whether it needs to be repainted. – jwismar Jun 20 '11 at 04:02
  • **every node you add is colored red, until you determine whether it needs to be repainted**,why is this not mentioned in the wiki page? – cpuer Jun 20 '11 at 05:19
  • @cpuer: It's under "Operations: Insertion". – jwismar Jun 21 '11 at 02:03
2

To answer the second part of the question, about deciding whether to print a node as red or black, that information is stored in each node.

In a typical binary search tree, each node contains a value, a left pointer, and a right pointer (and maybe a parent pointer). In a red-black tree, each node contains all those things plus an extra field indicating whether this node is red or black. The various operations on the tree, such as insert or delete, are then responsible for maintaining this color information in a consistent fashion.

You would never be given an uncolored tree and told to choose colors for the nodes (except perhaps as a homework or exam question).

Chris Okasaki
  • 4,875
  • 1
  • 20
  • 19
  • 2
    To amplify, because I think this is where the original poster's misconception is, nodes *always* have a color, not just at print time. Those colors can change as part of the insert/delete operations, but a node color is always either red or black. The red-ness and black-ness are not just cosmetic, they are an integral part of how the data structure maintains its balance properties. – Novak Feb 29 '12 at 19:20
1

Yes a tree with all black nodes can be a red black tree. It can be proved that such tree has to be a completely filled tree in order to preserve the equal black depth property.

You can yourself prove that a tree with all black nodes can be a red black tree by buildind a small tree of that kind. For example:

                            2,black
                      1,black      3,black 

This tree has all black nodes and it satisfies all the conditions. Assume that root has nil as its parent and both leaf nodes have both their children as nil.Hope this helps.

Rampal Chaudhary
  • 707
  • 1
  • 8
  • 18
0

Yes, but the same is not true for a red-black tree with all red nodes. Such a tree is invalid. There are restrictions on which nodes have to be black. For example, leaf nodes have to be black, and the children of a red node both have to be black.

Alan Turing
  • 12,223
  • 16
  • 74
  • 116