cse 780: design and analysis of algorithms

58
CSE 2331/5331 CSE2331/5331 Topic 9: Balanced search trees Rotate operation Red-black tree Augmenting data struct.

Upload: winter

Post on 23-Feb-2016

24 views

Category:

Documents


0 download

DESCRIPTION

CSE 780: Design and Analysis of Algorithms. Lecture 9: Hash Tables. Dictionary Operations. Given a universe of elements U Need to store some keys Need to perform the following for keys Insert Search Delete. Let’s call this a dictionary. . First Try: . - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

CSE2331/5331

Topic 9: Balanced search trees

Rotate operationRed-black tree

Augmenting data struct.

Page 2: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Set Operations Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Page 3: CSE 780: Design and Analysis of Algorithms

Motivation Using binary search trees,

most operations take time O(h) with h being the height of the tree

Want to keep h small the best one can hope for. (why?)

We can sort the elements, and construct a balanced binary search tree But then how do we maintain it under dynamic

operations (insertion / deletion)?

CSE 2331/5331

Page 4: CSE 780: Design and Analysis of Algorithms

Balanced Search Tree

CSE 2331/5331

Page 5: CSE 780: Design and Analysis of Algorithms

Un-balanced Search Tree

CSE 2331/5331

Page 6: CSE 780: Design and Analysis of Algorithms

Goal:

Maintain a balanced binary search tree with height such that all operations take Maintain means that the tree is always of height after

any operation supported, especially insertion and deletion.

There are multiple such balanced trees We focus on the so-called Red-black trees

CSE 2331/5331

Page 7: CSE 780: Design and Analysis of Algorithms

Rotation Operation A key operation in maintaining the “balance” of a

binary search tree Locally rotate subtree, while maintain binary search

tree property

CSE 2331/5331

x

y

A B

C

y

xA

B C

right rotation

left rotation

Page 8: CSE 780: Design and Analysis of Algorithms

Right Rotation Examples

Right rotation at 9.

CSE 2331/5331

Right rotation at 7 ? Right rotation at 13 ?

Does it changeBinary search tree

property?

Page 9: CSE 780: Design and Analysis of Algorithms

Left Rotation Examples

Left rotation at node 3.

CSE 2331/5331

what if we then right-rotate at node 6 ?

left and right rotationsare inverse of each other.

Page 10: CSE 780: Design and Analysis of Algorithms

Recall: Transplant

CSE 2331/5331

Page 11: CSE 780: Design and Analysis of Algorithms

Implementation of Right Rotation

CSE 2331/5331

Running time:

Page 12: CSE 780: Design and Analysis of Algorithms

Implementation of Left Rotation

CSE 2331/5331

Running time:

Page 13: CSE 780: Design and Analysis of Algorithms

Rotation does not change binary search tree property!

CSE 2331/5331

x

y

A B

C

y

xA

B C

right rotation

left rotation

Page 14: CSE 780: Design and Analysis of Algorithms

Rotation to Re-balance

Apply rotation to node 5

CSE 2331/5331

Reduce height to 3!

Page 15: CSE 780: Design and Analysis of Algorithms

Rotation to Re-balance

Apply rotation at node 5

CSE 2331/5331

Height not reduced!

Need a double-rotation(first at 16, then at 5)

In general, how and when?

Page 16: CSE 780: Design and Analysis of Algorithms

Red-Black Trees

Compared to an ordinary binary tree, a red-black binary tree: A leaf node is NIL Hence all nodes are either NIL or have exactly two

children! Each node will have a color, either red or black

CSE 2331/5331

Page 17: CSE 780: Design and Analysis of Algorithms

Definition

A Red-Black tree is a binary tree with the following properties: Every node is either red or black Root is black Every leaf is NIL and is black If a node is red, then both its children are black For each node, all simple paths from this node to its

decedent leaves contain same number of black nodes

CSE 2331/5331

Page 18: CSE 780: Design and Analysis of Algorithms

An Example

Double nodes are black.

CSE 2331/5331

No two consecutive red nodes.

Page 19: CSE 780: Design and Analysis of Algorithms

Skipping Leaves

CSE 2331/5331

Page 20: CSE 780: Design and Analysis of Algorithms

Is This a Red-Black Tree?

CSE 2331/5331

Page 21: CSE 780: Design and Analysis of Algorithms

Exercise

Color the following tree to make a valid red-black tree

CSE 2331/5331

Page 22: CSE 780: Design and Analysis of Algorithms

Given a tree node x size(x): the total number of internal nodes contained in

the subtree rooted at x bh(x): the number of black nodes on the path from x to

leaf (not counting x)

CSE 2331/5331

Page 23: CSE 780: Design and Analysis of Algorithms

Balancing Property of RB-tree

Lemma [BN-Bound]: Let r be the root of tree T. Then .

Proof: Since every root-leaf path has black nodes, contains a

complete binary tree of height as subtree. For a complete binary tree of height its size is Hence

CSE 2331/5331

Page 24: CSE 780: Design and Analysis of Algorithms

Balancing Property of RB-tree

Theorem [RB-Height] A red-black tree with n internal nodes has height .

Proof: Let r be the root of this red-black tree Since no red node has a red child, By Lemma [BN-Bound], Thus,

CSE 2331/5331

Page 25: CSE 780: Design and Analysis of Algorithms

Implication of the Theorem

CSE 2331/5331

In other words, if we can maintain a RB-treeunder every operation, then the tree always

has height, and All operations thus all take time.

How to maintain RB-tree underOperations?

Page 26: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Set Operations Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Only need to considerInsert / delete

Page 27: CSE 780: Design and Analysis of Algorithms

Insertion Example

CSE 2331/5331

Insert 24?Insert 2?Insert 36?

Page 28: CSE 780: Design and Analysis of Algorithms

Red-Black Tree Insert

CSE 2331/5331

Page 29: CSE 780: Design and Analysis of Algorithms

Insert Fixup: Case 3 (z is the new node)

CSE 2331/5331

Page 30: CSE 780: Design and Analysis of Algorithms

Example: Case 3

CSE 2331/5331

Insert 12?

Page 31: CSE 780: Design and Analysis of Algorithms

Implementation of Fixup Case 3

CSE 2331/5331

Page 32: CSE 780: Design and Analysis of Algorithms

Remarks

Running time of RBInsertFixupC

If the tree has red-black properties if not counting violation caused by z Then after RBInsertFixupC the tree is a red-black tree! No more operations needed.

CSE 2331/5331

Page 33: CSE 780: Design and Analysis of Algorithms

Insert Fixup: Case 2

CSE 2331/5331

Page 34: CSE 780: Design and Analysis of Algorithms

Example: Case 2

CSE 2331/5331

Insert 14?

Page 35: CSE 780: Design and Analysis of Algorithms

Implementation of Case 2

CSE 2331/5331

Running time

Page 36: CSE 780: Design and Analysis of Algorithms

Insert Fixup: Case 1: (z is new node)

The parent and uncle of z are red: Color the parent and uncle of z both black Color the grandparent of z red Continue with the grandparent of z

CSE 2331/5331

Page 37: CSE 780: Design and Analysis of Algorithms

Sibling

CSE 2331/5331

Takes time

Page 38: CSE 780: Design and Analysis of Algorithms

Implementation of Case 1

CSE 2331/5331

When does this procedure terminate?

Running time:

Page 39: CSE 780: Design and Analysis of Algorithms

Red-black Tree Insert Fixup Function RBInsertFixup (T, z)

RBInsertFixupA (T, z); RBInsertFixupB (T, z); RBInsertFixupC (T, z);T.root.color = Black;

Note z changes after each call. Total time copmlexity:

Total # rotations: At most 2

CSE 2331/5331

Page 40: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331/5331

Insert 21?

Page 41: CSE 780: Design and Analysis of Algorithms

Augmenting Data Structure

CSE 2331/5331

Page 42: CSE 780: Design and Analysis of Algorithms

CSE 2331/5331

Balanced Binary Search Tree Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Also supportSelect

operation ?

Page 43: CSE 780: Design and Analysis of Algorithms

Augment Tree Structure Select ( T, k ) Goal:

Augment the binary search tree data structure so as to support Select ( T, k ) efficiently

Ordinary binary search tree O(h) time for Select(T, k)

Red-black tree (balanced search tree) O(lg n) time for Select(T, k)

CSE 2331/5331

Page 44: CSE 780: Design and Analysis of Algorithms

How To Augment Tree Structure?

At each node x of the tree T store x.size = # nodes in the subtree rooted at x

Include x itself If a node (leaf) is NIL, its size is 0.

Space of an augmented tree:

Basic property:

CSE 2331/5331

Page 45: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

Page 46: CSE 780: Design and Analysis of Algorithms

How to Setup Size Information?

procedure AugmentSize( ) If () then

= AugmentSize( ); = AugmentSize( );; Return( );

end Return ();

CSE 2331/5331

Postorder traversalof the tree !

Page 47: CSE 780: Design and Analysis of Algorithms

Augmented Binary Search Tree

Let T be an augmented binary search tree OS-Select(x, k):

Return the k-th smallest element in the subtree rooted at x

OS-Select(T.root, k) returns the k-th smallest elements in the entire tree.

CSE 2331/5331

OS-Select(T.root, 5) ?

Page 48: CSE 780: Design and Analysis of Algorithms

Correctness? Running time?

O(h)

CSE 2331/5331

Page 49: CSE 780: Design and Analysis of Algorithms

OS-Rank(T, x) Return the rank of the element x in the linear order

determined by an inorder walk of T

CSE 2331/5331

Page 50: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

OS-Rank(T, M) ? OS-Rank(T, D) ?

Page 51: CSE 780: Design and Analysis of Algorithms

Correctness ? Time complexity?

O(h)

CSE 2331/5331

Page 52: CSE 780: Design and Analysis of Algorithms

Need to maintain augmented information under dynamic operations

Insert / delete Extract-Max can be implemented with delete

CSE 2331/5331

Are we done ?

Page 53: CSE 780: Design and Analysis of Algorithms

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

Insert(J) ?

Page 54: CSE 780: Design and Analysis of Algorithms

During the downward search, increase the size attribute of each node visited along the path from root to the final insert location.

Time complexity: O(h)

However, if we have to maintain balanced binary search tree, say Red-black tree Also need to adjust size attribute after rotation

CSE 2331/5331

Page 55: CSE 780: Design and Analysis of Algorithms

Left-Rotate

y.size = x.size x.size = x.left.size + x.right.size + 1 O(1) time per rotation

CSE 2331/5331

Page 56: CSE 780: Design and Analysis of Algorithms

Right-rotate can be done similarly.

Overall: Two phases: Update size for all nodes along the path from root to

insertion location O(h) = O(lg n) time

Update size for the Fixup stage involving O(1) rotations

O(1) + O(lg n) = O(lg n) time O(h) = O(lg n) time to insert in a Red-Black tree

Same asymptotic time complexity as the non-augmented version

CSE 2331/5331

Page 57: CSE 780: Design and Analysis of Algorithms

Delete

Two phases: Decrement size in each node on the path from the root

to the node to be deleted O(h) = O(lg n) time

During Fixup (to maintain balanced binary search tree property), update size for O(1) rotations

O(1) + O(lg n) time Overall:

O(h) = O(lg n) time

CSE 2331/5331

Page 58: CSE 780: Design and Analysis of Algorithms

Summary

Simple example of augmenting data structures In general, the augmented information can be

quite complicated Can be a separate data structure!

Need to consider how to maintain such information under dynamic changes

CSE 2331/5331