1 / 17

CS473-Algorithms

CS473-Algorithms. Lecture RBT - DELETION. 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.

monte
Download Presentation

CS473-Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS473-Algorithms Lecture RBT - DELETION Lecture X

  2. 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 Lecture X

  3. 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. Lecture X

  4. 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. Lecture X

  5. Determine a node “y” to splice out 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 Non-nil child of y nil[T] if y has no child x = Splice out node y If successor of “z” was spliced out then, move contents of “y” into “z” Property 4 is disturbed Non-nil child of y nil[T] if y has no child x = Lecture X

  6. Case 1 RB-DELETE-FIXUP(x) whilex root[T ] andcolor[x= BLACK ifx ≠left[p[x]] then w ←right[p[x]] ifcolor[w] = RED then color[w] ← BLACK color[p[x]] ← RED LEFT-ROTATE( p[x] ) w ←right[p[x]] Case 1 ifcolor[left[w]] = BLACK and color[right[w]] = BLACK then color[w] ← RED x ←p[x] else ifcolor[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 thenclause with “right” and “left” exchanged) color[x] ← BLACK Case 2 Case 3 Case 4 Lecture X

  7. 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 Lecture X

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

  9. 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 Lecture X

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

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

  12. 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 nb(r→α) = nb(r→β) = 3 nb(r→γ) = nb(r→δ) = nb(r→ε) = nb(r→ ζ) = 2 Case 2: Both before & after the transformation nb(r→α) = nb(r→β) = 2 + c nb(r→γ) = nb(r→δ) = nb(r→ε) = nb(r→ζ) = 1 +c 1 if color[r] = black 0 if color[r] = red Where c = Lecture X

  13. Deletion Case 3: Both before and after the transformation nb(r→α) = nb(r→β) = 2 + c nb(r→γ) = nb(r→δ) = 1+ c nb(r→ε) = nb(r→ζ) = 2 + c Case 4: Both before & after the transformation nb(r→α) = nb(r→β) = 2 + c nb(r→γ) = nb(r→δ) = 1+ c + c’ nb(r→ε) = nb(r→ζ) = 1 + c Lecture X

  14. 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. Lecture X

  15. 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 Lecture X

  16. 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 Lecture X

  17. 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. Lecture X

More Related