Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION.

Similar presentations


Presentation on theme: "CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION."— Presentation transcript:

1 CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION

2 CS 473Lecture X2 Deletion SENTINELS ( Dummy Records) Sentinel is a technique for handling boundary condition in a natural way. So that, you don’t need special cases in the code. Example: Merging lists for merge-sort. –Put the sentinel ∞ at the end of the lists to be merged –Just keep merging until you write an ∞ to the answer list. –No need, -To check one list running out, and -Specially handle moving other list to answer

3 CS 473Lecture X3 Deletion In Red-Black Trees Use sentinels to treat a NIL child of a node x as ordinary node whose parent is x. Could add a distinct sentinel node for each NIL in the tree. –Advantage: Parent of each node is well- defined. –Disadvantage: Waste of space.

4 CS 473Lecture X4 Deletion In Red-Black Trees Instead, use the unique sentinel nil[T] to represent all NIL’s. Caution: when you want to manipulate a child of a node x, set p[nil[T]] to x first.

5 CS 473Lecture X5 RB-DELETE (T,z) if left[z] = nil[T] or right[z] = nil[T] then y z else y ← SUCCESSOR(z) if left[y] ≠ NIL then x ← left[y] else x ← right[y] p[x] ← p[y] if y = left[p[y]] then left[p[y]] ← x else right[p[y]] ← x if y ≠ z then key[z] ← key[y] if color[y] = BLACK then RB-DELETE-FIXUP(T,x) return y Determine a node “y” to splice out Non-nil child of y nil[T] if y has no child x = Splice out node y Non-nil child of y nil[T] if y has no child x = Property 4 is disturbed If successor of “z” was spliced out then, move contents of “y” into “z”

6 CS 473Lecture X6 RB-DELETE-FIXUP(x) while x root[T ] and color[x= BLACK if x ≠ left[p[x]] then w ← right[p[x]] if color[w] = RED then color[w] ← BLACK color[p[x]] ← RED LEFT-ROTATE( p[x] ) w ← right[p[x]] Case 1 if color[left[w]] = BLACK and color[right[w]] = BLACK then color[w] ← RED x ← p[x] else if color[right[w]] = BLACK then color[left[w]] ← BLACK color[w] ← RED RIGHT-ROTATE(w) w ← right[p[x]] color[w] ← color[p[x]] color[p[x]] ← BLACK color[right[w]] ← BLACK LEFT-ROTATE( p[x] ) x ← root[T ] else (same as then clause with “right” and “left” exchanged) color[x] ← BLACK Case 4 Case 3 Case 2 Case 1

7 CS 473Lecture X7 Deletion When the spliced-out node y is black, its removal causes –Any path previously contained y to have one fewer black node. –Thus, property 4 is violated by any ancestor of y. Imagine node x having an extra black –This extra black assumption makes property 4 to hold. Then we splice-out black node y –We push its blackness onto its child x –The problem that may arise now Node x may be doubly-black, thus violating property 1

8 CS 473Lecture X8 Deletion Procedure DELETE-FIXUP attemps to restore property 1 –Goal of while-loop is to move extra black up the tree until (1) x points to a red node. - Color the node x black. (2) x points to the root. - Extra black can be simply removed. (3) Suitable rotations and recolorings can be performed. –Within the while-loop x always points to a doubly-black, non-root node.

9 CS 473Lecture X9 Deletion ???? Cases to consider in the while-loop. ??? 4 of them are symmetric to the other 4 depending on –Whether x is a left or right child of its parent p[x] Analyze only the 4 cases in which x is a left child. Maintain a pointer w to x’s sibling Since x is doubly-black, w ≠ nil[T] –Otherwise, property 4 was violated before the delete –# of blacks on the path p[x] – NIL leaf w Would be smaller than the number on the path p[x]-x

10 CS 473Lecture X10 Cases of the While Loop -switch colors of w &p[x]=p[w] -LEFT(p[x]) -Conver to case 2, 3, 4 Case 1 -Take onw black off both x of w -Then repeat the while- loop with new x Case 2 D B D

11 CS 473Lecture X11 Cases of the While Loop -switch colors of w & left[w] -RIGHT(w) Case 3 - Make some color changes -LEFT(p[x]) Case 4 C D E

12 CS 473Lecture X12 Deletion ???? idea: In each case, –Number of black nodes from (and including) root “r” of the subtree –To each of the subtrees α,β,....., δ Is preserved by the transformation Case 1: Both before and after the transformation n b (r→α) = n b (r→β) = 3 n b (r→γ) = n b (r→δ) = n b (r→ε) = n b (r→ ζ) = 2 Case 2: Both before & after the transformation n b (r→α) = n b (r→β) = 2 + c n b (r→γ) = n b (r→δ) = n b (r→ε) = n b (r→ζ) = 1 +c 1 if color[r] = black 0 if color[r] = red Where c =

13 CS 473Lecture X13 Deletion Case 3: Both before and after the transformation n b (r→α) = n b (r→β) = 2 + c n b (r→γ) = n b (r→δ) = 1+ c n b (r→ε) = n b (r→ζ) = 2 + c Case 4: Both before & after the transformation n b (r→α) = n b (r→β) = 2 + c n b (r→γ) = n b (r→δ) = 1+ c + c’ n b (r→ε) = n b (r→ζ) = 1 + c

14 CS 473Lecture X14 Deletion Case 1: w ( sibling of x) is red W must have black children and a black parent Switch colors of w and p[x] = p[w] Then, perform a left-rotation on p[x] –Without violating any R-B properties New sibling “new w” of x, one of old w’s children, is now black Thus we have converted Case 1 into Case 2, 3 or 4 Case 2, 3 or 4: w is black These cases are distibuished by colors of w’s children –Both of w’s children are black (Case 2) –w’s left child is red, right child is black (Case 3) –w’s right child is red.

15 CS 473Lecture X15 Deletion Case 2: w is black; left[w] & right[w] are both black Take one black off both x and w leaving –x with only one black an w red. –Add an extra black to p[x] Then repeat the while-loop with p[x] as the new node x If we enter Case 2 thru Case 1 –Color c of new node x is red since original p[x] was red. –Thus loop terminates when it tests the loop condition

16 CS 473Lecture X16 Deletion Case 3: w is black; left[w] is red; right[w] is black Switch colors of w and its left child left[w] Perform a right rotation on w –Without violating any R-B properties. New sibling “new w” of x –Is now black node with a red right child Thus we have transformed Case 3 into Case 4

17 CS 473Lecture X17 Deletion Case 4: w is black; right[w] is red By marking the following color changes –Set color of w’s right child to black –Set w’s color to its parents (p[x] = p[w]) color –Set p[x]’s color to black. and performing a left-rotation on p[x] We can remove the extra black on x –Without violating any R-B properties Setting x to be the “root” causes –While-loop to terminate when it tests the loop condition.


Download ppt "CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION."

Similar presentations


Ads by Google