CS473-Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

CS473-Algorithms

Description:

CS473-Algorithms Lecture RBT - DELETION Deletion SENTINELS ( Dummy Records) Sentinel is a technique for handling boundary condition in a natural way. – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 18
Provided by: Erg88
Category:

less

Transcript and Presenter's Notes

Title: CS473-Algorithms


1
CS473-Algorithms
  • Lecture
  • RBT - DELETION

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

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.

4
Deletion
  • In Red-Black Trees
  • Instead, use the unique sentinel nilT to
    represent all NILs.
  • Caution when you want to manipulate a child of a
    node x, set pnilT to x first.

5
  • RB-DELETE (T,z)
  • if leftz nilT or rightz nilT then
  • y z
  • else
  • y ? SUCCESSOR(z)
  • if lefty ? NIL then
  • x ? lefty
  • else
  • x ? righty
  • px ? py
  • if y leftpy then
  • leftpy ? x
  • else
  • rightpy ? x
  • if y ? z then
  • keyz ? keyy
  • if colory BLACK then
  • RB-DELETE-FIXUP(T,x)
  • return y

Determine a node y to splice out
Non-nil child of y nilT 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 nilT if y has no child
x
6
  • RB-DELETE-FIXUP(x)
  • while x rootT and colorx BLACK
  • if x ? leftpx then
  • w ? rightpx
  • if colorw RED then
  • colorw ? BLACK
  • colorpx ? RED
  • LEFT-ROTATE( px )
  • w ? rightpx Case 1
  • if colorleftw BLACK and
    colorrightw BLACK then
  • colorw ? RED
  • x ? px
  • else if colorrightw BLACK
    then
  • colorleftw ? BLACK
  • colorw ? RED
  • RIGHT-ROTATE(w)
  • w ? rightpx
  • colorw ? colorpx
  • colorpx ? BLACK

Case 1
Case 2
Case 3
Case 4
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

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.

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
    px
  • Analyze only the 4 cases in which x is a left
    child.
  • Maintain a pointer w to xs sibling
  • Since x is doubly-black, w ? nilT
  • Otherwise, property 4 was violated before the
    delete
  • of blacks on the path px NIL leaf w
  • Would be smaller than the number on the path
    px-x

10
Cases of the While Loop
-switch colors of w pxpw
-LEFT(px)
-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
11
Cases of the While Loop
-switch colors of w leftw
-RIGHT(w)
Case 3
D
C
- Make some color changes
-LEFT(px)
E
Case 4
12
Deletion
  • ???? idea In each case,
  • Number of black nodes from (and including) root
    r of the subtree
  • To each of the subtrees a,ß,....., d
  • Is preserved by the transformation
  • Case 1 Both before and after the transformation
  • nb(r?a) nb(r?ß) 3
  • nb(r??) nb(r?d) nb(r?e) nb(r? ?) 2
  • Case 2 Both before after the transformation
  • nb(r?a) nb(r?ß) 2 c
  • nb(r??) nb(r?d) nb(r?e) nb(r??) 1 c
  • 1 if colorr black
  • 0 if colorr red

Where c
13
Deletion
  • Case 3 Both before and after the transformation
  • nb(r?a) nb(r?ß) 2 c
  • nb(r??) nb(r?d) 1 c
  • nb(r?e) nb(r??) 2 c
  • Case 4 Both before after the transformation
  • nb(r?a) nb(r?ß) 2 c
  • nb(r??) nb(r?d) 1 c c
  • nb(r?e) nb(r??) 1 c

14
Deletion
  • Case 1 w ( sibling of x) is red
  • W must have black children and a black parent
  • Switch colors of w and px pw
  • Then, perform a left-rotation on px
  • Without violating any R-B properties
  • New sibling new w of x, one of old ws
    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 ws
    children
  • Both of ws children are black (Case 2)
  • ws left child is red, right child is black (Case
    3)
  • ws right child is red.

15
Deletion
  • Case 2 w is black leftw rightw 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 px
  • Then repeat the while-loop with px as the new
    node x
  • If we enter Case 2 thru Case 1
  • Color c of new node x is red since original px
    was red.
  • Thus loop terminates when it tests the loop
    condition

16
Deletion
  • Case 3 w is black leftw is red rightw is
    black
  • Switch colors of w and its left child leftw
  • 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
Deletion
  • Case 4 w is black rightw is red
  • By marking the following color changes
  • Set color of ws right child to black
  • Set ws color to its parents (px pw) color
  • Set pxs color to black.
  • and performing a left-rotation on px
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com