CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Red-Black - - PowerPoint PPT Presentation

▶
cs 758 858 algorithms
SMART_READER_LITE
LIVE PREVIEW

CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Red-Black - - PowerPoint PPT Presentation

CS 758/858: Algorithms http://www.cs.unh.edu/~ruml/cs758 Red-Black Trees Deletion Fixup 1 handout: slides Wheeler Ruml (UNH) Class 7, CS 758 1 / 16 Red-Black Trees Red-Black Trees BST Deletion Single Child Immed. Succ.


slide-1
SLIDE 1

CS 758/858: Algorithms

Red-Black Trees Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 1 / 16

http://www.cs.unh.edu/~ruml/cs758 1 handout: slides

slide-2
SLIDE 2

Red-Black Trees

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 2 / 16

slide-3
SLIDE 3

Red-Black Trees

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 3 / 16

node: data, left, right, parent, color 1. every node is either red or black 2. the root is black 3. (consider nil to be black) 4. both children of a red node are black 5. from any node, all paths to leaves have the same ‘black height’

slide-4
SLIDE 4

Plain Binary Tree Deletion

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 4 / 16

4 cases of delete(z): 1. no left child, or no kids: substitute right subtree at parent. 2. no right child: substitute left subtree at parent. 3. successor y is z’s right child: (a) substitute y for z (b) attach z’s left subtree as y’s left subtree 4. successor y is deeper: (a) substitute y’s right subtree for y (b) attach z’s right subtree as y’s right subtree (c) as above, substitute y for z (d) as above, attach z’s left subtree as y’s left subtree What if it’s a red-black tree?

slide-5
SLIDE 5

Cases 1 and 2: Single Child

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 5 / 16

1. every node is either red or black 2. the root is black 3. (consider nil to be black) 4. both children of a red node are black 5. from any node, all paths to leaves have the same ‘black height’ deleting z with single child x 1. x takes z’s place

slide-6
SLIDE 6

Cases 1 and 2: Single Child

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 5 / 16

1. every node is either red or black 2. the root is black 3. (consider nil to be black) 4. both children of a red node are black 5. from any node, all paths to leaves have the same ‘black height’ deleting z with single child x 1. x takes z’s place 2. book uses y for z for short code 3. if y (= z) was black, we have ‘extra black’ at x, so call fixup routine at x

slide-7
SLIDE 7

Case 3: Two Children, Successor is Child

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 6 / 16

1. every node is either red or black 2. the root is black 3. (consider nil to be black) 4. both children of a red node are black 5. from any node, all paths to leaves have the same ‘black height’ deleting z, successor y is right child 1. y takes z’s place and color 2. attach z’s left subtree as y’s left subtree

slide-8
SLIDE 8

Case 3: Two Children, Successor is Child

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 6 / 16

1. every node is either red or black 2. the root is black 3. (consider nil to be black) 4. both children of a red node are black 5. from any node, all paths to leaves have the same ‘black height’ deleting z, successor y is right child 1. y takes z’s place and color 2. attach z’s left subtree as y’s left subtree 3. if y was black, we need ‘extra black’ at y’s right child x, so call fixup routine at x

slide-9
SLIDE 9

Case 4: Two Children, Successor is Deeper

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 7 / 16

deleting z, successor y is deep down 1. substitute y’s right child x for y 2. attach z’s right subtree as y’s right subtree as in simpler case: 3. y takes z’s place and color 4. attach z’s left subtree as y’s left subtree 5. if y was black, we need ‘extra black’ at x, so call fixup routine at x

slide-10
SLIDE 10

Break

Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup

Wheeler Ruml (UNH) Class 7, CS 758 – 8 / 16

■

asst 4: write verifier

■

slide-11
SLIDE 11

Red-Black Tree Deletion Fixup

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 9 / 16

slide-12
SLIDE 12

Deletion Fix-up Loop

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 10 / 16

need to find a red node to make black when x red or root, color black and terminate x is non-root black node. assume x is a left child (other cases symmetric). Must have sibling w, since x holds ‘extra blackness’. 4 cases: 1. w is red 2. w and both its children are black 3. w is black, its right child is black, its left child is red 4. w is black, its right child is red fix-up loop invariant: all properties hold if ‘extra black’ at x is considered, heights of fringe (greek) nodes preserved

slide-13
SLIDE 13

Case 1

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 11 / 16

case 1: w is red. so parent and children must be black. solution: 1. rotate and recolor to get black sibling (moves red horizontally) 2. fall through to case 2, 3, or 4

slide-14
SLIDE 14

Case 2

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 12 / 16

case 2: w and both its children are black solution: 1. color w red. subtree at parent now ‘black-balanced’. 2. move x’s blackness (and w’s) up the tree 3. recur at parent if from case 1, x now red, so will terminate

slide-15
SLIDE 15

Case 3

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 13 / 16

case 3: w is black, its right is black, left is red solution: 1. rotate right and move red over to right child 2. fall through to case 4

slide-16
SLIDE 16

Case 4

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 14 / 16

case 4: w is black, its right child is red solution: 1. rotate and recolor to annihilate red with x’s black 2. set x to root to force termination

slide-17
SLIDE 17

Complexity

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 15 / 16

finding successor is O(lg n)

  • ne fixup iteration is constant time

fixup loops only when moving up, so is O(lg n) how many rotations are performed?

slide-18
SLIDE 18

EOLQs

Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs

Wheeler Ruml (UNH) Class 7, CS 758 – 16 / 16

For example:

■

What’s still confusing?

■

What question didn’t you get to ask today?

■

What would you like to hear more about? Please write down your most pressing question about algorithms and put it in the box on your way out. Thanks!