cs 758 858 algorithms
play

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.


  1. 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

  2. Red-Black Trees ■ Red-Black Trees ■ BST Deletion ■ Single Child ■ Immed. Succ. ■ Deep Succ. ■ Break Deletion Fixup Red-Black Trees Wheeler Ruml (UNH) Class 7, CS 758 – 2 / 16

  3. Red-Black Trees node: data, left, right, parent, color Red-Black Trees ■ Red-Black Trees 1. every node is either red or black ■ BST Deletion ■ Single Child 2. the root is black ■ Immed. Succ. 3. (consider nil to be black) ■ Deep Succ. ■ Break 4. both children of a red node are black Deletion Fixup 5. from any node, all paths to leaves have the same ‘black height’ Wheeler Ruml (UNH) Class 7, CS 758 – 3 / 16

  4. Plain Binary Tree Deletion 4 cases of delete( z ): Red-Black Trees ■ Red-Black Trees 1. no left child, or no kids: substitute right subtree at parent. ■ BST Deletion ■ Single Child 2. no right child: substitute left subtree at parent. ■ Immed. Succ. 3. successor y is z ’s right child: ■ Deep Succ. ■ Break (a) substitute y for z Deletion Fixup (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? Wheeler Ruml (UNH) Class 7, CS 758 – 4 / 16

  5. Cases 1 and 2: Single Child 1. every node is either red or black Red-Black Trees 2. the root is black ■ Red-Black Trees ■ BST Deletion 3. (consider nil to be black) ■ Single Child ■ Immed. Succ. 4. both children of a red node are black ■ Deep Succ. 5. from any node, all paths to leaves have the same ‘black ■ Break height’ Deletion Fixup deleting z with single child x 1. x takes z ’s place Wheeler Ruml (UNH) Class 7, CS 758 – 5 / 16

  6. Cases 1 and 2: Single Child 1. every node is either red or black Red-Black Trees 2. the root is black ■ Red-Black Trees ■ BST Deletion 3. (consider nil to be black) ■ Single Child ■ Immed. Succ. 4. both children of a red node are black ■ Deep Succ. 5. from any node, all paths to leaves have the same ‘black ■ Break height’ Deletion Fixup 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 Wheeler Ruml (UNH) Class 7, CS 758 – 5 / 16

  7. Case 3: Two Children, Successor is Child 1. every node is either red or black Red-Black Trees 2. the root is black ■ Red-Black Trees ■ BST Deletion 3. (consider nil to be black) ■ Single Child ■ Immed. Succ. 4. both children of a red node are black ■ Deep Succ. 5. from any node, all paths to leaves have the same ‘black ■ Break height’ Deletion Fixup 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 Wheeler Ruml (UNH) Class 7, CS 758 – 6 / 16

  8. Case 3: Two Children, Successor is Child 1. every node is either red or black Red-Black Trees 2. the root is black ■ Red-Black Trees ■ BST Deletion 3. (consider nil to be black) ■ Single Child ■ Immed. Succ. 4. both children of a red node are black ■ Deep Succ. 5. from any node, all paths to leaves have the same ‘black ■ Break height’ Deletion Fixup 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 Wheeler Ruml (UNH) Class 7, CS 758 – 6 / 16

  9. Case 4: Two Children, Successor is Deeper deleting z , successor y is deep down Red-Black Trees ■ Red-Black Trees 1. substitute y ’s right child x for y ■ BST Deletion ■ Single Child 2. attach z ’s right subtree as y ’s right subtree ■ Immed. Succ. as in simpler case: ■ Deep Succ. ■ Break 3. y takes z ’s place and color Deletion Fixup 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 Wheeler Ruml (UNH) Class 7, CS 758 – 7 / 16

  10. Break asst 4: write verifier ■ 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

  11. Red-Black Trees Deletion Fixup ■ Fix-up Loop ■ Case 1 ■ Case 2 ■ Case 3 ■ Case 4 ■ Complexity ■ EOLQs Red-Black Tree Deletion Fixup Wheeler Ruml (UNH) Class 7, CS 758 – 9 / 16

  12. Deletion Fix-up Loop need to find a red node to make black Red-Black Trees Deletion Fixup ■ Fix-up Loop when x red or root, color black and terminate ■ Case 1 ■ Case 2 ■ Case 3 x is non-root black node. ■ Case 4 assume x is a left child (other cases symmetric). ■ Complexity ■ EOLQs 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 Wheeler Ruml (UNH) Class 7, CS 758 – 10 / 16

  13. Case 1 case 1: w is red. so parent and children must be black. Red-Black Trees Deletion Fixup ■ Fix-up Loop solution: ■ Case 1 ■ Case 2 1. rotate and recolor to get black sibling (moves red ■ Case 3 horizontally) ■ Case 4 ■ Complexity 2. fall through to case 2, 3, or 4 ■ EOLQs Wheeler Ruml (UNH) Class 7, CS 758 – 11 / 16

  14. Case 2 case 2: w and both its children are black Red-Black Trees Deletion Fixup ■ Fix-up Loop solution: ■ Case 1 ■ Case 2 1. color w red. subtree at parent now ‘black-balanced’. ■ Case 3 2. move x ’s blackness (and w ’s) up the tree ■ Case 4 ■ Complexity 3. recur at parent ■ EOLQs if from case 1, x now red, so will terminate Wheeler Ruml (UNH) Class 7, CS 758 – 12 / 16

  15. Case 3 case 3: w is black, its right is black, left is red Red-Black Trees Deletion Fixup ■ Fix-up Loop solution: ■ Case 1 ■ Case 2 1. rotate right and move red over to right child ■ Case 3 2. fall through to case 4 ■ Case 4 ■ Complexity ■ EOLQs Wheeler Ruml (UNH) Class 7, CS 758 – 13 / 16

  16. Case 4 case 4: w is black, its right child is red Red-Black Trees Deletion Fixup ■ Fix-up Loop solution: ■ Case 1 ■ Case 2 1. rotate and recolor to annihilate red with x ’s black ■ Case 3 2. set x to root to force termination ■ Case 4 ■ Complexity ■ EOLQs Wheeler Ruml (UNH) Class 7, CS 758 – 14 / 16

  17. Complexity finding successor is O (lg n ) Red-Black Trees Deletion Fixup ■ Fix-up Loop one fixup iteration is constant time ■ Case 1 ■ Case 2 ■ Case 3 fixup loops only when moving up, so is O (lg n ) ■ Case 4 ■ Complexity ■ EOLQs how many rotations are performed? Wheeler Ruml (UNH) Class 7, CS 758 – 15 / 16

  18. EOLQs For example: Red-Black Trees Deletion Fixup What’s still confusing? ■ ■ Fix-up Loop What question didn’t you get to ask today? ■ ■ Case 1 ■ Case 2 What would you like to hear more about? ■ ■ Case 3 ■ Case 4 Please write down your most pressing question about algorithms ■ Complexity and put it in the box on your way out. ■ EOLQs Thanks! Wheeler Ruml (UNH) Class 7, CS 758 – 16 / 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend