RBT Operations The basic algorithm for inserting a node into an RBT is: 1: procedure RBT INSERT(T, x) 2: BST insert(t, x) : colour[x] red 4: if parent[x] = red then 5: RBT insert fixup(t, x) 6: end if 7: end procedure By colouring x red, we may violate the property that says red nodes have black children. Think of x as the problem node. To fix up the violation we distinguish three cases: 1. x s uncle is red 2. x s uncle is black and x is an inner child.. x s uncle is black and x is an outer child. All fixups push the problem back up the tree, so RBT insert fixup needs to traverse the tree upwards until either there is no problem anymore, or we reach the root of the tree. This can be done recursively or iteratively. COSC242 Lecture 16 / Slide 1
Case 1: x s uncle is red 1 4 uncle 2 x Solution: push grandparent s black down one level, and make the grandparent red. new x 1 4 2 COSC242 Lecture 16 / Slide 2
Case 2: x s uncle is black and x is an inner child. 1 uncle 2 x Solution: rotate left around x s parent, turning the problem into a case problem. 2 new x 1 COSC242 Lecture 16 / Slide
Case : x s uncle is black and x is an outer child 2 uncle x 1 Solution: make x s parent black and x s grandparent red. Then rotate right around x s grandparent. 2 1 In this case the new subtree has black at its root and therefore we don t need to propagate the fixup any further up the tree. Note that in these three cases we ve used examples where x is on the left of the root of the subtree. There are equivalent cases which are mirror images of the ones discussed here. COSC242 Lecture 16 / Slide 4
Example Insertion Here are the RBTs that successively result when inserting 9,, 6,, 5: 9 9 9 case 6 9 6 case 1 6 9 6 9 6 9 5 case 2 case 6 9 5 5 9 6 COSC242 Lecture 16 / Slide 5
BST Deletion To delete a node z, BST-deletion recursively searches for z, and then uses replace-the-root. Replace-the-root falls into two cases: 1. if the root has < 2 (nonempty) children, replace it by a child (empty if thats all that is available); 2. if the root has two children, replace the root by its successor (minimum of right subtree). Some node, call it y, eventually gets spliced out. It may be that y = z, or y may be z s successor. delete 0 0 5 5 COSC242 Lecture 16 / Slide 6
RBT deletion To delete a node, z, in an RBT: 1. delete z as for a BST 2. fix any RBT violations (how?) Call the spliced-out node y (remember that the spliced out node is the node that is removed from the tree - which is not necessarily z). Here are the RBT properties again: 1. Every node is either red or black 2. The root is black. All dummy leaves are black (think of dummy leaves as NULL subtrees) 4. If a node is red, then the roots of both its subtrees are black 5. For each node, all paths from the node to the leaves contain the same number of black nodes (including the dummy leaves). If y is red, can any of those properties be violated? If y is black, can any of those properties be violated? COSC242 Lecture 16 / Slide 7
If y is black If y is black, what can go wrong: If y was the root, the new root might be red (property 2). This is easy to fix - just make the root black. Splicing out y might leave two red nodes in a parent-child relationship. The black-height of at least one path will be reduced by one (an imbalance). This is always the case when splicing out a black node. Let s start by defining some labels: z is the node to be deleted y is the node that gets spliced out (sometimes y = z and sometimes y is z s successor) x is the child that replaced y w is the new sibling of x COSC242 Lecture 16 / Slide
Four cases There are 4 cases to handle: 1. x s sibling, w, is red, fix then fall to case 2, or 4 2. w is black and has two black children, fix then traverse up the tree. w is black and w s inner child is red and outer child is black, fix then fall to case 4 4. w is black and w s outer child is red, fix and terminate The trick to fixing all of these is to give x an extra black value. Since we ve removed a black node, giving x the value of 2 black restores the black-height property of the tree. We move this extra black value up the tree until we can either give it to a red node, or until we reach the root. COSC242 Lecture 16 / Slide 9
Case 1 x s sibling w is red. We can convert this case to another case by rotating and swapping colours: x(bb) w 0 x(bb) new w 0 Now we have case 2, or 4. COSC242 Lecture 16 / Slide
Case 2 x s sibling is not red and has two black children: x(bb) w 0 new x (bb or rb) 0 Either we are done (rb) or we head up the tree with a new x. COSC242 Lecture 16 / Slide 11
Case w s inner child is red and outer child is black: x (bb) w 0 x (bb) new w 0 Now we have Case 4. COSC242 Lecture 16 / Slide 12
Case 4 w s outer child is red: x (bb) w 0 x 0 COSC242 Lecture 16 / Slide 1
Exercises 1. Draw all the RBTs that result from successive insertions of 5, 4,, 2, 1 into an initially empty RBT. State which cases apply. 2. Draw all the RBTs that result from successive insertion of 11,, 9,, 2, 6,, 5, 4, 1 into an initially empty RBT. State which cases apply.. Draw all the RBTs that result from successive insertion of 14, 12, 65,, 92 into an initially empty RBT. Now draw the RBTs that result from deletion of 65. State which cases apply. 4. Draw the RBTs that result from successive insertion of, 92, 12, 2, 17 into an initially empty RBT. Now draw the RBTs that result from deletion of 92. State which cases apply. 5. Draw all the RBTs that result from successive insertion of 6, 96, 9, 4, 11, 26, 7 into an initially empty RBT. Now draw the RBTs that result from deletion of 96. State which cases apply. COSC242 Lecture 16 / Slide 14