170 likes | 289 Views
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.
E N D
CS473-Algorithms Lecture RBT - DELETION Lecture X
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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