Download presentation
Presentation is loading. Please wait.
1
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two consecutive red nodes -- i.e. the children of a red node must be black Every path from a node, x, to a descendant leaf contains the same number of black nodes -- the “black height” of node x. In these slides we don’t force the root to be black
2
2 /26 Deletion: The Main Idea We are given a pointer to the node z to be deleted. Since a Red-Black Tree is a Binary Search Tree (BST), use the BST Delete procedure to: –splice out a node y –locate x, which is either y’s sole non-sentinel child before y was spliced out, or the sentinel, if y had no children. If y is red, no problem, so stop. If y is black, give x an “extra black.” –If x is “red-black,” turn it into a black node and stop. –If x is “black-black ”, use the 4 cases described in the textbook (and there are 4 symmetric cases) to restore any Red-Black Tree properties that are violated. Main idea: “move” extra black up the tree until it can be safely removed w is x’s sibling do recolorings and rotations some cases lead to other cases one case may cause propagation up the tree total amount of work done is in O(lgn). Note: Source of some of these slides is Cormen textbook and accompanying notes.
3
3 /26 Example: Deleting the sequence 18, 91, 2, 36 y is red, so no property is violated, so there’s nothing to do. y x Deleting 18 x w
4
4 /26 Example: Deleting the sequence 18, 91, 2, 36 Case 2, since w is black and both children of w are black. Deleting 91 y x w x x is “doubly- black” because 91 was black. w x Remove one black from w (it becomes red) and remove one black from x (it becomes singly-black). Move the removed black up the tree so that parent of x becomes black.
5
5 /26 Example: Deleting the sequence 18, 91, 2, 36 Deleting 2 x y x y is red, so no property is violated, so there’s nothing to do.
6
6 /26 Example: Deleting the sequence 18, 91, 2, 36 Deleting 36 x y Empty Tree
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.