# 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$.
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.
• Every node is either RED or BLACK.
• Every leaf is $\text{NIL}$ and is BLACK.
• If a node is RED, then both its children are BLACK.
• Every simple path from a node to one of its descendant leaf nodes contains the same number of BLACK nodes.
Maintaining these properties, a red-black tree with $n$ internal nodes ensures that its height is at most $2\mathrm{log}\left(n+1\right)$. Thus, a red-black tree may be unbalanced but will avoid becoming a linked-list that is longer than $2\mathrm{log}\left(n+1\right)+1$.
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 $\left(T,x\right)$
1     $y$ $←$  $\mathrm{right}\left[x\right]$
2     $\mathrm{right}\left[x\right]$ $←$  $\mathrm{left}\left[y\right]$
3     $p\left[\mathrm{left}\left[y\right]\right]$ $←$  $x$
4     $p\left[y\right]$ $←$  $p\left[x\right]$
5    if  $p\left[x\right]=\text{NIL}$
6          then  $\mathrm{root}\left[T\right]$ $←$  $y$
7          else  if  $x=\mathrm{left}\left[p\left[x\right]\right]$
8                            then  $\mathrm{left}\left[p\left[x\right]\right]$ $←$  $y$
9                            else   $\mathrm{right}\left[p\left[x\right]\right]$ $←$  $y$
10     $\mathrm{left}\left[y\right]$ $←$  $x$
11     $p\left[x\right]$ $←$  $y$
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 $\left(T,x\right)$
1     TREE-INSERT $\left(T,x\right)$  /* See Binary Search Trees */
2     $\mathrm{color}\left[x\right]$ $←$ RED
3    while  $x\ne \mathrm{root}\left[T\right]$ and $\mathrm{color}\left[p\left[x\right]\right]=$RED
4                  do if  $p\left[x\right]=\mathrm{left}\left[p\left[p\left[x\right]\right]\right]$  /* Is the parent of $x$ a left child? */
5                              then  $y$ $←$  $\mathrm{right}\left[p\left[p\left[x\right]\right]\right]$
6                                          if  $\mathrm{color}\left[y\right]=$RED
7                                                then  $\mathrm{color}\left[p\left[x\right]\right]$ $←$ BLACK
8                                                             $\mathrm{color}\left[y\right]$ $←$ BLACK
9                                                             $\mathrm{color}\left[p\left[p\left[x\right]\right]\right]$ $←$ RED
10                                                             $x$ $←$  $p\left[p\left[x\right]\right]$
11                                                else  if  $x=\mathrm{right}\left[p\left[x\right]\right]$
12                                                                  then  $x$ $←$  $p\left[x\right]$
13                                                             RBTREE-LEFT-ROTATE $\left(T,x\right)$
14                                                             $\mathrm{color}\left[p\left[x\right]\right]$ $←$ BLACK
15                                                             $\mathrm{color}\left[p\left[p\left[x\right]\right]\right]$ $←$ RED
16                                                             RBTREE-RIGHT-ROTATE $\left(T,p\left[p\left[x\right]\right]\right)$
17                              else  Mirror opposite of "then" clause
18     $\mathrm{color}\left[\mathrm{root}\left[T\right]\right]$ $←$ BLACK

### 2.3  Delete

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

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