balanced search trees problem: efficiency of bst is related to tree’s height. search, insert and...

Post on 04-Jan-2016

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Balanced Search Trees

• Problem: Efficiency of BST is related to tree’s height. search, insert and remove follow a path from

root to desired location range in value for ceil(log (N+1)) to N

• What affects height of BST? sensitive to order of data in which you insert or

delete items.

• Solution: Maintain balance by using variation of BST.

Splay Trees• Type of balanced binary search trees.• Search, insert, delete, and split have amortized

complexity O(log n) & actual complexity O(n).• Actual and amortized complexity of join is O(1).• Priority queue and double-ended priority queue

versions outperform heaps over a sequence of operations.

• Two varieties. Bottom up. Top down.

Top-Down Splay Trees• On the way down the tree, split the tree into

the binary search trees S (small elements) and B (big elements). Similar to split operation in an unbalanced binary

search tree. However, a rotation is done whenever an LL or RR

move is made. Move down 2 levels at a time, except (possibly) in the

end when a one level move is made.

• When the splay node is reached, S, B, and the subtree rooted at the splay node are combined into a single binary search tree.

Split A Binary Search TreeB

e

d

b

A

a B

c

C

D

f

m

g

E

S

Split A Binary Search Tree

A

a

B

d

b

B

c

C

D

f

m

g

E

e

S

Split A Binary Search Tree

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Two-Level Moves

e

d

b

A

a B

c

C

D

f

m

g

E

• Let m be the splay node.• RL move from A to C.

• RR move from C to E.

• L move from E to m.

RL MoveB

e

d

b

A

a B

c

C

D

f

m

g

E

S

RL Move

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

RR Move

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

RR Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

L Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

L Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Wrap Up

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Wrap Up

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Wrap Up

b

A

a

B

dc

C

D

f

m

g

E

e

YouTube lecture

• CS 61B Lecture 34: Splay Trees

Java Applets

• http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/SplayTree-Example.html

• http://techunix.technion.ac.il/~itai/

top related