Red-Black Trees

A binary search tree becomes less efficient when the tree becomes unbalanced. For instance, if we store a set of numbers in increasing order, we get a tree as shown in Figure 1 that is virtually a linked list. To overcome this weakness of binary search trees, we can use red-black trees. A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. Each node therefore contains at least the fields color, key, left, right, and parent pointer p.
images/BinarySearchTreeLinkedList.png
Figure 1: A binary search tree that is no better than a linked-list!

1  Properties of Red-black Trees

To avoid the situation illustrated in Figure 1, red-black trees adhere to the following properties in addition to properties of a binary search tree.
Maintaining these properties, a red-black tree with n internal nodes ensures that its height is at most 2log(n+1). Thus, a red-black tree may be unbalanced but will avoid becoming a linked-list that is longer than 2log(n+1)+1.
images/RedBlackTreeNilNodes.png
Figure 2: Example Red Black Tree.
Definition: The black-height of a node x refers to the number of BLACK nodes on any path from, but not including x, to a leaf. The black-height of the tree is the black-height of the root node.

2  Insertion and Deletion

As red-black trees are essentially binary search trees, querying algorithms such as TREE-SEARCH and TREE-MINIMUM can be used on red-black trees. However, due to the red-black tree properties, insertion and deletion are different from TREE-INSERT and TREE-DELETE. Now we may have to change the colours of some nodes in the tree as well as pointer structures. Changing pointer structures is the most important as this allows us to avoid the "excessive linked-list" situation.

2.1  Rotation

We change the pointer structure through rotation, which is a local operation in a search tree that perserves the binary search tree properties. The algorithm for left rotation is shown below along with an illustration in Figure 3. Right rotation is the mirror reflection of left rotation.
RBTREE-LEFT-ROTATE (T,x)
 1     y   right[x]
 2     right[x]   left[y]
 3     p[left[y]]   x
 4     p[y]   p[x]
 5    if  p[x]= NIL
 6          then  root[T]   y
 7          else  if  x=left[p[x]]
 8                            then  left[p[x]]   y
 9                            else   right[p[x]]   y
 10     left[y]   x
 11     p[x]   y
images/RedBlackTreeLeftRotate.png
Figure 3: RBTREE-LEFT-ROTATE in action. The rotation results in: (1) y takes x's original position, (2) x becomes y's left child, and (3) y's original left child becomes x's right child.

2.2  Insert

We use the TREE-INSERT procedure to insert a node x into T as if it were an ordinary binary search tree, and then color x RED. If x is the new root node, then we bypass the while-loop and colour it BLACK. Otherwise, if x's parent is BLACK, then there is nothing to do as adding a RED node does not violate any of the red-black tree properties. However, if x's parent is RED, then the property that a RED node has two BLACK nodes is violated (because x is RED). In this case, we enter the while-loop.
RBTREE-INSERT (T,x)
 1     TREE-INSERT (T,x)  /* See Binary Search Trees */
 2     color[x]  RED
 3    while  xroot[T] and color[p[x]]=RED
 4                  do if  p[x]=left[p[p[x]]]  /* Is the parent of x a left child? */
 5                              then  y   right[p[p[x]]]
 6                                          if  color[y]=RED
 7                                                then  color[p[x]]  BLACK
 8                                                             color[y]  BLACK
 9                                                             color[p[p[x]]]  RED
 10                                                             x   p[p[x]]
 11                                                else  if  x=right[p[x]]
 12                                                                  then  x   p[x]
 13                                                             RBTREE-LEFT-ROTATE (T,x)
 14                                                             color[p[x]]  BLACK
 15                                                             color[p[p[x]]]  RED
 16                                                             RBTREE-RIGHT-ROTATE (T,p[p[x]])
 17                              else  Mirror opposite of "then" clause
 18     color[root[T]]  BLACK

2.3  Delete

RBTREE-DELETE (T,z)
 1    if  left[z]= NIL or right[z]= NIL
 2          then  y   z
 3          else   y   TREE-SUCCESSOR (z)
 4    if  left[y] NIL
 5          then  x   left[y]
 6          else   x   right[y]
 7     p[x]   p[y]
 8    if  p[y]= NIL
 9          then  root[t]   x
 10          else  if  y=left[p[y]]
 11                            then  left[p[y]]   x
 12                            else   right[p[y]]   x
 13    if  yz
 14          then  key[z]   key[y]
 15    if  color[y]=BLACK
 16          then  RBTREE-DELETE-FIXUP (T,x)
 17    return  y
RBTREE-DELETE-FIXUP (T,x)
 1    while  xroot[T] and color[x]=BLACK
 2                  do if  x=left[p[x]]
 3                              then  w   right[p[x]]
 4                                          if  color[w]=RED
 5                                                then  color[w]  BLACK
 6                                                             color[p[x]]  RED
 7                                                             RBTREE-LEFT-ROTATE (T,p[x])
 8                                                             w   right[p[x]]
 9                                          if  color[left[w]]=BLACK and color[right[w]]=BLACK
 10                                                then  color[w]  RED
 11                                                             x   p[x]
 12                                                else  if  color[right[w]]=BLACK
 13                                                                  then  color[left[w]]  BLACK
 14                                                                               color[w]  RED
 15                                                                               RBTREE-RIGHT-ROTATE (T,w)
 16                                                                               w   right[p[x]]
 17                                                             color[w]   color[p[x]]
 18                                                             color[p[x]]  BLACK
 19                                                             color[right[w]]  BLACK
 20                                                             RBTREE-LEFT-ROTATE (T,p[x])
 21                                                             x   root[T]
 22                              else  Mirror opposite of "then" clause
 23     color[x]  BLACK



File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.