Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

CS 21: Red Black Tree Deletion February 25, 1998

erm 12.235
E
R
S
X
C
D
B
Deletion from Red-Black
Trees
R
U
O
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.236
Setting Up Deletion
As with binary search trees, we can always
delete a node that has at least one external child
If the key to be deleted is stored at a node that
has no external children, we move there the key
of its inorder predecessor (or successor), and
delete that node instead
Example: to delete key 7, we move key 5 to
node u, and delete node v
7
4 8
2 5 9
5
4 8
2 9
u
v
u
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.237
1. Remove v with a removeAboveExternal op-
eration
2. If v was red, color u black. Else, color u
double black.
3. While a double black edge exists, perform
one of the following actions ...
v
v
u
u
u u
Deletion Algorithm
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.238
How to Eliminate the
Double Black Edge
The intuitive idea is to perform a color
compensation
Find a red edge nearby, and change the
pair ( red , double black ) into
( black , black )
As for insertion, we have two cases:
restructuring, and
recoloring (demotion, inverse of
promotion)
Restructuring resolves the problem lo-
cally, while recoloring may propagate it
two levels up
Slightly more complicated than inser-
tion, since two restructurings may occur
(instead of just one)
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.239
If sibling is black and one of its children is
red, perform a restructuring
Case 1: blacksibling witha red
child
v
p
s
v
p
s
v
p
s
z
v
z
p
s
z
z
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.240
(2,4) Tree Interpretation
30
20
x
y
10
40
r
z
20 10
40
... 30 ...
... ...
30
20
c
b
10
40
r
a
40
... 20 ...
... ...
30 10
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.241
Case 2: black sibling with
black childern
If sibling and its children are black, per-
form a recoloring
If parent becomes double black, continue
upward
v
s
p
v
p
s
v
p
s v
p
s
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.242
(2,4) Tree Interpretation
30
20
x
y
40
r
40
10 30 ...
20
10
...
30
20
x
y
40
r
40
10 ...
20
10
...
30
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.243
Case 3: red sibling
If sibling is red, perform an adjustment
Nowthe sibling is black and one the of pre-
vious cases applies
If the next case is recoloring, there is no
propagation upward (parent is now red)
v
p
s
v
p
s
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.244
How About an Example?
6
4 8
2 5 9
Remove 9
6
4 8
2
5 7
7
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.245
6
4 8
2
5
What do we know?
Sibling is black with black
children
What do we do?
Recoloring
7
6
4 8
2
5 7
Example
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.246
Delete 8
no double black
6
4 8
2
5 7
Example
6
4 7
2
5
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.247
Delete 7
Restructuring
Example
4
2
5
6
4 7
2
5
6
4
6
2
5
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.248
Example
4
7
5
14
12
16
15 18
17
4
7
5
14
16
15 18
17
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.249
Example
4
7
5
14
16
15 18
17
5
14
16
15 18
17
7 4
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.250
Time Complexity of Deletion
Take a guess at the time complexity of deletion
in a red-black tree . . .
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.251
What else could it be?!
O(logN)
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.252
Colors and Weights
Color Weight
red
black
double black
0
1
2
Every root-to-leaf path has the same weight
There are no two consecutive red edges
Therefore, the length of any root-to-leaf
path is at most twice the weight
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.253
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)
The perturbation is either
resolved locally (restructuring), or
propagated to a higher level in the tree
by recoloring (promotion or demotion)
O(1) time for a restructuring or recoloring
At most one restructuring per insertion, and
at most two restructurings per deletion
O(log N) recolorings
Total time: O(log N)
CS 21: Red Black Tree Deletion February 25, 1998
erm 12.254
Red-Black Trees
Search O(log N)
Insert O(log N)
Delete O(log N)
Operation Time

You might also like