Professional Documents
Culture Documents
Tree PDF
Tree PDF
6.046J/18.401J
LECTURE 10
Balanced Search Trees
Red-black trees
Height of a red-black tree
Rotations
Insertion
L7.1
Examples:
AVL trees
2-3 trees
2-3-4 trees
B-trees
Red-black trees
L7.2
Red-black trees
This data structure requires an extra onebit color field in each node.
Red-black properties:
1. Every node is either red or black.
2. The root and leaves (NILs) are black.
3. If a node is red, then its parent is black.
L7.3
18
18
NIL
10
10
88
11
11
h=4
22
22
NIL
26
26
NIL
NIL
L7.4
18
18
NIL
10
10
88
22
22
11
11
NIL
26
26
NIL
NIL
L7.5
18
18
NIL
10
10
88
22
22
11
11
NIL
26
26
NIL
NIL
L7.6
18
18
NIL
10
10
88
22
22
11
11
NIL
26
26
NIL
NIL
L7.7
18
18 bh = 2
NIL
bh = 1 10
10
bh = 1
88
22
22
11
11
NIL
26
26
NIL
NIL
L7.8
h 2 lg(n + 1).
INTUITION:
L7.9
h 2 lg(n + 1).
INTUITION:
L7.10
h 2 lg(n + 1).
INTUITION:
L7.11
h 2 lg(n + 1).
INTUITION:
L7.12
h 2 lg(n + 1).
INTUITION:
L7.13
h 2 lg(n + 1).
INTUITION:
L7.14
Proof (continued)
We have
h h/2, since
at most half
the leaves on any path
are red.
in each tree is n + 1
n + 1 2h'
lg(n + 1) h' h/2
h 2 lg(n + 1).
L7.15
Query operations
L7.16
Modifying operations
color changes,
restructuring the links of the tree via
rotations.
L7.17
Rotations
RIGHT-ROTATE(B)
BB
LEFT-ROTATE(A)
AA
AA
BB
a , b , c a A b B c.
L7.18
IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
77
33
18
18
10
10
88
22
22
11
11
26
26
L7.19
IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
33
Insert x =15.
77
18
18
10
10
88
22
22
11
11
26
26
15
15
October 19, 2005
L7.20
IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
33
Insert x =15.
RIGHT-ROTATE(18).
October 19, 2005
77
18
18
10
10
88
22
22
11
11
26
26
15
15
L7.21
IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
77
Example:
33
Insert x =15.
88
Recolor, moving the
RIGHT-ROTATE(18).
LEFT-ROTATE(7) and recolor.
October 19, 2005
10
10
18
18
11
11
15
15
22
22
26
26
L7.22
IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the
violation up the tree by recoloring until it can
be fixed with rotations and recoloring.
Example:
77
Insert x =15.
RIGHT-ROTATE(18).
LEFT-ROTATE(7) and recolor.
October 19, 2005
10
10
18
18
11
11
15
15
22
22
26
26
L7.23
Pseudocode
RB-INSERT(T, x)
TREE-INSERT(T, x)
color[x] RED only RB property 3 can be violated
while x root[T] and color[p[x]] = RED
do if p[x] = left[p[p[x]]
then y right[p[p[x]]
y = aunt/uncle of x
if color[y] = RED
then Case 1
else if x = right[p[x]]
then Case 2 Case 2 falls into Case 3
Case 3
else then clause with left and right swapped
color[root[T]] BLACK
October 19, 2005
L7.24
Graphical notation
Let
All
L7.25
Case 1
Recolor
CC
DD
AA
BB
new x
DD
AA
BB
(Or, children of
A are swapped.)
CC
L7.26
Case 2
CC
AA
BB
LEFT-ROTATE(A)
CC
BB
AA
Transform to Case 3.
L7.27
Case 3
CC
BB
AA
RIGHT-ROTATE(C)
y
BB
AA
CC
Done! No more
violations of RB
property 3 are
possible.
October 19, 2005
L7.28
Analysis
L7.29