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

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

description

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.

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

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

1

Algorithms

CSCI 235, Fall 2015Lecture 25

Red Black Trees II

Page 2: 1 Algorithms CSCI 235, Fall 2015 Lecture 25 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.

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

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.

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

4

When do violations of Red-Black Properties occur?

a) Suppose spliced out node (y) was red:

Property Description True?1 Every node in the tree is red or black yes2 The root is black yes3 Every leaf (nil[T]) is black yes4 If a node is red, both children are black yes5 All paths from any given node to the yes

descendant leaves contain the same numberof black nodes.

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

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 yes2 The root is black ?3 Every leaf (nil[T]) is black yes4 If a node is red, both children are black ?5 All paths from any given node to the ?

descendant leaves contain the same numberof black nodes.

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

6

Restoring Property 5To 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-blackIf 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.

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

7

Restoring Property 1We 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.

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

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

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

9

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

xw

B

x w

Pseudocode:if color[w] = RED then

color[w] BLACKcolor[p[x]] REDLeft-Rotate(T, p[x])w right[p[x]]

CA

CB

CC CE

CD

CE

CA CC

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

10

Case 2Case 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

xw

c B

D

x

c+b Pseudocode:if color[left[w]] = BLACK and color[right[w]]=BLACK then

color[w] REDx p[x]

Note: if node B was previously red, it is now red-black. We can change it to black and be finished.

CACA

CCCC CECE

CD

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

11

Case 3Case 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

xw

C

c B

xw

D

cPseudocode:else if color[right[w]]=BLACK then color[left[w]] BLACK

color[w] REDRight-Rotate(T, w)w right[p[x]]

CACA CCCD

CE

CE

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

12

Case 4Case 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

c'Pseudocode:color[w] color[p[x]]color[p[x]] BLACKcolor[right[w]] BLACKLeft-Rotate(T, p[x])x root[T]

Full code on p. 326 of textRunning time of fixup?

CA

CA CBCD CE