powerpoint presentation - topology based methods in shape...

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

Upload: others

Post on 20-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

CSE 2331/5331

CSE2331/5331

Topic 7:Balanced search trees

Rotate operationRed-black tree

Augmenting data struct.

Page 2: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

CSE 2331/5331

Set Operations

Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Page 3: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Motivation

Using binary search trees, most operations take time O(h) with h being the height

of the tree Want to keep h small

ℎ = Θ(log𝑛𝑛) 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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Balanced Search Tree

CSE 2331/5331

Page 5: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Un-balanced Search Tree

CSE 2331/5331

Page 6: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Goal:

Maintain a balanced binary search tree with height Θ(lg𝑛𝑛) such that all operations take 𝑂𝑂(lg𝑛𝑛) Maintain means that the tree is always of height Θ(lg(#𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑒𝑛𝑛𝑒𝑒𝑒𝑒)) 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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Recall: Transplant

CSE 2331/5331

Page 11: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implementation of Right Rotation

CSE 2331/5331

Running time:Θ(1)

Page 12: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implementation of Left Rotation

CSE 2331/5331

Running time:Θ(1)

Page 13: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Rotation to Re-balance

Apply rotation to node 5

CSE 2331/5331

Reduce height to 3!

Page 15: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

An Example

Double nodes are black.

CSE 2331/5331

No two consecutive red nodes.

Page 19: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Skipping Leaves

CSE 2331/5331

Page 20: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Is This a Red-Black Tree?

CSE 2331/5331

Page 21: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Exercise

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

CSE 2331/5331

Page 22: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Balancing Property of RB-tree

Lemma [BN-Bound]: Let r be the root of tree T. Then 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 𝑟𝑟 ≥ 2𝑏𝑏𝑏 𝑟𝑟 − 1 .

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 2𝑏𝑏𝑏 𝑥𝑥 − 1

Hence 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 𝑟𝑟 ≥ 2𝑏𝑏𝑏 𝑟𝑟 − 1

CSE 2331/5331

Page 24: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Balancing Property of RB-tree

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

2 log2(𝑛𝑛 + 1).

Proof: Let r be the root of this red-black tree

Since no red node has a red child, 𝑏𝑏ℎ 𝑟𝑟 ≥ 𝑏2

By Lemma [BN-Bound], 𝑛𝑛 ≥ 2𝑏𝑏𝑏 𝑟𝑟 − 1 ≥ 2𝑏/2 − 1 Thus, ℎ ≤ 2 log2 𝑛𝑛 + 1

CSE 2331/5331

Page 25: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implication of the Theorem

CSE 2331/5331

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

has Θ(lg𝑛𝑛) height, and All operations thus all take 𝑂𝑂(lg𝑛𝑛) time.

How to maintain RB-tree underOperations?

Page 26: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

CSE 2331/5331

Set Operations

Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Only need to considerInsert / delete

Page 27: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Insertion Example

CSE 2331/5331

Insert 24?Insert 2?Insert 36?

Page 28: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Red-Black Tree Insert

CSE 2331/5331

Page 29: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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

CSE 2331/5331

Page 30: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example: Case 3

CSE 2331/5331

Insert 12?

Page 31: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implementation of Fixup Case 3

CSE 2331/5331

Page 32: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Remarks

Running time of RBInsertFixupC Θ 1

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Insert Fixup: Case 2

CSE 2331/5331

Page 34: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example: Case 2

CSE 2331/5331

Insert 14?

Page 35: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implementation of Case 2

CSE 2331/5331

Running timeΘ 1

Page 36: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Sibling

CSE 2331/5331

Takes Θ 1 time

Page 38: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Implementation of Case 1

CSE 2331/5331

When does this procedure terminate?

Running time:Θ ℎ = Θ(lg𝑛𝑛)

Page 39: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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:

Θ(lg𝑛𝑛)

Total # rotations: At most 2

CSE 2331/5331

Page 40: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example

CSE 2331/5331

Insert 21?

Page 41: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Augmenting Data Structure

CSE 2331/5331

Page 42: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

CSE 2331/5331

Balanced Binary Search Tree

Maximum Extract-Max Insert Increase-key

Search Delete Successor Predecessor

Also supportSelect

operation ?

Page 43: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: 𝑥𝑥. 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 = 𝑥𝑥. 𝑒𝑒𝑒𝑒𝑙𝑙𝑒𝑒. 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 + 𝑥𝑥. 𝑟𝑟𝑠𝑠𝑟𝑟ℎ𝑒𝑒. 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 + 1

CSE 2331/5331

Page 45: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

Page 46: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

How to Setup Size Information?

procedure AugmentSize( 𝑒𝑒𝑟𝑟𝑒𝑒𝑒𝑒𝑛𝑛𝑡𝑡𝑡𝑡𝑒𝑒 𝑥𝑥 )If (𝑥𝑥 ≠ 𝑁𝑁𝑁𝑁𝑁𝑁 ) then 𝑁𝑁𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 = AugmentSize( 𝑥𝑥. 𝑒𝑒𝑒𝑒𝑙𝑙𝑒𝑒 );𝑅𝑅𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 = AugmentSize( 𝑥𝑥. 𝑟𝑟𝑠𝑠𝑟𝑟ℎ𝑒𝑒);𝑥𝑥. 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 = 𝑁𝑁𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 + 𝑅𝑅𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 + 1; Return( 𝑥𝑥. 𝑒𝑒𝑠𝑠𝑠𝑠𝑒𝑒 );

endReturn (0);

CSE 2331/5331

Postorder traversalof the tree !

Page 47: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Correctness? Running time?

O(h)

CSE 2331/5331

Page 49: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

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

Page 51: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Correctness ? Time complexity?

O(h)

CSE 2331/5331

Page 52: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

Example

CSE 2331/5331

M9

C5

A1

F3

D1

H1

P3

T2

Q1

Insert(J) ?

Page 54: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

During the downward search, increase the sizeattribute 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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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: PowerPoint Presentation - Topology Based Methods in Shape ...web.cse.ohio-state.edu/~wang.1016/courses/2331/cse2331-lec7.pdf · Compared to an ordinary binary tree, a red-black binary

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