next up previous
Next: About this document Up: My Home Page

Red-Black Trees
Lecture 25

Steven S. Skiena

Red-Black Tree Definition

Red-black trees have the following properties:

  1. Every node is colored either red or black.
  2. Every leaf (NIL pointer) is black.
  3. If a node is red then both its children are black.
  4. Every single path from a node to a decendant leaf contains the same number of black nodes.

What does this mean?

If the root of a red-black tree is black can we just color it red?

No! For one of its children might be red.


If an arbitrary node is red can we color it black?

No! Because now all nodes may not have the same black height.

What tree maximizes the number of nodes in a tree of black height h?


What does a red-black tree with two real nodes look like?

Not (1) - consecutive reds Not (2), (4) - Non-Uniform black height

Red-Black Tree Height

Lemma: A red-black tree with n internal nodes has height at most tex2html_wrap_inline286 .

Proof: Our strategy; first we bound the number of nodes in any subtree, then we bound the height of any subtree.

We claim that any subtree rooted at x has at least tex2html_wrap_inline290 - 1 internal nodes, where bh(x) is the black height of node x.

Proof, by induction:

displaymath278

Now assume it is true for all tree with black height < bh(x).

If x is black, both subtrees have black height bh(x)-1. If x is red, the subtrees have black height bh(x).

Therefore, the number of internal nodes in any subtree is

displaymath279

Now, let h be the height of our red-black tree. At least half the nodes on any single path from root to leaf must be black if we ignore the root.

Thus tex2html_wrap_inline308 and tex2html_wrap_inline310 , so tex2html_wrap_inline312 .

This implies that tex2html_wrap_inline314 ,so tex2html_wrap_inline316 . height6pt width4pt

Therefore red-black trees have height at most twice optimal. We have a balanced search tree if we can maintain the red-black tree structure under insertion and deletion.

Rotations

The basic restructuring step for binary search trees are left and right rotation:

  1. Rotation is a local operation changing O(1) pointers.
  2. An in-order search tree before a rotation stays an in-order search tree.
  3. In a rotation, one subtree gets one level closer to the root and one subtree one level further from the root.

Rotation Implementation

    PROCEDURE RR (VAR root: BinTreeRep.NodeT) =
      (*simple rotation right*)
      VAR left:= root.left;
      BEGIN
        root.left:= left.right;
        left.right:= root;
        root:= left;
      END RR;
 
    PROCEDURE RL (VAR root: BinTreeRep.NodeT) =
      (*simple rotation left*)
      VAR right:= root.right;
      BEGIN
        root.right:= right.left;
        right.left:= root;
        root:= right;
      END RL;

Note the in-order property is preserved.

Red-Black Insertion

Since red-black trees have tex2html_wrap_inline320 height, if we can preserve all properties of such trees under insertion/deletion, we have a balanced tree!

Suppose we just did a regular insertion. Under what conditions does it stay a red-black tree?

Since every insertion take places at a leaf, we will change a black NIL pointer to a node with two black NIL pointers.

To preserve the black height of the tree, the new node must be red. If its new parent is black, we can stop, otherwise we must restructure! How can we fix two reds in a row?

It depends upon our uncle's color:

If our uncle is red, reversing our relatives' color either solves the problem or pushes it higher!

Note that after the recoloring:

  1. The black height is unchanged.
  2. The shape of the tree is unchanged.
  3. We are done if our great-grandparent is black.

If we get all the way to the root, recall we can always color a red-black tree's root black. We always will, so initially it was black, and so this process terminates.

The Case of the Black Uncle

If our uncle was black, observe that all the nodes around us have to be black:

Solution - rotate right about B:

Since the root of the subtree is now black with the same black-height as before, we have restored the colors and can stop!

Double Rotations

A double rotation can be required to set things up depending upon the left-right turn sequence, but the principle is the same.

Deletion from Red-Black Trees

Recall the three cases for deletion from a binary tree:

Case (a) The node to be deleted was a leaf;

Case (b) The node to be deleted had one child;

Case (c) relabel to node as its successor and delete the successor.

Deletion Color Cases

Suppose the node we remove was red, do we still have a red-black tree?

Yes! No two reds will be together, and the black height for each leaf stays the same.

However, if the dead node y was black, we must give each of its decendants another black ancestor. If an appropriate node is red, we can simply color it black otherwise we must restructure.

Case (a) black NIL becomes ``double black'';

Case (b) red tex2html_wrap_inline324 becomes black and black tex2html_wrap_inline326 becomes ``double black'';

Case (c) red tex2html_wrap_inline328 becomes black and black tex2html_wrap_inline330 becomes ``double black''.

Our goal will be to recolor and restructure the tree so as to get rid of the ``double black'' node.

In setting up any case analysis, we must be sure that:

  1. All possible cases are covered.
  2. No case is covered twice.

In the case analysis for red-black trees, the breakdown is:

Case 1: The double black node x has a red brother.

Case 2: x has a black brother and two black nephews.

Case 3: x has a black brother, and its left nephew is red and its right nephew is black.

Case 4: x has a black brother, and its right nephew is red (left nephew can be any color).

Conclusion

Red-Black trees let us implement all dictionary operations in tex2html_wrap_inline340 . Further, in no case are more than 3 rotations done to rebalance. Certain very advanced data structures have data stored at nodes which requires a lot of work to adjust after a rotation -- red-black trees ensure it won't happen often.

Example: Each node represents the endpoint of a line, and is augmented with a list of segments in its subtree which it intersects.

We will not study such complicated structures, however.




next up previous
Next: About this document Up: My Home Page

Steve Skiena
Tue Nov 25 15:36:22 EST 1997