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

22
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.

Upload: lindsay-murphy

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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.

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

Split A Binary Search TreeB

e

d

b

A

a B

c

C

D

f

m

g

E

S

Page 5: 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

Split A Binary Search Tree

A

a

B

d

b

B

c

C

D

f

m

g

E

e

S

Page 6: 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

Split A Binary Search Tree

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

Page 7: 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

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Page 8: 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

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Page 9: 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

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Page 10: 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

Split A Binary Search Tree

b

A

a

B

c

C

B

d

D

f

m

g

E

e

S

Page 11: 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

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.

Page 12: 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

RL MoveB

e

d

b

A

a B

c

C

D

f

m

g

E

S

Page 13: 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

RL Move

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

Page 14: 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

RR Move

b

A

a

B

B

d

c

C

D

f

m

g

E

e

S

Page 15: 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

RR Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Page 16: 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

L Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Page 17: 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

L Move

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Page 18: 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

Wrap Up

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Page 19: 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

Wrap Up

b

A

a

B

B

dc

C

D

f

m

g

E

e

S

Page 20: 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

Wrap Up

b

A

a

B

dc

C

D

f

m

g

E

e

Page 21: 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

YouTube lecture

• CS 61B Lecture 34: Splay Trees

Page 22: 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

Java Applets

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

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