+ All Categories
Home > Documents > 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

Date post: 04-Jan-2016
Category:
Upload: letitia-ellis
View: 219 times
Download: 1 times
Share this document with a friend
38
1 Splay trees (Sleator, Tarjan 1983)
Transcript
Page 1: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

1

Splay trees (Sleator, Tarjan 1983)

Page 2: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

2

Goal

Support the same operations as previous search trees.

Page 3: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

3

Highlights

• binary

• simple

• good amortized property

• very elegant

• interesting open conjectures -- further and deeper understanding of this data structure is still due

Page 4: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

4

Main idea

• Try to arrange so frequently used items are near the root

• We shall assume that there is an item in every node including internal nodes. We can change this assumption so that items are at the leaves.

Page 5: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

5

First attempt

Move the accessed item to the root by doing rotations

y

x

B

C

x

Ay

B C

<===>

A

Page 6: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

6

Move to root (example)

e

d

b

a

c

A

D

E

F

CB

e

d

a

b

c

A

B

E

F

DC

e

d

b

a E

F

DC

c

BA

Page 7: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

7

Move to root (analysis)

There are arbitrary long access sequences such that the time per access is O(n) !

Page 8: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

8

Splaying

Does rotations bottom up on the access path, but rotations are done in pairs in a way that depends on the structure of the path.

A splay step:

z

y

x

A B

C

D

x

y

z

DC

B

A

==>(1) zig - zig

Page 9: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

9

Splaying (cont)

z

y

x

B C

A

D

x

z

DC

==>(2) zig - zag

y

BA

y

x

BA

C

x

y

CB

==>(3) zig

A

Page 10: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

10

Splaying (example)

i

h

H

g

f

e

d

c

b

a

I

J

G

A

B

C

D

E F

i

h

H

g

f

e

d

a

b

c

I

J

G

A

B

F

E

DC

==>

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

==>

Page 11: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

11

Splaying (example cont)

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

==>i

h

H

a

f g

d e

b

c

I

J

G

A

BF

E

DC

==>

f

d

b

c

A

B

E

DC

a

h

i

I JH

g

e

GF

Page 12: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

12

Splaying (analysis)

Assume each item i has a positive weight w(i) which is arbitrary but fixed.

Define the size s(x) of a node x in the tree as the sum of the weights of the items in its subtree.

The rank of x: r(x) = log2(s(x))

Measure the splay time by the number of rotations

Page 13: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

13

Access lemma

The amortized time to splay a node x in a tree with root t is at most 3(r(t) - r(x)) + 1 = O(log(s(t)/s(x)))

This has many consequences:

Potential used: The sum of the ranks of the nodes.

Page 14: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

14

Balance theorem

Balance Theorem: Accessing m items in an n node splay tree takes O((m+n) log n)

More consequences after the proof.

Proof.

Assign weight of 1/n to each item.

The total weight is then W=1.

To splay at any item takes 3log(n) +1 amortized time

the total potential drop is at most n log(n)

Page 15: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

15

Proof of the access lemma

proof. Consider a splay step.

Let s and s’, r and r’ denote the size and the rank function just before and just after the step, respectively. We show that the amortized time of a zig step is at most 3(r’(x) - r(x)) + 1, and that the amortized time of a zig-zig or a zig-zag step is at most 3(r’(x)-r(x))

The lemma then follows by summing up the cost of all splay steps

The amortized time to splay a node x in a tree with root t is at most 3(r(t) - r(x)) + 1 = O(log(s(t)/s(x)))

Page 16: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

16

Proof of the access lemma (cont)

y

x

BA

C

x

y

CB

==>(3) zig

A

amortized time(zig) = 1 + =

1 + r’(x) + r’(y) - r(x) - r(y)

1 + r’(x) - r(x)

1 + 3(r’(x) - r(x))

Page 17: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

17

Proof of the access lemma (cont)

amortized time(zig) = 1 + =

2 + r’(x) + r’(y) + r’(z) - r(x) - r(y) - r(z) =

2 + r’(y) + r’(z) - r(x) - r(y)

2 + r’(x) + r’(z) - 2r(x)

2r’(x) - r(x) - r’(z) + r’(x) + r’(z) - 2r(x) = 3(r’(x) - r(x))

z

y

x

A B

C

D

x

y

z

DC

B

A

==>(1) zig - zig

Page 18: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

18

Proof of the access lemma (cont)

z

y

x

B C

A

D

x

z

DC

==>(2) zig - zag

y

BA

Similar. (do at home)

Page 19: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

19

More consequences

Static finger theorem: Let f be an arbitrary fixed item, the total

access time is O(nlog(n) + m + log(|ij-f| + 1))j=1

m

Splay trees support access within the vicinity of any fixed finger as good as finger search trees.

Suppose all items are numbered from 1 to n in symmetric order. Let the sequence of accessed items be i1,i2,....,im

Page 20: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

21

Static optimality theorem

Static optimality theorem: If every item is accessed at least once

then the total access time is O(m + q(i) log (m/q(i)) ) i=1

n

For any item i let q(i) be the total number of time i is accessed

Optimal average access time up to a constant factor.

Page 21: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

22

Static optimality theorem (proof)

Static optimality theorem: If every item is accessed at least once

then the total access time is O(m + q(i) log (m/q(i)) ) i=1

n

Proof. Assign weight of q(i)/m to item i.

Then W=1.

Amortized time to splay at i is 3log(m/q(i)) + 1

Maximum potential drop over the sequence is

log(W)- log (q(i)/m) i=1

n

Page 22: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

23

Application: Data Compression via Splay Trees

Suppose we want to compress text over some alphabet

Prepare a binary tree containing the items of at its leaves.

To encode a symbol x:

•Traverse the path from the root to x spitting 0 when you go left and 1 when you go right.

•Splay at the parent of x and use the new tree to encode the next symbol

Page 23: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

24

Compression via splay trees (example)

e f g ha b c d

aabg...

000

e f g h

a

b

c d

Page 24: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

25

Compression via splay trees (example)

e f g ha b c d

aabg...

000

e f g h

a

b

c d

0

Page 25: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

26

Compression via splay trees (example)

aabg...

0000

e f g h

a

b

c de f g h

c d

a b

10

Page 26: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

27

Compression via splay trees (example)

aabg...

0000

e f g h

a

b

c de f g h

c d

a b

101110

Page 27: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

28

Decoding

Symmetric.

The decoder and the encoder must agree on the initial tree.

Page 28: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

29

Compression via splay trees (analysis)

How compact is this compression ?

Suppose m is the # of characters in the original string

The length of the string we produce is m + (cost of splays)

by the static optimality theorem

m + O(m + q(i) log (m/q(i)) ) =O(m + q(i) log (m/q(i)) )

Recall that the entropy of the sequence q(i) log (m/q(i)) is a lower bound.

Page 29: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

30

Compression via splay trees (analysis)

In particular the Huffman code of the sequence is at least

q(i) log (m/q(i))

But to construct it you need to know the frequencies in advance

Page 30: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

31

Compression via splay trees (variations)

D. Jones (88) showed that this technique could be competitive with dynamic Huffman coding (Vitter 87)

Used a variant of splaying called semi-splaying.

Page 31: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

32

Semi - splaying

z

y

x

A B

C

D

y

z

DC

==>Semi-splay zig - zig

x

A B

z

y

x

A B

C

D

x

y

z

DC

B

A==>

Regular zig - zig

*

*

*

*

Continue splay at y rather than at x.

Page 32: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

38

Update operations on splay trees

Catenate(T1,T2):

Splay T1 at its largest item, say i.

Attach T2 as the right child of the root.

T1 T2

i

T1T2

i

T1 T2

≤ 3log(W/w(i)) + O(1)

Amortize time: 3(log(s(T1)/s(i)) + 1 + s(T1)

s(T1) + s(T2)log( )

Page 33: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

39

Update operations on splay trees (cont)

split(i,T):

Assume i T

T

i

Amortized time = 3log(W/w(i)) + O(1)

Splay at i. Return the two trees formed by cutting off the right son of i

i

T1 T2

Page 34: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

40

Update operations on splay trees (cont)

split(i,T):

What if i T ?

T

i-

Amortized time = 3log(W/min{w(i-),w(i+)}) + O(1)

Splay at the successor or predecessor of i (i- or i+). Return the two trees formed by cutting off the right son of i or the left son of i

i-

T1 T2

Page 35: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

41

Update operations on splay trees (cont)

insert(i,T):

T1 T2

i

Perform split(i,T) ==> T1,T2

Return the tree

Amortize time:

min{w(i-),w(i+)}W-w(i)

3log( ) + log(W/w(i)) + O(1)

Page 36: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

42

Update operations on splay trees (cont)

delete(i,T):

T1 T2

i

Splay at i and then return the catenation of the left and right subtrees

Amortize time:

w(i-)W-w(i)

3log( ) + O(1)

T1 T2+

3log(W/w(i)) +

Page 37: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

43

Open problems

Self adjusting form of a,b tree ?

Page 38: 1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.

44

Open problems

Dynamic optimality conjecture:

Consider any sequence of successful accesses on an n-node search tree. Let A be any algorithm that carries out each access by traversing the path from the root to the node containing the accessed item, at the cost of one plus the depth of the node containing the item, and that between accesses perform rotations anywhere in the tree, at a cost of one per rotation. Then the total time to perform all these accesses by splaying is no more than O(n) plus a constant time the cost of algorithm A.


Recommended