Data Structures2

advertisement

CS6045: Advanced Algorithms

Data Structures

Red-Black Trees

• Red-black trees :

– A variation of binary search trees

– Operations designed to guarantee that the height h = O(lg n )

• Red-black trees properties :

1. Every node is either red or black

2. Every leaf (NULL pointer) is black

Note: this means every internal node has 2 children

3. If a node is red, both children are black

Note: can’t have 2 consecutive reds on a path

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees

• Height of a node: number of edges in a longest path to a leaf

• black-height of a node x: bh(x) is # black nodes on path from x to leaf, without counting x

Red-Black Tree?

Red-black properties:

1. Every node is either red or black

2. Every leaf (NULL pointer) is black

3. If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Tree?

Red-black properties:

1. Every node is either red or black

2. Every leaf (NULL pointer) is black

3. If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Implications of the Properties

• If a red node has any children, it must have two children and they must be _____

• A: Black

• If a black node has only one child that child must be a ____________

• A: Red leaf

Height of Red-Black Trees

• What is the minimum black-height bh(x) of a node x with height h ?

• A: a heighth node has black-height

 h /2

• Proof: By property 4,

<= h/2 nodes on the path from the node to a leaf are red

– Hence

>= h/2 are black

Height of Rad-Black Tree

• Claim: A subtree rooted at a node x contains at least

2 bh(x) - 1 internal nodes

Proof: by induction

– 1. x is a leaf, bh(x) = 0 , # internal node = 2 0 -1 = 0

– 2. height of x be h and bh(x) = b ,

– Any child of x had height h -1 and black height bh(x) either b

(if the child is red) or b -1 (if the child is black)

– By induction, each child of x has >= 2 bh(x)-1 – 1 internal nodes

– 3. the subtree rooted at x contains >= 2 (2 bh(x)-1 – 1 ) + 1 =

2 bh(x) – 1 internal nodes

Height of Rad-Black Tree

• Theorem: A red-black tree with n internal nodes has height h

2 lg( n + 1)

• Proof: by previous claim, the subtree rooted at a node x contains >= 2 bh(x) - 1 internal nodes

• A height h node has black height >= h/2

• Thus n >= 2 h/2 – 1 n + 1 >= 2 h/2 lg(n+1) >= h/2 h <= 2lg(n+1)

• A red-black tree has

O(lg n) height

RB Trees: Worst-Case Time

• Corollary: These operations take O(lg n) time:

– Minimum(), Maximum()

– Successor(), Predecessor()

– Search()

• Insert() and Delete():

– Will also take O(lg n ) time

– But will need special care since they modify tree

Red-Black Trees: An Example

Red-black properties:

1.

Every node is either red or black

2.

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

Insert 8

■ Where does it go?

5

7

9

8 12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 8

Where does it go?

What color should it be?

5

7

9

8 12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 8

Where does it go?

What color should it be?

5

7

9

8 12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 11

Where does it go?

7

5 9

8 12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 11

Where does it go?

What color?

5

7

9

8

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 11

Where does it go?

What color?

• Can’t be red! (#3)

5

7

9

8

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 11

Where does it go?

What color?

• Can’t be red! (#3)

• Can’t be black! (#4)

5

7

8

9

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 11

Where does it go?

What color?

• Solution: recolor the tree

5

7

8

9

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 10

Where does it go?

7

5 9

8

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

Red-Black Trees:

The Problem With Insertion

• Insert 10

Where does it go?

What color?

5

7

9

8

11

12

1.

2.

Every node is either red or black

Every leaf (NULL pointer) is black

3.

If a node is red, both children are black

4. Every path from node to descendent leaf contains the same number of black nodes

5. The root is always black

10

Red-Black Trees:

The Problem With Insertion

• Insert 10

Where does it go?

What color?

• A: no color! Tree is too imbalanced

5

• Must change tree structure to allow recoloring

– Goal: restructure tree in

O(lg n) time

7

8

9

10

11

12

RB Trees: Rotation

• Basic tree-restructuring structure is called rotation :

A x y

B

C rightRotate(y) leftRotate(x)

A x

B y

C

• Left rotation and right rotation are inverses of each other

RB Trees: Rotation y x rightRotate(y) x C A y

A B B C

• Only pointers are changed

– x keeps its left child

– y keeps its right child

– x ’s right child becomes y ’s left child

– x ’s and y ’s parents change

Does rotation preserve inorder key ordering?

Rotation Example

RB Trees: Rotation

• Running Time

O(1) //constant number of points are modified

Red-Black Trees: Insertion

• Insertion: the basic idea

– Insert x into tree, color x red

– Only r-b property 3 might be violated

(if x.p

is red)

• If so, move violation node up tree until a place is found where it can be fixed (till x.p

is black)

– Total time will be O(lg n)

RB Insert: Case 1 if (y.color == RED) z.p.color = BLACK; y.color = BLACK; z.p.p.color = RED; z = z.p.p;

• Case 1: “uncle” is red

• In figures below, all  ’s are equal-black-height subtrees

C

A

B z

  case 1

D y

  if z is a right child

C new z

A

B

 

D

 

Change colors of some nodes, preserving #4: all downward paths have equal bh

The while loop now continues with x

’s grandparent as the new x

RB Insert: Case 1 if (y.color == RED) z.p.color = BLACK; y.color = BLACK; z.p.p.color = RED; z = z.p.p;

• Case 1: “uncle” is red

• In figures below, all  ’s are equal-black-height subtrees

C

B z

 

A D y

   if z is a left child case 1

B z

 

A

C new z

D

 

Same action whether x is a left or a right child

RB Insert: Case 2 if (z == z.p.right) z = z.p;

LEFT-ROTATE(T,z);

// continue with case 3 code

A

B

C z

 

 y

Case 2:

– “Uncle” is black

Node z is a right child

• Transform to case 3 via a left-rotation

C case 2

B

 y

A z

 

Transform case 2 into case 3 with a left rotation

This preserves property 4: all downward paths contain same number of black nodes

RB Insert: Case 3 z.p.color = BLACK; z.p.p.color = RED;

RIGHT-ROTATE(T,z.p.p);

Case 3:

– “Uncle” is black

Node z is a left child

• Change colors; rotate right

C B

 y case 3

A z

 

B

  z A

 

C

Perform some color changes and do a right rotation

Again, preserves property 4: all downward paths contain same number of black nodes

RB Insert: Cases 4-6

• Cases 1-3 hold if z ’s parent is a left child

• If z

’s parent is a right child, cases 4-6 are symmetric (swap left for right)

• Running time: O(lg n)

Red-Black Trees: Deletion

• Rules for BST Deletion

– If vertex to be deleted is a leaf, just delete it

– If vertex to be deleted has just one child, replace it with that child

– If vertex to be deleted has two children, replace it with it’s successor, then goes to the first 1 and 2 cases

Review BST Delete

• 3 cases:

– x has no children: Remove x

– x has one child: Splice out x

7 7

2

3

4

5 8 5

6 9 3 6

6

– x has two children:

2 4 6

• Swap x with successor

• Perform case 1 or 2 to delete it

9

2

1 3

4

5

6

6

7

8

9 3

1 2

4

5

6

6

7

8

9 3

1 4

5

6

6

7 z z

8

9 z exchange successor ( z )

Red-Black Tree Deletion

• If the deleted node is red?

– Not a problem,

– No r-b properties violated

• If the deleted node is black?

– If the node is not the root, deleting it will change the black height along some path

Red-Black Deletion

• The goal of r-b deletion

– Delete a red leaf

• General idea

– As we traverse the tree looking for the leaf to delete, we change every node we encounter to red

– If this causes a violation of the r-b properties, we fix it

Red-Black: Deletion

• Base case: check the root

– If both of root’s children are black

• Make the root red

• Move x to the appropriate child of the root

• Proceed to the main case

– Otherwise, designate the root as x and proceed to case 1

Red-Black: Deletion

• Main case: keeping traverse down the tree ,

• Case 1: x has at least on red child

– Down the tree to the next level

– If the new x is red, continue down

• Case 2: x has two black children, sibling has 2 black children

• Case 3: x has two black children, sibling’s left child is Red

• Case 4: x has two black children, sibling’s right child is Red

Red-Black: Deletion

• Terminate case:

– Find the node to be deleted – a leaf or a node with one child that is a leaf (case 1 or 2 of BST deletion)

– Case 1: delete the appropriate node (a red leaf)

– Case 2: delete the appropriate node and color its child as black

– Color the root black

RB Delete: Case 1

Case 1: “sibling” t is red

 x A

B case 1

D t

C E

  

D

B x A

C

  

E

 

Make t black and x.p

red; then left rotate on x.p

RB Delete: Case 2

• Case 2: “sibling” t is black, and both of t

’s children are black

 x A

B case 2

D t

C E

  

A

B

D

C E

  

Make x , t red and

If x.p

was red, make it black

RB Delete: Case 3

• Case 3: “sibling” t is black, t

’s left child is red, and t

’s right child is black

 x A

B B case 3

C

D t

E

  

 x A

C

D

E

Make t red and t

’s left child black, then right rotate on t

RB Delete: Case 4

• Case 4: “sibling” t is black, and t

’s right child is red

 x A

B case 4

D t

C E

   

D

B

A C

  

E

 

Make x , and t red, and make t ’s right child and x.p

black then left rotate on x.p

RB Delete: Case 5 - 8

• Cases 1-4 hold if x is a left child

• If x is a right child, cases 5-8 are symmetric (swap left for right)

• Running time: O(lg n)

• Delete 10

Example 1 x

Base Case

Example 1

• Delete 10 x

Case 1

• Delete 10

Example 1 x

• Delete 10

Example 1 x

• Delete 10

Example 1

• Delete 10

Example 2 x

Case 1

• Delete 10 x

Example 2

• Delete 10

Example 2

Case 2 x

• Delete 10

Example 2 x

Case 2

• Delete 10

Example 2

• Delete 10

Example 2

• Delete 11

Example 3

Base Case x

• Delete 11 x

Case 1

Example 3

• Delete 11

Example 3

Case 1 x

• Delete 11

Example 3 x

• Delete 11

Example 3

Case 2 x

• Delete 11

Example 3 x

Case 2

• Delete 11

Example 3

• Delete 11

Example 3

Download