Академический Документы
Профессиональный Документы
Культура Документы
C RR
B D O U
S X
erm 12.235
CS 21: Red Black Tree Deletion February 25, 1998
Setting Up Deletion
As with binary search trees, we can always
delete a node that has at least one external child
u u
7 5
4 8 4 8
v
2 5 9 2 9
erm 12.236
CS 21: Red Black Tree Deletion February 25, 1998
Deletion Algorithm
1. Remove v with a removeAboveExternal op-
eration
2. If v was red, color u black. Else, color u
double black.
v
u u
v
u u
erm 12.237
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.238
CS 21: Red Black Tree Deletion February 25, 1998
p s
v s p z
z v
p z
v s
p s
z
v
erm 12.239
CS 21: Red Black Tree Deletion February 25, 1998
... 30 ...
x
30
... ...
y r
20 40
10 20 z
10
40
... 20 ... b
... ... 20
a c
10 30
10 30
r
40
40
erm 12.240
CS 21: Red Black Tree Deletion February 25, 1998
p p
v s v s
p p
v s v s
erm 12.241
CS 21: Red Black Tree Deletion February 25, 1998
10 30 ...
x
30
...
y r
20 40
20
40
10
10 ...
x
30
...
y r
20 40
20 30
40
erm 12.242
CS 21: Red Black Tree Deletion February 25, 1998
p s
v s p
v
erm 12.243
CS 21: Red Black Tree Deletion February 25, 1998
4 8
2 5 7 9
4 8
2 5 7
erm 12.244
CS 21: Red Black Tree Deletion February 25, 1998
Example
What do we know?
• Sibling is black with black
children
What do we do?
• Recoloring
6 6
4 8 4 8
2 5 7 2 5 7
erm 12.245
CS 21: Red Black Tree Deletion February 25, 1998
Example
Delete 8
• no double black
6 6
4 8 4 7
2 5 7 2 5
erm 12.246
CS 21: Red Black Tree Deletion February 25, 1998
Example
Delete 7
• Restructuring
6 6
4 7 4
2 5 2 5
2 6
erm 12.247
CS 21: Red Black Tree Deletion February 25, 1998
Example
14
7 16
4 12 15 18
5 17
14
7 16
4 15 18
5 17
erm 12.248
CS 21: Red Black Tree Deletion February 25, 1998
Example
14
7 16
4 15 18
5 17
14
5 16
4 7 15 18
17
erm 12.249
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.250
CS 21: Red Black Tree Deletion February 25, 1998
O(logN)
What else could it be?!
erm 12.251
CS 21: Red Black Tree Deletion February 25, 1998
Color Weight
red 0
black 1
double black 2
erm 12.252
CS 21: Red Black Tree Deletion February 25, 1998
Bottom-Up Rebalancing
of Red-Black Trees
• An insertion or deletion may cause a local
perturbation (two consecutive red edges, or
a double-black edge)
• O(log N) recolorings
erm 12.253
CS 21: Red Black Tree Deletion February 25, 1998
Red-Black Trees
Operation Time
Search O(log N)
Insert O(log N)
Delete O(log N)
erm 12.254