Top Banner
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007
36

CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

Dec 19, 2015

Download

Documents

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: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

CSE 326: Data StructuresSplay Trees

Ben Lerner

Summer 2007

Page 2: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

2

Administrivia

• Midterm July 16 during lecture– Topics posted, Review next week

• Project 2c posted early next week

Page 3: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

3

Self adjustment for better living

• Ordinary binary search trees have no balance conditions– what you get from insertion order is it

• Balanced trees like AVL trees enforce a balance condition when nodes change– tree is always balanced after an insert or delete

• Self-adjusting trees get reorganized over time as nodes are accessed

Page 4: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

4

Splay Trees

• Blind adjusting version of AVL trees– Why worry about balances? Just rotate

anyway!

• Amortized time per operations is O(log n)• Worst case time per operation is O(n)

– But guaranteed to happen rarely

Insert/Find always rotate node to the root!

Page 5: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

5

Amortized ComplexityIf a sequence of M operations takes O(M f(n)) time,we say the amortized runtime is O(f(n)).

Amortized complexity is worst-case guarantee over sequences of operations.

• Worst case time per operation can still be large, say O(n)

• Worst case time for any sequence of M operations is O(M f(n))

Average time per operation for any sequence is O(f(n))

Page 6: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

6

Amortized Complexity• Is amortized guarantee any weaker than

worstcase?

• Is amortized guarantee any stronger than averagecase?

• Is average case guarantee good enough in practice?

• Is amortized guarantee good enough in practice?

Page 7: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

7

The Splay Tree Idea

17

10

92

5

If you’re forced to make a really deep access:

Since you’re down there anyway,fix up a lot of deep nodes!

3

Page 8: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

8

1. Find or insert a node k2. Splay k to the root using:

zig-zag, zig-zig, or plain old zig rotation

Why could this be good??

1. Helps the new root, ko Great if k is accessed again

2. And helps many others!o Great if many others on the path are accessed

Find/Insert in Splay Trees

Page 9: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

9

Splaying node k to the root:Need to be careful!

One option (that we won’t use) is to repeatedly use AVL single rotation until k becomes the root: (see Section 4.5.1 for details)

s

Ak

B C

r

D

q

E

p

F

r

D

q

E

p

F

C

s

A B

k

Page 10: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

10

Splaying node k to the root:Need to be careful!

What’s bad about this process?

s

Ak

B C

r

D

q

E

p

F

r

D

q

E

p

F

C

s

A B

k

Page 11: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

11

• Let X be a non-root node with 2 ancestors.• P is its parent node.• G is its grandparent node.

P

G

X

G

P

X

G

P

X

G

P

X

Splay Tree Terminology

Page 12: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

12

Zig-Zig and Zig-Zag

4

G 5

1 P Zig-zag

G

P 5

X 2

Zig-zig

X

Parent and grandparentin same direction.

Parent and grandparentin different directions.

Page 13: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

13

Zig-Zag operation

• “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed)

(RotateFromRight) (RotateFromLeft)

ZigZagFromLeft

Page 14: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

14

Splay: Zig-Zag*

g

Xp

Y

k

Z

W

*Just like an…

k

Y

g

W

p

ZX

Which nodes improve depth?

Page 15: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

15

Zig-Zig operation

• “Zig-Zig” consists of two single rotations of the same direction (R is the node that was accessed)

(RotateFromLeft) (RotateFromLeft)

ZigZigFromLeft

Page 16: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

16

Splay: Zig-Zig*

k

Z

Y

p

X

g

W

g

W

X

p

Y

k

Z

*Is this just two AVL single rotations in a row?

Why does this help?

Page 17: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

17

Special Case for Root: Zigp

X

k

Y

Z

root k

Z

p

Y

X

root

Relative depth of p, Y, Z? Relative depth of everyone else?

Why not drop zig-zig and just zig all the way?

Page 18: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

18

Splaying Example: Find(6)

2

1

3

4

5

6

Find(6)

2

1

3

6

5

4

?

Think of as if created by inserting 6,5,4,3,2,1 – each took constant time – a LOT of savings so far to amortize those bad accesses over

Page 19: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

19

Still Splaying 6

2

1

3

6

5

4

1

6

3

2 5

4

?

Page 20: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

20

Finally…

1

6

3

2 5

4

6

1

3

2 5

4

?

Page 21: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

21

Another Splay: Find(4)

Find(4)

6

1

3

2 5

4

6

1

4

3 5

2

?

Page 22: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

22

Example Splayed Out

6

1

4

3 5

2

61

4

3 5

2

?

Page 23: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

23

Practice Finding

• Find 2…

• Then how long would it take to find 6? 4?

• Will the tree ever look like what we started with again?

61

4

3 5

2

Page 24: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

24

But Wait…

What happened here?

Didn’t two find operations take linear timeinstead of logarithmic?

What about the amortized O(log n) guarantee?

Page 25: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

25

Why Splaying Helps

• If a node n on the access path is at depth d before the splay, it’s at about depth d/2 after the splay

• Overall, nodes which are low on the access path tend to move closer to the root

• Splaying gets amortized O(log n) performance. (Maybe not now, but soon, and for the rest of the operations.)

Page 26: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

26

Practical Benefit of Splaying• No heights to maintain, no imbalance to

check for– Less storage per node, easier to code

• Often data that is accessed once, is soon accessed again!– Splaying does implicit caching by bringing it to

the root

Page 27: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

27

Splay Operations: Find

• Find the node in normal BST manner

• Splay the node to the root– if node not found, splay what would have

been its parent

What if we didn’t splay?

Page 28: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

28

Splay Operations: Insert

• Insert the node in normal BST manner

• Splay the node to the root

What if we didn’t splay?

Page 29: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

29

Example Insert

• Inserting in order 1,2,3,…,8

• Without self-adjustment

1

2

3

4

5

6

7

8

O(n2) time

Page 30: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

30

With Self-Adjustment

1

2

1 2

1

ZigFromRight

2

1 3

ZigFromRight2

1

3

1

2

3

Page 31: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

31

With Self-Adjustment

ZigFromRight2

1

34

4

2

1

3

4

O(n) time!!

Page 32: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

32

Splay Operations: Remove

find(k)

L R

k

L R

> k< k

delete k

Now what?

Page 33: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

33

Example Deletion

10

155

201382

96

10

15

5

2013

8

2 96

splay

10

15

5

2013

2 96

remove

10

15

5

2013

2 9

6splay

attach

Page 34: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

34

Practice Delete

10

155

201382

96

Page 35: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

35

JoinJoin(L, R): given two trees such that (stuff in L) < (stuff in R), merge them:

Splay on the maximum element in L, then attach R

L R R

L

Does this work to join any two trees?

splay

max

Page 36: CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

36

Splay Tree Summary• All operations are in amortized O(log n) time

• Splaying can be done top-down; this may be better because:– only one pass– no recursion or parent pointers necessary– we didn’t cover top-down in class

• Splay trees are very effective search trees– Relatively simple– No extra fields required– Excellent locality properties: frequently accessed keys are

cheap to find