Top Banner
Introduction to Algorithms Jiafen Liu Sept. 2013
35

Introduction to Algorithms

Jan 21, 2016

Download

Documents

mandell

Introduction to Algorithms. Jiafen Liu. Sept. 2013. Today’s Tasks. Balanced Search Trees Red-black trees Height of a red-black tree Rotations Insertion. Balanced Search Trees. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Algorithms

Introduction to Algorithms

Jiafen Liu

Sept. 2013

Page 2: Introduction to Algorithms

Today’s Tasks

Balanced Search Trees

• Red-black trees – Height of a red-black tree

– Rotations

– Insertion

Page 3: Introduction to Algorithms

Balanced Search Trees

• Balanced search tree: A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items.

• Examples: – AVL Tree– 2-3-4 Tree– B Tree– Red-black Tree

Page 4: Introduction to Algorithms

Red-black trees

• This data structure requires an extra 1-bit color field in each node.

• Red-black properties: – Every node is either red or black. – The root and leaves (NIL’s) are black. – If a node is red, then its parent is black.– All simple paths from any node x to a

descendant leaf have the same number of black nodes = black-height(x).

Page 5: Introduction to Algorithms

Example of a red-black tree

• Convention: black-height of x does not include x itself.

Page 6: Introduction to Algorithms

Example of a red-black tree

• It could have a bunch of blacks, but it will never repeat two reds in a row.

Page 7: Introduction to Algorithms

Goals of red-black trees

• there are a couple of goals that we are trying to achieve. – These properties should force the tree to have

logarithmic height, O(lgn) height.– The other desire we have from these properties

is that they are easy to maintain.• We can create a tree in the beginning that has this

property.• A perfectly balanced binary tree with all nodes black

will satisfy those properties. • The tricky part is to maintain them when we make

changes to the tree.

Page 8: Introduction to Algorithms

Height of a red black tree

• Let's look at the height of a red black tree. And we will start to see where these properties come from.

• Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1) .– Can be proved by induction, as in the book

P164.– Another informal way: intuition.

Page 9: Introduction to Algorithms

Intuition

• Merge red nodes into their black parents.

• This process produces another type of balanced search tree: 2-3-4 tree.– Any guesses why it's called a 2-3-4 tree?– Another nice property:

• All of the leaves have the same depth. Why?

• By Property 4

Page 10: Introduction to Algorithms

height of merged tree h'

• Now we will prove the height of merged tree h' .

• The first question is how many leaves are there in a red-black tree?

– #(internal nodes) +1 – It can also be proved by induction. Try it.

Page 11: Introduction to Algorithms

Height of Red-Black Tree

• The number of leaves in each tree is n + 1

• 2h' ≤ n + 1 ≤ 4h'

• h' ≤ lg(n + 1)

• How to connect h' and h?

• We also have h' ≥ 1/2 h

• h ≤ 2 lg(n + 1).

Page 12: Introduction to Algorithms

Query operations

• Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lgn) time on a red-black tree with n nodes.

Page 13: Introduction to Algorithms

Modifying operations

• The operations INSERT and DELETE cause modifications to the red-black tree.

• How to INSERT and DELETE a node in Red Black Tree?– The first thing we do is just use the BST

operation. – They will preserve the binary search tree

property, but don't necessarily preserve balance.

Page 14: Introduction to Algorithms

Modifying operations

• The operations INSERT and DELETE cause modifications to the red-black tree.

• How to INSERT and DELETE a node in Red Black Tree?– The second thing is to set color of new

internal node, to preserve property 1.– We can color it red, and property 3 does not

hold.– The good news is that property 4 is still true.

Page 15: Introduction to Algorithms

How to fix property 3?

• We are going to move the violation of property 3 up the tree.

• IDEA: Only red-black property 3 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

• we have to restructure the links of the tree via “rotation”.

Page 16: Introduction to Algorithms

Rotations

• Rotations maintain the inorder ordering of keys.

a α, b β, c γ ∈ ∈ ∈ ⇒ a ≤ A ≤ b ≤ B ≤ c.• A rotation can be performed in O(1) time.

– Because we only change a constant number of pointers.

Page 17: Introduction to Algorithms

Implement of Rotation

• P166

Page 18: Introduction to Algorithms

Example

• Insert 15

Page 19: Introduction to Algorithms

Example

• Insert 15

• Color it red

Page 20: Introduction to Algorithms

Example

• Insert 15

• Color it red

• Handle Property 3 by recoloring

Page 21: Introduction to Algorithms

Example

• Insert 15

• Color it red

• Handle Property 3 by recoloring

Page 22: Introduction to Algorithms

Example

• Insert 15

• Color it red

• Handle Property 3 by recoloring

• Failed!

Page 23: Introduction to Algorithms

RIGHT-ROTATE

• RIGHT-ROTATE(18)

• It turns out to be?

Page 24: Introduction to Algorithms

RIGHT-ROTATE

• We are still in trouble between 10 and 18. But made this straighter.

• It doesn't look more balanced than before.• We can not resolve by recoloring. Then?

Page 25: Introduction to Algorithms

LEFT-ROTATE

• LEFT-ROTATE(7)

• It turns out to be?

Page 26: Introduction to Algorithms

LEFT-ROTATE

• LEFT-ROTATE(7) and recoloring at the same time

• It satisfy all the 4 properties

Page 27: Introduction to Algorithms

Pseudocode of insertionRB-INSERT(T, x)

TREE-INSERT(T, x)

color[x] ← RED // only RB property 3 can be violated

while x ≠ root[T] and color[x] = RED

do if p[x] = left[ p[ p[x] ]

then y ← right[ p[ p[x] ] // y = aunt or uncle of x

if color[y] = RED

then Case 1 ⟨ ⟩

Page 28: Introduction to Algorithms

Case 1

• Let denote a subtree with a black root. • All have the same black-height.

• Push C’s black onto A and D, and recurse. • Caution: We don't know whether B was the right

child or the left child. In fact, it doesn't matter.

How?

Page 29: Introduction to Algorithms

Pseudocode of insertionRB-INSERT(T, x)

TREE-INSERT(T, x)

color[x] ← RED // only RB property 3 can be violated

while x ≠ root[T] and color[x] = RED

do if p[x] = left[ p[ p[x] ]

then y ← right[ p[ p[x] ] // y = aunt/uncle of x

if color[y] = RED

then Case 1 ⟨ ⟩

else if x = right[p[x]] //y is red, x,y have a zigzag

then Case 2 ⟨ ⟩ // This falls into case 3

Page 30: Introduction to Algorithms

Case 2

• Now we have a zigzig.

• we may want a straight path between x.

How?

Page 31: Introduction to Algorithms

Pseudocode of insertionRB-INSERT(T, x)

TREE-INSERT(T, x)

color[x] ← RED // only RB property 3 can be violated

while x ≠ root[T] and color[x] = RED

do if p[x] = left[ p[ p[x] ]

then y ← right[ p[ p[x] ] // y = aunt/uncle of x

if color[y] = RED

then Case 1 ⟨ ⟩

else if x = right[p[x]]

then Case 2 ⟨ ⟩ // This falls into case 3

⟨Case 3 ⟩ //straight path

Page 32: Introduction to Algorithms

Case 3

• Done! No more violations of RB property 3 are possible.

• Property 4 is also preserved.

How?

Page 33: Introduction to Algorithms

Pseudocode of insertionRB-INSERT(T, x)

TREE-INSERT(T, x)

color[x] ← RED // only RB property 3 can be violated

while x ≠ root[T] and color[x] = RED

do if p[x] = left[ p[ p[x] ]

then y ← right[ p[ p[x] ] // y = aunt/uncle of x

if color[y] = RED

then Case 1 ⟨ ⟩

else if x = right[p[x]]

then Case 2 ⟨ ⟩ // This falls into case 3

⟨Case 3 ⟩

else “then” clause with “left” and “right” swapped ⟨ ⟩

color[root[T]] ← BLACK

Page 34: Introduction to Algorithms

Analysis

• Go up the tree performing Case 1, which only recolors nodes.

• If Case 2 or Case 3 occurs, perform 1 or 2 rotations, and terminate.

• Running time of insertion?– O(lgn) with O(1) rotations.

• RB-DELETE — same asymptotic running time and number of rotations as RB-INSERT (see textbook).

Page 35: Introduction to Algorithms