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
.
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
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
internal
nodes ensures that its height is at most
. Thus, a
red-black tree may be unbalanced but will avoid becoming a
linked-list that is longer than
.
Figure 2: Example Red Black Tree.
Definition: The black-height of a node
refers
to the number of BLACK nodes on any path from, but not including
, 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
1
2
3
4
5 if
6 then
7 else if
8 then
9 else
10
11
Figure 3: RBTREE-LEFT-ROTATE in action. The rotation results in:
(1)
takes
's original position, (2)
becomes
's left
child, and (3)
's original left child becomes
's right child.
2.2 Insert
We use the TREE-INSERT procedure to insert a node
into
as if it were an ordinary binary search tree, and then color
RED. If
is the new root node, then we bypass the while-loop
and colour it BLACK. Otherwise, if
'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
's
parent is RED, then the property that a RED node has two
BLACK nodes is violated (because
is RED). In this case,
we enter the while-loop.
RBTREE-INSERT
1 TREE-INSERT
/* See Binary Search Trees */
2
RED
3 while
and
RED
4 do if
/* Is the parent of
a left child? */
5 then
6 if
RED
7 then
BLACK
8
BLACK
9
RED
10
11 else if
12 then
13 RBTREE-LEFT-ROTATE
14
BLACK
15
RED
16 RBTREE-RIGHT-ROTATE
17 else Mirror opposite of "then" clause
18
BLACK
2.3 Delete
RBTREE-DELETE
1 if
or
2 then
3 else
TREE-SUCCESSOR
4 if
5 then
6 else
7
8 if
9 then
10 else if
11 then
12 else
13 if
14 then
15 if
BLACK
16 then RBTREE-DELETE-FIXUP
17 return
RBTREE-DELETE-FIXUP
1 while
and
BLACK
2 do if
3 then
4 if
RED
5 then
BLACK
6
RED
7 RBTREE-LEFT-ROTATE
8
9 if
BLACK and
BLACK
10 then
RED
11
12 else if
BLACK
13 then
BLACK
14
RED
15 RBTREE-RIGHT-ROTATE
16
17
18
BLACK
19
BLACK
20 RBTREE-LEFT-ROTATE
21
22 else Mirror opposite of "then" clause
23
BLACK
File translated from
TEX
by
TTM,
version 3.67.
On 31 Mar 2006, 18:12.