Deletion from Red

advertisement
CS 21: Red Black Tree Deletion
February 25, 1998
Deletion from Red-Black
Trees
E
RR
C
B
D
U
O
S
erm
12.235
X
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
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
2
erm
u
5
8
5
v
4
9
2
12.236
u
8
9
CS 21: Red Black Tree Deletion
February 25, 1998
Deletion Algorithm
1. Remove v with a removeAboveExternal operation
2. If v was red, color u black. Else, color u
double black.
v
u
u
v
u
u
3. While a double black edge exists, perform
one of the following actions ...
erm
12.237
CS 21: Red Black Tree Deletion
February 25, 1998
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 locally, while recoloring may propagate it
two levels up
• Slightly more complicated than insertion, since two restructurings may occur
(instead of just one)
erm
12.238
CS 21: Red Black Tree Deletion
February 25, 1998
Case 1: black sibling with a red
child
• If sibling is black and one of its children is
red, perform a restructuring
s
p
v
p
s
v
z
p
v
erm
z
s
z
z
p
v
12.239
s
CS 21: Red Black Tree Deletion
February 25, 1998
(2,4) Tree Interpretation
...
30
...
x
...
30
...
r
y
20
10
40
z
20
10
40
...
20
...
...
b
20
...
c
a
10
10
30
30
r
40
40
erm
12.240
CS 21: Red Black Tree Deletion
February 25, 1998
Case 2: black sibling with
black childern
• If sibling and its children are black, perform a recoloring
• If parent becomes double black, continue
upward
p
p
v
s
v
p
v
erm
s
p
s
v
12.241
s
CS 21: Red Black Tree Deletion
February 25, 1998
(2,4) Tree Interpretation
10
10
30
x
...
30
...
y
r
20
40
20
40
10
10
x
...
30
...
y
r
20
20
30
40
erm
12.242
40
CS 21: Red Black Tree Deletion
February 25, 1998
Case 3: red sibling
• If sibling is red, perform an adjustment
• Now the sibling is black and one the of previous cases applies
• If the next case is recoloring, there is no
propagation upward (parent is now red)
s
p
v
p
s
v
erm
12.243
CS 21: Red Black Tree Deletion
February 25, 1998
How About an Example?
Remove 9
6
4
2
8
7
5
9
6
4
2
erm
8
5
7
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
2
erm
8
5
4
7
2
12.245
8
5
7
CS 21: Red Black Tree Deletion
February 25, 1998
Example
Delete 8
• no double black
6
6
4
2
erm
8
5
4
7
2
12.246
7
5
CS 21: Red Black Tree Deletion
February 25, 1998
Example
Delete 7
• Restructuring
6
6
4
2
7
4
5
2
4
6
2
5
erm
12.247
5
CS 21: Red Black Tree Deletion
February 25, 1998
Example
14
7
4
16
12
15
18
5
17
14
7
16
4
15
5
erm
18
17
12.248
CS 21: Red Black Tree Deletion
February 25, 1998
Example
14
7
16
4
15
18
5
17
14
5
4
16
7
15
18
17
erm
12.249
CS 21: Red Black Tree Deletion
February 25, 1998
Time Complexity of Deletion
Take a guess at the time complexity of deletion
in a red-black tree . . .
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
Colors and Weights
Color
Weight
red
black
0
1
double black
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
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)
• 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)
erm
12.253
CS 21: Red Black Tree Deletion
February 25, 1998
Red-Black Trees
Operation
erm
Time
Search
O(log N)
Insert
O(log N)
Delete
O(log N)
12.254
Download