mudasser naseer 1 10/20/2015 csc 201: design and analysis of algorithms lecture # 11 red-black trees

35
Mudasser Naseer 1 06/27/2 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Upload: vanessa-austin

Post on 02-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 1 04/20/23

CSC 201: Design and Analysis of Algorithms

Lecture # 11

Red-Black Trees

Page 2: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 2 04/20/23

Red-Black Trees

● Red-black trees:■ Binary search trees augmented with node color ■ Balanced: height is O(lg n), where n is the number

of nodes.■ Operations will take O(lg n) time in the worst case.

● First: describe the properties of red-black trees● Then: prove that these guarantee h = O(lg n)● Finally: describe operations on red-black trees

Page 3: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 3 04/20/23

Red-Black Properties

● The red-black properties:1. Every node is either red or black

2. The root is always black.

3. Every leaf (NULL pointer (nil[T])) is black○ Note: this means every “real” node has 2 children

4. If a node is red, both children are black○ Note: can’t have 2 consecutive reds on a path

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

Page 4: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 4 04/20/23

Page 5: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 5 04/20/23

Red-Black Trees

● Put example on board and verify properties:1. Every node is either red or black

2. The root is always black.

3. Every leaf (NULL pointer) is black

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

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

● black-height bh(x): # black nodes on path from x to leaf (including nil[T] but not counting x)■ Label example with h and bh values

Page 6: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 6 04/20/23

Page 7: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 7 04/20/23

Height of Red-Black Trees

● What is the minimum black-height of a node with height h?

● A: a height-h 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.

● Theorem: A red-black tree with n internal nodes has height h 2 lg(n + 1)

● How do you suppose we’ll prove this?■ {An internal node or inner node is any node of a tree that has

child nodes and is thus not a leaf node.}

Page 8: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 8 04/20/23

RB Trees: Proving Height Bound

● Prove: n-node RB tree has height h 2 lg(n+1)

● Claim: A subtree rooted at a node x contains at least 2bh(x) - 1 internal nodes■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node)

○ What is bh(x)?

Page 9: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 9 04/20/23

RB Trees: Proving Height Bound

● Prove: n-node RB tree has height h 2 lg(n+1)● Claim: A subtree rooted at a node x contains at

least 2bh(x) - 1 internal nodes■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node)

○ What is bh(x)?○ A: 0○ So…subtree contains 2bh(x) - 1

= 20 - 1 = 0 internal nodes (TRUE)

Page 10: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 10 04/20/23

RB Trees: Proving Height Bound

● Inductive proof that subtree at node x contains at least 2bh(x) - 1 internal nodes■ Inductive step: x has positive height and 2 children

○ Each child has black-height of bh(x) or bh(x)-1 (Why?)○ The height of a child = (height of x) - 1○ So the subtrees rooted at each child contain at least

2bh(x) - 1 - 1 internal nodes○ Thus subtree at x contains

(2bh(x) - 1 - 1) + (2bh(x) - 1 - 1) + 1= 2•2bh(x)-1 - 1 = 2bh(x) - 1 nodes

Page 11: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 11 04/20/23

RB Trees: Proving Height Bound

● Thus at the root of the red-black tree:n 2bh(root) - 1 (Why?)

n 2h/2 - 1 (Why?)

lg(n+1) h/2 (Why?)

h 2 lg(n + 1) (Why?)

Thus h = O(lg n)

Page 12: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 12 04/20/23

RB Trees: Worst-Case Time

● So we’ve proved that a red-black tree has O(lg n) height

● 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

Page 13: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 13 04/20/23

Red-Black Trees: An Example

● Color this tree: 7

5 9

1212

5 9

7

Red-black properties:1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

Page 14: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 14 04/20/23

● Insert 8■ Where does it go?

Red-Black Trees: The Problem With Insertion

12

5 9

7

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

Page 15: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 15 04/20/23

● Insert 8■ Where does it go?■ What color

should it be?

Red-Black Trees: The Problem With Insertion

12

5 9

7

8

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

Page 16: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 16 04/20/23

● Insert 8■ Where does it go?■ What color

should it be?

Red-Black Trees: The Problem With Insertion

12

5 9

7

8

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

Page 17: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 17 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 11■ Where does it go?

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

Page 18: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 18 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 11■ Where does it go?■ What color?

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

Page 19: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 19 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 11■ Where does it go?■ What color?

○ Can’t be red! (#3)

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

Page 20: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 20 04/20/23

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)

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

Page 21: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 21 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 11■ Where does it go?■ What color?

○ Solution: recolor the tree

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

Page 22: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 22 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 10■ Where does it go?

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

Page 23: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 23 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 10■ Where does it go?■ What color?

1. Every node is either red or black2. Every leaf (NULL pointer) is black3. If a node is red, both children are black4. Every path from node to descendent leaf

contains the same number of black nodes5. The root is always black

12

5 9

7

8

11

10

Page 24: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 24 04/20/23

Red-Black Trees:The Problem With Insertion

● Insert 10■ Where does it go?■ What color?

○ A: no color! Tree is too imbalanced

○ Must change tree structureto allow recoloring

■ Goal: restructure tree in O(lg n) time

12

5 9

7

8

11

10

Page 25: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 25 04/20/23

RB Trees: Rotation

● Our basic operation for changing tree structure is called rotation:

● Does rotation preserve inorder key ordering?● What would the code for rightRotate() actually

do?

y

x C

A B

x

A y

B C

rightRotate(y)

leftRotate(x)

Page 26: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 26 04/20/23

rightRotate(y)

RB Trees: Rotation

● Answer: A lot of pointer manipulation■ 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

● What is the running time?

y

x C

A B

x

A y

B C

Page 27: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 27 04/20/23

Rotation Example

● Rotate left about 9:

12

5 9

7

8

11

Page 28: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 28 04/20/23

Rotation Example

● Rotate left about 9:

5 12

7

9

118

Page 29: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 29 04/20/23

Rotation Example

● After Colouring

9

118

Page 30: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Rotation

LEFT-ROTATE(T, x)

1 y ← right[x] % Set y.

2 right[x] ← left[y] % Turn y’s left subtree into x’s right subtree.

3 if left[y] = nil[T ]

4 then p[left[y]] ← x

5 p[y] ← p[x] % Link x’s parent to y.

6 if p[x] = nil[T ]

7 then root[T ] ← y

8 else if x = left[p[x]]

9 then left[p[x]] ← y

10 else right[p[x]] ← y

11 left[y] ← x % Put x on y’s left.

12 p[x] ← y

Mudasser Naseer 30 04/20/23

Page 31: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Rotation - Example

Mudasser Naseer 31 04/20/23

Page 32: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

● The longest possible path from the root to a leaf is no more than twice as long as the shortest possible path. The result is that the tree is roughly balanced.

● The shortest possible path has all black nodes, and the longest possible path alternates between red and black nodes.

● All maximal paths have the same number of black nodes, by property 5, this shows that no path is more than twice as long as any otherpath.

Mudasser Naseer 32 04/20/23

Page 33: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Time Taken

● Read-only operations: binary search trees. ● Insertion or removal may violate the properties

of a red-black tree. ● Restoring the red-black properties requires a

small number (O(lg n)) of color changes and no more than three tree rotations (two forinsertion). Although insert and deleteoperations are complicated, their times remainO(lg n).

Mudasser Naseer 33 04/20/23

Page 34: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Advantages

● Red-black trees offer the best possible worst-case insertion time, deletion time, and search time.

Mudasser Naseer 34 04/20/23

Page 35: Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Applications

● Time-sensitive applications such as real-time applications.

● Data structures used in computational geometry.

● Valuable in functional programming where they are one of the most common persistent data structures, used to construct associative arrays.

Mudasser Naseer 35 04/20/23