7 Red-Black Trees: Dictionary Data Type, Which Requires Support For
7 Red-Black Trees: Dictionary Data Type, Which Requires Support For
7 Red-Black Trees: Dictionary Data Type, Which Requires Support For
Figure 18: From left to right a right rotation and from right to
left a left rotation.
Node ZIG(Node )
assert = NULL and = = NULL;
= r; r = ; return .
Function ZAG is symmetric and performs a left rotation.
Occasionally, it is necessary to perform two rotations in
sequence, and it is convenient to combine them into a sin-
gle operation referred to as a double rotation, as shown
in Figure 19. We use a function ZIGZAG to implement a
A
right rotation
ZigZag
double
C B
A
D
B C D
Figure 19: The double right rotation at is the concatenation of
a single left rotation at and a single right rotation at .
double right rotation and the symmetric function ZAGZIG
to implement a double left rotation.
Node ZIGZAG(Node )
= ZAG( ); return ZIG().
The double right rotation is the composition of two single
rotations: ZIGZAG() = ZIG() ZAG(). Remember
that the composition of functions is written from right to
left, so the single left rotation of precedes the single right
rotation of . Single rotations preserve the ordering of
nodes and so do double rotations.
Insertion. Before studying the details of the restructur-
ing algorithms for red-black trees, we look at the trees that
arise in a short insertion sequence, as shown in Figure 20.
After adding 10, 7, 13, 4, we have two red edges in se-
quence and repair this by promoting 10 (A). After adding
2, we repair the two red edges in sequence by a single ro-
tation of 7 (B). After adding 5, we promote 4 (C), and after
adding 6, we do a double rotation of 7 (D).
5
4 13
2
4
5
2 7
10
6
5
13
7
2 7
6
10
4
A
7
D
C
B
13
4
2
10
13
4
10 10
13 7
Figure 20: Sequence of red-black trees generated by inserting
the items 10, 7, 13, 4, 2, 5, 6 in this sequence.
An item x is added by substituting a new internal node
for a leaf at the appropriate position. To satisfy Rule (2)
of the red-black tree denition, color the incoming edge
of the new node red, as shown in Figure 21. Start the
x
Figure 21: The incoming edge of a newly added node is always
red.
adjustment of color and structure at the parent of the new
node. We state the properties maintained by the insertion
algorithmas invariants that apply to a node traced by the
algorithm.
INVARIANT I. The only possible violation of the red-
black tree properties is that of Rule (1) at the node
, and if has a red incoming edge then it has ex-
actly one red outgoing edge.
Observe that Invariant I holds right after adding x. We
continue with the analysis of all the cases that may arise.
The local adjustment operations depend on the neighbor-
hood of .
Case 1. The incoming edge of is black. Done.
23
Case 2. The incoming edge of is red. Let be the
parent of and assume is left child of .
Case 2.1. Both outgoing edges of are red, as
in Figure 22. Promote . Let be the parent of
and recurse.
Figure 23: Right rotation of to the left and double right rotation
of to the right.
Case 2.2.2. The right outgoing edge of
is red, as in Figure 23 to the right. Double
right rotate . Done.
Case 2 has a symmetric case where left and right are in-
terchanged. An insertion may cause logarithmically many
promotions but at most two rotations.
Deletion. First nd the node that is to be removed. If
necessary, we substitute the inorder successor for so we
can assume that both children of are leaves. If is last
in inorder we substitute symmetrically. Replace by a
leaf , as shown in Figure 24. If the incoming edge of is
red then change it to black. Otherwise, remember the in-
coming edge of as double-black, which counts as two
black edges. Similar to insertions, it helps to understand
the deletion algorithm in terms of a property it maintains.
INVARIANT D. The only possible violation of the red-
black tree properties is a double-black incoming edge
of .
Figure 24: Deletion of node . The dashed edge counts as two
black edges when we compute the black depth.
Note that Invariant D holds right after we remove . We
now present the analysis of all the possible cases. The ad-
justment operation is chosen depending on the local neigh-
borhood of .
Case 1. The incoming edge of is black. Done.
Case 2. The incoming edge of is double-black. Let
be the parent and the sibling of . Assume is
left child of and note that is internal.
Case 2.1. The edge from to is black.
Case 2.1.1. Both outgoing edges of are
black, as in Figure 25. Demote . Recurse
for = .
Figure 26: Left rotation of to the left and double left rotation
of to the right.
Case 2.1.3. The right outgoing edge of
is black, as in Figure 26 to the right.
Change the color of the left outgoing edge
to black and double left rotate . Done.
Case 2.2. The edge from to is red, as in Fig-
ure 27. Left rotate . Recurse for .
24