x ← left[y]

Chapter 13
Red-Black Trees
Lee, Hsiu-Hui
Ack: This presentation is based on the
lecture slides from Hsu, Lih-Hsing, as
well as various materials from the web.
Balanced search Trees
•
Many search-tree schemes that are
“balanced” in order to guarantee that
basic dynamic-set operations take O(lg
n ) time in the worse case.
e.g. AVL trees
Red-black trees
20071130 chap13
Hsiu-Hui Lee
2
Red-Black Tree
•
•
•
a variation of binary search trees
with one extra bit of storage per node: its color, which
can either RED or BLACK.
Each node of the tree now contains the fields color, key,
left, right, and p. If a child or the parent of a node does
not exist, the corresponding pointer field of the node
contains the value NIL. We shall regard these NIL’s as
being pointers to external nodes(leaves) of the binary
search tree and the normal, key-bearing nodes as
being internal nodes of the tree.
20071130 chap13
Hsiu-Hui Lee
3
Red-Black Properties
1.
2.
3.
4.
5.
Every node is either red or black.
The root is black.
Every leaf (NIL) is black
If a node is red, then both its children are black.
For each node, all paths from the node x to
descendant leaves contain the same number of
black nodes (i.e. black-height(x)).
20071130 chap13
Hsiu-Hui Lee
4
Example of a red-black tree
20071130 chap13
Hsiu-Hui Lee
5
Example of a red-black tree
1. Every node is either red or black.
20071130 chap13
Hsiu-Hui Lee
6
Example of a red-black tree
2. 3. The root and leaves (NIL’s) are black.
20071130 chap13
Hsiu-Hui Lee
7
Example of a red-black tree
4. If a node is red, then its children are black.
?? Means (If a node is red, then its parent is black.)
20071130 chap13
Hsiu-Hui Lee
8
Example of a red-black tree
5. All simple paths from any node x to a
descendant leaf have the same number of
black nodes = black-height(x).
20071130 chap13
Hsiu-Hui Lee
9
black-height of the node: bh(x)
# of black nodes on any path from, but not including,
a node x down to a leaf
black-height of a RB tree = black-height of its root
20071130 chap13
Hsiu-Hui Lee
10
20071130 chap13
Hsiu-Hui Lee
11
Leaves and the root’s
parent omitted entirely
20071130 chap13
Hsiu-Hui Lee
12
Height and black-height
• Height of a node x: h(x)
is the number of edges in a longest path to a leaf.
• Black-height of a node x: bh(x)
is the number of black nodes (including nil[T ])
on the path from x to leaf, not counting x.
20071130 chap13
Hsiu-Hui Lee
13
Lemma 13.0a
The subtree rooted at any node x contains
≥ 2bh(x) − 1 internal nodes.
Proof By induction on height of x.
Basis: Height of x = 0⇒ x is a leaf ⇒bh(x) = 0. The subtree
rooted at x has 0 internal nodes. 20 − 1 = 0.
Inductive step: Let the height of x be h and bh(x) = b.
Any child of x has height h − 1 and black-height either b
(if the child is red) or b − 1 (if the child is black).
By the inductive hypothesis, each child has ≥ 2bh(x)-1 − 1
internal nodes.
Thus, the subtree rooted at x contains ≥ 2 · (2bh(x)-1 − 1 )+1 =
2bh(x) −1 internal nodes. (The +1 is for x itself.)
20071130 chap13
Hsiu-Hui Lee
14
Lemma 13.0b
Any node with height h has black –height at
least h/2.
Proof
By property 4 (If a node is red, then both its
children are black),
≤ h/2 nodes on the path from the node to a leaf are
red.
Hence ≥ h/2 are black.
20071130 chap13
Hsiu-Hui Lee
15
Lemma 13.1
A red-black tree with n internal nodes
has height at most 2lg(n+1).
Proof
Let h and b be the height and black-height of the root,
respectively.
By the above two claims,
n ≥ 2b − 1 ≥ 2h/2 − 1 .
Adding 1 to both sides and then taking logs gives
lg(n + 1) ≥ h/2,
which implies that h ≤ 2 lg(n + 1).
20071130 chap13
Hsiu-Hui Lee
16
13.2 Rotations
Left-Rotation makes y’s left subtree into x’s right subtree.
Right-Rotation makes x’s right subtree into y’s left subtree.
20071130 chap13
Hsiu-Hui Lee
17
LEFT-ROTATE(T, x)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
y ← right[x]
✄ Set y.
right[x] ← left[y]
✄ Turn y’s left subtree into x’s right subtree.
if left[y] ≠ nil[T ]
then p[left[y]] ← x
p[y] ← p[x]
✄ Link x’s parent to y.
if p[x] = nil[T ]
The code for RIGHT-ROTATE is symmetri
then root[T ]← y
Both LEFT-ROTATE and RIGHT-ROTATE
else if x = left[p[x]] run in O(1) time.
then left[p[x]] ← y
else right[p[x]] ← y
left[y] ← x
✄ Put x on y’s left.
p[x] ← y
20071130 chap13
Hsiu-Hui Lee
18
Example of LEFT-ROTATE(T,x)
Rotation makes y’s left subtree
into x’s right subtree.
20071130 chap13
Hsiu-Hui Lee
19
Some Good Java Applets
to simulate Red-Black Tree
• http://webpages.ull.es/users/jriera/Docencia/AVL/
AVL tree applet.htm
• http://gauss.ececs.uc.edu/RedBlack/redblack.html
20071130 chap13
Hsiu-Hui Lee
20
Insertion and Deletion
RB-INSERT(T, x)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
y ← nil[T]
x ← root[T]
while x ≠ nil[T]
do y ← x
if key[z] < key[x]
then x ← left[x]
else x ← right[x]
p[z] ← y
if y = nil[T]
then root[T] ← z
else if key[z] < key[y]
then left[y] ← z
else right[y] ← z
left[z] ← nil[T]
right[z] ← nil[T]
color[z] ← RED
RB-INSERT-FIXUP(T, z)
20071130 chap13
Hsiu-Hui Lee
22
RB-INSERT-FIXUP(T, z)
1 while color[p[z]] = RED
2
do if p[z] = left[p[p[z]]]
3
then y ← right[p[p[z]]]
4
if color[y] = RED
5
then color[p[z]] ←BLACK
6
color[y] ← BLACK
7
color[p[p[z]]] ← RED
8
z ← p[p[z]]
20071130 chap13
Hsiu-Hui Lee
Case 1
Case 1
Case 1
Case 1
23
9
10
11
12
13
14
15
else if z = right[p[z]]
then z ← p[p[z]]
Case 2
LEFT-ROTATE(T,z)
Case 2
color[p[z]] ← BLACK
Case 3
color[p[p[z]]] ← RED
Case 3
RIGHT-ROTATE(T,p[p[z]]) Case 3
else (same as then clause
with “right” and “left” exchanged)
16 color[root[T]] ← BLACK
20071130 chap13
Hsiu-Hui Lee
24
The operation of
RB-INSERT-FIXUP
紅叔
黑叔右子
20071130 chap13
Hsiu-Hui Lee
25
黑叔左子
20071130 chap13
Hsiu-Hui Lee
26
case 1 of the RB-INSERT
(z’s uncle y is red)
紅叔右子
紅叔左子
20071130 chap13
Hsiu-Hui Lee
27
case 2/3 of the RB-INSERT
(z’s uncle y is black and
z is a right/left child)
黑叔右子
20071130 chap13
黑叔左子
Hsiu-Hui Lee
28
Analysis
• RB-INSERT take a total of O(lg n) time.
• It never performs more than two
rotations, since the while loop terminates
if case 2 or case 3 executed.
20071130 chap13
Hsiu-Hui Lee
29
13.4 Deletion
RB-DELETE(T, z)
1
2
3
4
5
6
7
8
if left[z] = nil[T] or right[z] = nil[T]
then y ← z
else y ← TREE-SUCCESSOR(z)
if left[y] ≠ nil[T]
then x ← left[y]
else x ← right[y]
p[x] ← p[y]
if p[y] = nil[T]
20071130 chap13
Hsiu-Hui Lee
30
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 y ≠ z
14
then key[z] ← key[y]
15
copy y’s satellite data into z
16 if color[y] = BLACK
17
then RB-DELETE-FIXUP(T, x)
18 return y
20071130 chap13
Hsiu-Hui Lee
31
RB-DELETE-FIXUP(T,x)
RB-DELETE-FIXUP (T, x)
1
2
3
4
5
6
7
8
20071130 chap13
while x ≠ root[T] and color[x] = BLACK
do if x = left[p[x]]
then w ← right[p[x]]
if color[w] = RED
then color[w] ← BLACK
color[p[x]] = RED
LEFT-ROTATE(T,p[x])
w ← right[p[x]]
Hsiu-Hui Lee
Case1
Case1
Case1
Case1
32
9
if color[right[w]] = BLACK and color[right[w]= BLACK
10
then color[w] ← RED
Case2
11
x ← p[x]
Case2
12
else if color[right[w]] = BLACK
13
then color[left[w]] ← BLACK
Case3
14
color[w] ← RED
Case3
15
RIGHT-ROTATE(T,w)
Case3
16
w ← right[p[x]]
Case3
17
color[w] ← color[p[x]]
Case4
18
color[p[x]] ← BLACK
Case4
19
color[right[w]] ← BLACK
Case4
20
LEFT-ROTATE(T,p[x])
Case4
21
x ← root[T]
Case4
22
else (same as then clause with “right” and “left” exchanged)
23 color[x] ← BLACK
20071130 chap13
Hsiu-Hui Lee
33
the case in the while loop of
RB-DELETE-FIXUP
20071130 chap13
Hsiu-Hui Lee
34
20071130 chap13
Hsiu-Hui Lee
35
Analysis
• The RB-DELETE-FIXUP takes O(lg n)
time and performs at most three
rotations.
• The overall time for RB-DELETE is
therefore also O(lg n)
20071130 chap13
Hsiu-Hui Lee
36