Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II

2 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 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 nil(T)) Case 2: Node, z, has one non-nil child. Splice out node by connecting p[z] to child[z] Case 3: Node, z, has two non-nil children. Find immediate successor. Copy data from successor to z (keep color[z] same) Delete successor.

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

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

6 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 7 Restoring Property 1 We restore property 1 by pushing the "extra" black up the tree until one of the following holds: 1)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 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  right[p[x]]) 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 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 p[x] and then performing a left rotation on p[x] D    x w  B    x w   Pseudocode: if color[w] = RED then color[w]  BLACK color[p[x]]  RED Left-Rotate(T, p[x]) w  right[p[x]] C A C B C C C E C D C E C A C C

10 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 p[x]. Move x pointer to p[x]. B    x w  c B D    x  c+b Pseudocode: if color[left[w]] = BLACK and color[right[w]]=BLACK then color[w]  RED x  p[x] Note: if node B was previously red, it is now red-black. We can change it to black and be finished. C A C A C C C C C E C E C D

11 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. B    x w C  cB    x w D   c Pseudocode: else if color[right[w]]=BLACK then color[left[w]]  BLACK color[w]  RED Right-Rotate(T, w) w  right[p[x]] C A C A C C C D C E C E

12 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. B    x E w C  c c' D    C   c Pseudocode: color[w]  color[p[x]] color[p[x]]  BLACK color[right[w]]  BLACK Left-Rotate(T, p[x]) x  root[T] Full code on p. 326 of text Running time of fixup? C A C A C B C D C E


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II."

Similar presentations


Ads by Google