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 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 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 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 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) 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 p z v s v z p z v s p s z v erm 12.239
CS 21: Red Black Tree Deletion February 25, 1998 (2,4) Tree Interpretation x ... 30 ... 30 ... ... y r 20 40 z 10 20 10 40 ... 20 ... b 20 ... ... c a 10 30 10 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 , per- form a recoloring • If parent becomes double black , continue upward p p v s v s p p s s v v erm 12.241
CS 21: Red Black Tree Deletion February 25, 1998 (2,4) Tree Interpretation 10 x 10 30 ... 30 ... y r 20 40 20 40 10 x 10 ... 30 ... y r 20 40 20 30 40 erm 12.242
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 pre- vious cases applies • If the next case is recoloring, there is no propagation upward (parent is now red) s p p v s v erm 12.243
CS 21: Red Black Tree Deletion February 25, 1998 How About an Example? Remove 9 6 4 8 7 2 5 9 6 4 8 5 7 2 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 5 7 5 7 2 2 erm 12.245
CS 21: Red Black Tree Deletion February 25, 1998 Example Delete 8 • no double black 6 6 4 8 4 7 5 7 5 2 2 erm 12.246
CS 21: Red Black Tree Deletion February 25, 1998 Example Delete 7 • Restructuring 6 6 4 7 4 5 5 2 2 4 6 2 5 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 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 0 black 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 Time Search O(log N) Insert O(log N) Delete O(log N) erm 12.254
Recommend
More recommend