Professional Documents
Culture Documents
Red-Black Trees
Trees
Comp 122,
redblack - 3
Comp 122,
Red-black Tree
Binary search tree + 1 bit per node: the attribute
color, which is either red or black.
All other attributes of BSTs are inherited:
key, left, right, and p.
All empty trees (leaves) are colored black.
We use a single sentinel, nil, for all the leaves of
red-black tree T, with color[nil] = black.
The roots parent is also nil[T ].
redblack - 4
Comp 122,
Red-black Properties
1.
2.
3.
4.
Comp 122,
17
41
30
47
38
nil[T]
redblack - 6
Remember: every
internal node has two
children, even though
nil leaves are not
usually shown.
Comp 122,
50
redblack - 7
Comp 122,
26
Height of a node:
17
h(x) = # of edges in a
longest path to a leaf.
Black-height of a node
bh(x) = # of black nodes
on path from x to leaf,
not counting x.
h=1
bh=1
h=2 30
bh=1
redblack - 8
41
nil[T]
Comp 122,
h=3
bh=2
h=1
bh=1
38
h=4
bh=2
47
h=2
bh=1
h=1 50
bh=1
Lemma RB Height
Consider a node x in an RB tree: The longest
descending path from x to a leaf has length h(x),
which is at most twice the length of the shortest
descending path from x to a leaf.
Proof:
# black nodes on any path from x = bh(x) (prop 5)
# nodes on shortest path from x, s(x). (prop 1)
But, there are no consecutive red (prop 4),
and we end with black (prop 3), so h(x) 2 bh(x).
Thus, h(x) 2 s(x). QED
redblack - 9
Comp 122,
Comp 122,
redblack - 11
Comp 122,
Operations on RB Trees
All operations can be performed in O(lg n) time.
The query operations, which dont modify the
tree, are performed in exactly the same way as
they are in BSTs.
Insertion and Deletion are not straightforward.
Why?
redblack - 12
Comp 122,
Rotations
Left-Rotate(T, x)
redblack - 13
Right-Rotate(T, y)
Comp 122,
Rotations
Rotations are the basic tree-restructuring operation for
almost all balanced search trees.
Rotation takes a red-black-tree and a node,
Changes pointers to change the local structure, and
Wont violate the binary-search-tree property.
Left rotation and right rotation are inverses.
Left-Rotate(T, x)
redblack - 14
Right-Rotate(T, y)
Comp 122,
y
10.
else right[p[x]] y
11.
////Put
11. left[y]
left[y]
xx
Putxxon
onys
ysleft.
left.
12.
12. p[x]
p[x]
yy
redblack - 15
Comp 122,
Rotation
The pseudo-code for Left-Rotate assumes that
right[x] nil[T ], and
roots parent is nil[T ].
redblack - 16
Comp 122,
Comp 122,
Insertion in RB Trees
Insertion must preserve all red-black properties.
Should an inserted node be colored Red? Black?
Basic steps:
Use Tree-Insert from BST (slightly modified) to
insert a node x into T.
Procedure RB-Insert(x).
Comp 122,
Insertion
RB-Insert(T,
RB-Insert(T,z)z)
1.1.
yy
nil[T]
nil[T]
2.2. xx
root[T]
root[T]
3.3. while
whilexxnil[T]
nil[T]
4.4.
do
doyy
xx
5.5.
ififkey[z]
key[z]<<key[x]
key[x]
6.6.
then
thenxx
left[x]
left[x]
7.7.
else
elsexx
right[x]
right[x]
8.8. p[z]
p[z]
yy
9.9. ififyy==nil[T]
nil[T]
10.
then
10.
thenroot[T]
root[T]
zz
11.
else
11.
elseififkey[z]
key[z]<<key[y]
key[y]
12.
then
12.
then left[y]
left[y]
zz
13.
else
13.
elseright[y]
right[y]
zz
redblack - 19
RB-Insert(T,
RB-Insert(T,z)z)Contd.
Contd.
14.
14. left[z]
left[z]
nil[T]
nil[T]
15.
15. right[z]
right[z]
nil[T]
nil[T]
16.
16. color[z]
color[z]
RED
RED
17.
17. RB-Insert-Fixup
RB-Insert-Fixup(T,
(T,z)z)
Insertion Fixup
redblack - 20
Comp 122,
Insertion Fixup
Problem: we may have one pair of consecutive
reds where we did the insertion.
Solution: rotate it up the tree and away
Three cases have to be handled
http://www2.latech.edu/~apaun/RBTreeDemo.htm
redblack - 21
Comp 122,
Insertion Fixup
RB-Insert-Fixup
RB-Insert-Fixup(T,
(T, z)
z)
1.1. while
whilecolor[p[z]]
color[p[z]]=
= RED
RED
2.2.
do
doififp[z]
p[z]=
= left[p[p[z]]]
left[p[p[z]]]
3.3.
then
thenyy
right[p[p[z]]]
right[p[p[z]]]
4.4.
ififcolor[y]
color[y]=
= RED
RED
5.5.
then
thencolor[p[z]]
color[p[z]]
BLACK
BLACK //// Case
Case11
6.6.
color[y]
//// Case
color[y]
BLACK
BLACK
Case11
7.7.
color[p[p[z]]]
color[p[p[z]]]
RED
RED //// Case
Case11
8.8.
zz
//// Case
p[p[z]]
p[p[z]]
Case11
redblack - 22
Comp 122,
Insertion Fixup
RB-Insert-Fixup(T,
RB-Insert-Fixup(T, z)
z) (Contd.)
(Contd.)
9.
else
9.
elseififzz=
= right[p[z]]
right[p[z]] ////color[y]
color[y]RED
RED
10.
then
//// Case
10.
thenzz
p[z]
p[z]
Case22
11.
LEFT-ROTATE(T,
//// Case
11.
LEFT-ROTATE(T, z)
z)
Case22
12.
color[p[z]]
//// Case
12.
color[p[z]]
BLACK
BLACK
Case33
13.
color[p[p[z]]]
//// Case
13.
color[p[p[z]]]
RED
RED
Case33
14.
RIGHT-ROTATE(T,
14.
RIGHT-ROTATE(T, p[p[z]])
p[p[z]]) //// Case
Case33
15.
else
15.
else(if
(ifp[z]
p[z]==right[p[p[z]]])(same
right[p[p[z]]])(sameas
as10-14
10-14
16.
with
16.
withright
rightand
andleft
leftexchanged)
exchanged)
17.
17.color[root[T
color[root[T]]]]
BLACK
BLACK
redblack - 23
Comp 122,
Correctness
Loop invariant:
At the start of each iteration of the while loop,
z is red.
If p[z] is the root, then p[z] is black.
There is at most one red-black violation:
Property 2: z is a red root, or
Property 4: z and p[z] are both red.
redblack - 24
Comp 122,
Correctness Contd.
Initialization:
Termination: The loop terminates only if p[z] is black.
Hence, property 4 is OK.
The last line ensures property 2 always holds.
Maintenance: We drop out when z is the root (since
then p[z] is sentinel nil[T ], which is black). When we
start the loop body, the only violation is of property 4.
There are 6 cases, 3 of which are symmetric to the other 3.
We consider cases in which p[z] is a left child.
Let y be zs uncle (p[z]s sibling).
redblack - 25
Comp 122,
p[p[z]]
new z
p[z]
D
z
p[p[z]] (zs grandparent) must be black, since z and p[z] are both red and
there are no other violations of property 4.
Make p[z] and y black now z and p[z] are not both red. But property 5
might now be violated.
Make p[p[z]] red restores property 5.
The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
redblack - 26
Comp 122,
p[z]
p[z]
Left rotate around p[z], p[z] and z switch roles now z is a left
child, and both z and p[z] are red.
Takes us immediately to case 3.
redblack - 27
Comp 122,
C
p[z]
B
z
Comp 122,
Algorithm Analysis
O(lg n) time to get through RB-Insert up to the
call of RB-Insert-Fixup.
Within RB-Insert-Fixup:
redblack - 29
Comp 122,
Deletion
Deletion, like insertion, should preserve all the
RB properties.
The properties that may be violated depends on
the color of the deleted node.
Red OK. Why?
Black?
Steps:
Do regular BST deletion.
Fix any violations of RB properties that may result.
redblack - 30
Comp 122,
Deletion
RB-Delete(T,
RB-Delete(T, z)
z)
1.1. ififleft[z]
left[z]=
= nil[T]
nil[T]or
orright[z]
right[z]=
= nil[T]
nil[T]
2.2.
then
thenyy
zz
3.3.
else
elseyy
TREE-SUCCESSOR(z)
TREE-SUCCESSOR(z)
4.4. ififleft[y]
left[y]=
= nil[T
nil[T]]
5.5.
then
thenxx
left[y]
left[y]
6.6.
else
elsexx
right[y]
right[y]
7.7. p[x]
p[x]
p[y]
p[y] ////Do
Dothis,
this,even
evenififxxisisnil[T]
nil[T]
redblack - 31
Comp 122,
Deletion
RB-Delete
RB-Delete(T,
(T, z)
z) (Contd.)
(Contd.)
8.
8. ififp[y]
p[y]=
= nil[T
nil[T]]
9.
9. then
thenroot[T
root[T]]
xx
10.
10. else
elseififyy=
= left[p[y]]
left[p[y]]
11.
then
11.
thenleft[p[y]]
left[p[y]]
xx
12.
else
12.
elseright[p[y]]
right[p[y]]
xx
13.
13.ififyy=
= zz
14.
14. then
thenkey[z]
key[z]
key[y]
key[y]
15.
15. copy
copyys
yssatellite
satellitedata
datainto
intozz
16.
16.ififcolor[y]
color[y]=
= BLACK
BLACK
17.
17. then
thenRB-Delete-Fixup(T,
RB-Delete-Fixup(T, x)
x)
18.
18.return
returnyy
redblack - 32
Comp 122,
RB Properties Violation
If y is black, we could have violations of redblack properties:
Prop. 1. OK.
Prop. 2. If y is the root and x is red, then the root has
become red.
Prop. 3. OK.
Prop. 4. Violation if p[y] and x are both red.
Prop. 5. Any path containing y now has 1 fewer
black node.
redblack - 33
Comp 122,
RB Properties Violation
Prop. 5. Any path containing y now has 1 fewer black
node.
Comp 122,
Deletion Fixup
RB-Delete-Fixup(T,
RB-Delete-Fixup(T, x)
x)
1.
1. while
whilexx root[T
root[T]]and
andcolor[x]
color[x]=
= BLACK
BLACK
2.
do
2.
doififxx=
= left[p[x]]
left[p[x]]
3.
then
3.
thenw
w
right[p[x]]
right[p[x]]
4.
ififcolor[w]
4.
color[w]=
= RED
RED
5.
then
5.
thencolor[w]
color[w]
BLACK
BLACK
6.
color[p[x]]
6.
color[p[x]]
RED
RED
7.
LEFT-ROTATE(T,
7.
LEFT-ROTATE(T, p[x])
p[x])
8.
w
8.
w
right[p[x]]
right[p[x]]
redblack - 35
Comp 122,
//// Case
Case11
//// Case
Case11
//// Case
Case11
//// Case
Case11
RB-Delete-Fixup(T,
RB-Delete-Fixup(T, x)
x) (Contd.)
(Contd.)
/*
/* xx is
is still
still left[p[x]]
left[p[x]] */
*/
9.9.
ififcolor[left[w]]
color[left[w]]=
= BLACK
BLACKand
andcolor[right[w]]
color[right[w]]=
= BLACK
BLACK
10.
then
//// Case
10.
thencolor[w]
color[w]
RED
RED
Case22
11.
xx
//// Case
11.
p[x]
p[x]
Case22
12.
else
12.
elseififcolor[right[w]]
color[right[w]]=
= BLACK
BLACK
13.
then
13.
thencolor[left[w]]
color[left[w]]
BLACK
BLACK //// Case
Case33
14.
color[w]
//// Case
14.
color[w]
RED
RED
Case33
15.
RIGHT-ROTATE(T,w)
//// Case
15.
RIGHT-ROTATE(T,w)
Case33
16.
w
//// Case
16.
w
right[p[x]]
right[p[x]]
Case33
17.
color[w]
//// Case
17.
color[w]
color[p[x]]
color[p[x]]
Case44
18.
color[p[x]]
//// Case
18.
color[p[x]]
BLACK
BLACK
Case44
19.
color[right[w]]
//// Case
19.
color[right[w]]
BLACK
BLACK
Case44
20.
LEFT-ROTATE(T,
//// Case
20.
LEFT-ROTATE(T, p[x])
p[x])
Case44
21.
xx
//// Case
21.
root[T
root[T]]
Case44
22.
22. else
else(same
(sameas
asthen
thenclause
clausewith
withright
rightand
andleft
leftexchanged)
exchanged)
23.
23.- 36color[x]
color[x]
BLACK
BLACK
redblack
Comp 122,
Deletion Fixup
Idea: Move the extra black up the tree until x points to
a red & black node turn it into a black node,
x points to the root just remove the extra black, or
We can do certain rotations and recolorings and finish.
Within the while loop:
x always points to a nonroot doubly black node.
w is xs sibling.
w cannot be nil[T ], since that would violate property 5 at
p[x].
Comp 122,
Case 1 w is red
p[x]
B
x
D
B
x A
new w
Comp 122,
B c
x
new w
D
redblack - 40
Comp 122,
c
w
D
B
x A
redblack - 41
Comp 122,
Analysis
O(lg n) time to get through RB-Delete up to the
call of RB-Delete-Fixup.
Within RB-Delete-Fixup:
Case 2 is the only case in which more iterations
occur.
x moves up 1 level.
Hence, O(lg n) iterations.
Comp 122,
redblack - 43
Comp 122,