E R S X C D B
Deletion from Red-Black Trees
R U O
Deletion from Red-Black Trees E R R C U D O B S X erm - - PDF document
CS 21: Red Black Tree Deletion February 25, 1998 Deletion from Red-Black Trees E R R C U D O B 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
E R S X C D B
Deletion from Red-Black Trees
R U O
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
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
eration
double black.
v v u u u u Deletion Algorithm
How to Eliminate the Double Black Edge
compensation’’
pair ( red , double black ) into ( black , black )
promotion)
cally, while recoloring may propagate it two levels up
tion, since two restructurings may occur (instead of just one)
red, perform a restructuring
Case 1: black sibling with a red child v p s v p s v p s z v z p s z z
(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 10Case 2: black sibling with black childern
form a recoloring
upward
v s p v p s v p s v p s
(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 ... 30Case 3: red sibling
vious cases applies
propagation upward (parent is now red)
v p s v p s
How About an Example?
6 4 8 2 5 9 Remove 9 6 4 8 2 5 7 7
6 4 8 2 5 What do we know?
children What do we do?
7 6 4 8 2 5 7
Example
Delete 8
6 4 8 2 5 7
Example
6 4 7 2 5
Delete 7
Example
4 2 5 6 4 7 2 5 6 4 6 2 5
Example
4 7 5 14 12 16 15 18 17 4 7 5 14 16 15 18 17Example
4 7 5 14 16 15 18 17 5 14 16 15 18 17 7 4Time Complexity of Deletion
Take a guess at the time complexity of deletion in a red-black tree . . .
What else could it be?!
Colors and Weights Color Weight red black double black 1 2
Every root-to-leaf path has the same weight There are no two consecutive red edges
path is at most twice the weight
Bottom-Up Rebalancing
perturbation (two consecutive red edges, or a double-black edge)
by recoloring (promotion or demotion)
at most two restructurings per deletion
Red-Black Trees
Search O(log N) Insert O(log N) Delete O(log N) Operation Time