Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II

2 Deleting from a Red-Black Tree
To delete from a Red-Black Tree we 1) Delete the node, z, from the tree as with a binary-search tree. 2) Call RB-Delete-Fixup to regain Red-Black tree properties.

3 Recall deletion from binary search tree
To delete a node, z, from a binary search tree: Case 1: Node, z, has no non-nil children. Delete the node (splice parent to T.nil) Case 2: Node, z, has one non-nil child. Splice out node by connecting z.p to z.child Case 3: Node, z, has two non-nil children. Find immediate successor. Copy data from successor to z (keep z.color same) Delete successor.

4 When do violations of Red-Black Properties occur?
Suppose spliced out node (y) was red: Property Description True? 1 Every node in the tree is red or black yes 2 The root is black yes 3 Every leaf (T.nil) is black yes 4 If a node is red, both children are black yes 5 All paths from any given node to the yes descendant leaves contain the same number of black nodes.

5 If Spliced out node is Black:
b) If the spliced out node, y, was black: Property Description True? 1 Every node in the tree is red or black yes 2 The root is black ? 3 Every leaf (T.nil) is black yes 4 If a node is red, both children are black ? 5 All paths from any given node to the ? descendant leaves contain the same number of black nodes.

6 Restoring Property 5 To restore the equal number of black nodes on each path, we "push" the black color of the removed node onto its child. We imagine that the child now has an "extra" black. If the child has color = BLACK, it is now double-black If the child has color = RED, it is now red-black (Note: we don't actually change the node in the code). Property 5 now holds, but property 1 is now violated. The child is neither red nor black.

7 Restoring Property 1 We restore property 1 by pushing the "extra" black up the tree until one of the following holds: x points to a red-black node. In this case we color the node black (single), and we are done. 2) x points to the root of the tree. If x is doubly black, we remove the extra black (and color the root singly black). Removing the extra black from the root doesn't affect paths from root to leaf. 3) We reach a point where we can perform rotations and re-orderings so that we can remove the extra black without creating more violations of the Red-Black Tree property.

8 Cases to Consider We will consider 4 cases for removing the extra black: We will only consider cases where x is the left child. Cases for x as the right child are symmetric to this. We will assign a pointer, w, to x's sibling. (w = (x.p).right]) Case 1: x's sibling, w, is red Case 2: x's sibling, w, is black. Both of w's children are black. Case 3: x's sibling, w, is black. w's left child is red and w's right child is black. Case 4: x's sibling, w, is black. w's right child is red.

9 Case 1 Case 1: x's sibling is red.
Convert case 1 to case 2, 3, or 4 by switching colors of w and x.p and then performing a left rotation on x.p C D C B w x C A D B C E a x e z b C A C C E C w g a g d e z b d Pseudocode: if w.color == RED w.color = BLACK (x.p).color = RED Left-Rotate(T, x.p) w =(x.p).right

10 Case 2 Case 2: x's sibling, w, is black. w has 2 black children.
Remove one black from both x and w (x becomes singly black, w becomes red). Push "extra" black onto x.p. Move x pointer to x.p. x B c+b B c Pseudocode: if (w.left).color == BLACK and (w.right).color ==BLACK w.color = RED x = x.p w C A x D C A C D a a b b C C E C C E g g d e z d e z Note: if node B was previously red, it is now red-black. We can change it to black and be finished.

11 Case 3 Case 3: x's sibling, w, is black. w's left child is red. w's right child is black. Convert to case 4 by switching the colors of w and its left child and then performing a rotation on w. Pseudocode: else if (w.right).color == BLACK (w.left).color = BLACK w.color = RED Right-Rotate(T, w) w = (x.p).right B c B c w w x C A x C A C D C a a b C E b C g D g d e z d C E e z

12 Case 4 Case 4: x's sibling, w, is black and w's right child is red.
We can remove the extra black after making color changes and performing a left-rotate. D c B c w x C A C B C E C D C A e z a b C C E c' c' Pseudocode: w.color = (x.p).color (x.p).color = BLACK (w.right).color = BLACK Left-Rotate(T, x.p) x = T.root a g g b d d e z Full code on p. 326 of text Running time of fixup?


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II"

Similar presentations


Ads by Google