You are on page 1of 44

Splay Trees

In balanced tree schemes, explicit rules are


followed to ensure balance.
In splay trees, there are no such rules.
Search, insert, and delete operations are like in
binary search trees, except at the end of each
operation a special step called splaying is done.
Splaying ensures that all operations take O(lg n)
amortized time.
First, a quick review of BST operations
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 1

BST: Search
Search(25)
Note: In the handout,
sentinel leaf nodes are
assumed.
tree with n keys
has 2n+1 nodes.

Search(76)
44

17

88
32

28

65
54

29

97
82

76
80

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 2

BST: Insert
Insert(78)
44

17

88
32

28

65
54

29

97
82

76
80
78

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 3

BST: Delete
44
Delete(32)
17

Has only one


child: just splice
out 32.
28

88
32

65
54

29

97
82

76
80
78

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 4

BST: Delete
44
Delete(32)
17

88
28

65
29

54

97
82

76
80
78
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 5

BST: Delete
44
Delete(65)
17

88
32

Has two children:


Replace 65 by successor,
76, and splice out
successor.

28

65
54

29

97
82

76

Note: Successor can have


at most one child. (Why?)

80
78

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 6

BST: Delete
44
Delete(65)
17

88
32

28

76
54

97
82

80

29
78

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 7

Splaying
In splay trees, after performing an ordinary BST
Search, Insert, or Delete, a splay operation is
performed on some node x (as described later).
The splay operation moves x to the root of the
tree.
The splay operation consists of sub-operations
called zig-zig, zig-zag, and zig.

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 8

Zig-Zig
z

10

x has a grandparent
y
20

y
x

T1

30

30

20
T4

10
T3

T2
T3

T1

T4

T2

(Symmetric case too)


Note: xs depth decreases by two.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 9

Zig-Zag
z

10

y
30
x
20

T1

x has a grandparent

20

10

30

T4
T2

T1

T3

T2

T3

T4

(Symmetric case too)


Note: xs depth decreases by two.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 10

Zig
x has no grandparent (so, y is the root)
y

Note: w could be NIL


10

x
20
w

T1

30

20

10

30

T2
T3

T1

T4

T2

T3

T4

(Symmetric case too)


Note: xs depth decreases by one.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 11

Complete Example
44
50

Splay(78)
17

88
32

65

97

zig-zag
28

54
29

82
76

x
Jim Anderson

Comp 750, Fall 2009

y
80
78
Splay Trees - 12

Complete Example
44
50

Splay(78)
17

88
32

65

97

zig-zag
28

54
29

78
z 76

Jim Anderson

82

Comp 750, Fall 2009

x
80

Splay Trees - 13

Complete Example
44
50

Splay(78)
17

88
z 65

32

97

zig-zag
28

54

82
78 x

29
76

Jim Anderson

Comp 750, Fall 2009

80

Splay Trees - 14

Complete Example
44
50

Splay(78)
17

88
x 78

32

97

zig-zag
z 65

28
29

Jim Anderson

54

Comp 750, Fall 2009

y 82
76

80

Splay Trees - 15

Complete Example
44
50

Splay(78)

z
88 y

17
x 78

32

97

zig-zag
28
29

Jim Anderson

82

65
54

Comp 750, Fall 2009

76

80

Splay Trees - 16

Complete Example
44
78

Splay(78)
17

x
88 y

50 z
82

32

65

zig-zag

97

80

28

54

76

29

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 17

Complete Example
y

44
78

Splay(78)
17

x
88 w

50
82

32

65

zig

97

80

28

54

76

29

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 18

Complete Example
78 x

y 44
Splay(78)
17

88 w

50
82

32

65

zig

97

80

28

54

76

29

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 19

Result of splaying
The result is a binary tree, with the left subtree
having all keys less than the root, and the right
subtree having keys greater than the root.
Also, the final tree is more balanced than the
original.
However, if an operation near the root is done,
the tree can become less balanced.

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 20

When to Splay
Search:
Successful: Splay node where key was found.
Unsuccessful: Splay last-visited internal node (i.e., last node
with a key).

Insert:
Splay newly added node.

Delete:
Splay parent of removed node (which is either the node with
the deleted key or its successor).

Note: All operations run in O(h) time, for a tree of height


h.

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 21

Amortized Analysis
Based on the Accounting Method (see Sec.
17.2 of CLRS).
Idea: When an operations amortized cost exceeds it
actual cost, the difference is assigned to certain tree
nodes as credit.
Credit is used to pay for subsequent operations
whose amortized cost is less than their actual cost.

Most of our analysis will focus on splaying.


The BST operations will be easily dealt with at the
end.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 22

Review: Accounting Method


Stack Example:
Operations:
Push(S, x).
Can implement in O(1) time.
Pop(S).
Multipop(S, k): if stack has s items, pop off min(s, k)
items.
sk
items

sk
items
Jim Anderson

Multipop(S, k)
sk
items
Multipop(S, k)
0 items
Comp 750, Fall 2009

Splay Trees - 23

Accounting Method (Continued)


We charge each operation an amortized cost.
Charge may be more or less than actual cost.
If more, then we have credit.
This credit can be used to pay for future
operations whose amortized cost is less than
their actual cost.
Require: For any sequence of operations,
amortized cost upper bounds worst-case cost.
That is, we always have nonnegative credit.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 24

Accounting Method (Continued)


Stack Example:
Actual Costs:
Push:
Pop:
Multipop:

1
1
min(s, k)

Amortized Costs:
Push:
Pop:
Multipop:

For a sequence of n
operations, does total
amortized cost upper
bound total worst-case
cost, as required?

2
0
0

What is the total worstcase cost of the sequence?

All O(1).

Pays for the push and a future pop.


Jim Anderson

Comp 750, Fall 2009

Splay Trees - 25

Ranks
T is a splay tree with n keys.
Definition: The size of node v in T, denoted n(v),
is the number of nodes in the subtree rooted at v.
Note: The root is of size 2n+1.

Definition: The rank of v, denoted r(v), is


lg(n(v)).
Note: The root has rank lg(2n+1).

Definition: r(T) = vT r(v).


Jim Anderson

Comp 750, Fall 2009

Splay Trees - 26

Meaning of Ranks
The rank of a tree is a measure of how well balanced it
is.
A well balanced tree has a low rank.
A badly balanced tree has a high rank.
The splaying operations tend to make the rank smaller,
which balances the tree and makes other operations
faster.
Some operations near the root may make the rank larger
and slightly unbalance the tree.
Amortized analysis is used on splay trees, with the rank
of the tree being the potential
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 27

Credit Invariant
We will define amortized costs so that the following
invariant is maintained.
Each
Each node
node vv of
ofTThas
has r(v)
r(v) credits
credits in
in its
its account.
account.
So, each operations amortized cost = its real cost + the total
change in r(T) it causes (positive or negative).

Let Ri = op. is real cost and i = change in r(T) it causes.


Total am. cost = i=1,,n (Ri + i). Initial tree has rank 0 &
final tree has non-neg. rank. So, i=1,n i 0, which
implies total am. cost total real cost.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 28

Whats Left?
We want to show that the per-operation amortized cost is
logarithmic.
To do this, we need to look at how BST operations and
splay operations affect r(T).
We spend most of our time on splaying, and consider the
specific BST operations later.

To analyze splaying, we first look at how r(T) changes as


a result of a single substep, i.e., zig, zig-zig, or zig-zag.
Notation: Ranks before and after a substep are denoted r(v) and
r(v), respectively.

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 29

Proposition 13.6
Proposition
Proposition13.6:
13.6:Let
Letbe
bethe
thechange
changein
inr(T)
r(T)caused
causedby
byaasingle
single
substep.
substep. Let
Letxxbe
bethe
thex
xin
inour
ourdescriptions
descriptionsof
ofthese
thesesubsteps.
substeps. Then,
Then,
3(r(x)
3(r(x)r(x))
r(x))22ififthe
thesubstep
substepisisaazig-zig
zig-zigor
oraazig-zag;
zig-zag;
3(r(x)
3(r(x)r(x))
r(x))ififthe
thesubstep
substepisisaazig.
zig.
Proof:
Three cases, one for each kind of substep

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 30

Case 1: zig-zig
z

Only the ranks of x, y, and


z change. Also, r(x) = r(z),
r(y) r(x), and r(y) r(x).
Thus,

10
20

T1

y
30

20

z 10

= r(x) + r(y) + r(z) r(x) r(y) r(z)


= r(y) + r(z) r(x) r(y)
r(x) + r(z) 2r(x). (*)

T4

T1

T4
T3

T2
T3

30

T2

Also, n(x) + n(z) n(x), which (by property of lg), implies


r(x) + r(z) 2r(x) 2, i.e.,

If a > 0, b > 0, and c a + b,


then lg a + lg b 2 lg c 2.

r(z) 2r(x) r(x) 2. (**)

By (*) and (**), r(x) + (2r(x) r(x) 2) 2r(x)


= 3(r(x) r(x)) 2.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 31

Case 2: zig-zag
z 10

Only the ranks of x, y, and


z change. Also, r(x) = r(z)
and r(x) r(y). Thus,

y
x

T1

x 20

30

20

T2

T4
T3

z 10
T1

y 30
T2

T3

T4

= r(x) + r(y) + r(z) r(x) r(y) r(z)


= r(y) + r(z) r(x) r(y)
r(y) + r(z) 2r(x). (*)
Also, n(y) + n(z) n(x), which (by property of lg), implies
r(y) + r(z) 2r(x) 2. (**)
By (*) and (**), 2r(x) 2 2r(x)
3(r(x) r(x)) 2.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 32

Case 3: zig
y 10
20

x 20

T1

30

T2
T3

T4

y 10
T1

w 30
T2

T3

T4

Only the ranks of x and y change.


Also, r(y) r(y) and r(x) r(x). Thus,
= r(x) + r(y) r(x) r(y)
r(x) r(x)
3(r(x) r(x)).

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 33

Proposition 13.7
Proposition
Proposition13.7:
13.7:Let
LetTTbe
beaasplay
splaytree
treewith
withroot
roott,t,and
andlet
letbe
bethe
the
total
totalvariation
variationof
ofr(T)
r(T)caused
causedby
bysplaying
splayingaanode
nodexxatatdepth
depthd.
d. Then,
Then,
3(r(t)
3(r(t)r(x))
r(x))dd++2.2.
Proof:
Splay(x) consists of p = d/2 substeps, each of which is a zig-zig or
zig-zag, except possibly the last one, which is a zig if d is odd.
Let r0(x) = xs initial rank, ri(x) = xs rank after the ith substep, and
i = the variation of r(T) caused by the ith substep, where 1 i p.
p

i 1

i 1

By Proposition 13.6, i 3(ri (x) ri 1 (x)) 2 2


3(rp (x) r0 (x)) 2p 2
3(r(t) r(x)) d 2
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 34

Meaning of Proposition
If d is small (less than 3(r(t) r(x)) + 2) then the
splay operation can increase r(t) and thus make
the tree less balanced.
If d is larger than this, then the splay operation
decreases r(t) and thus makes the tree better
balanced.
Note that r(t) 3lg(2n + 1)
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 35

Amortized Costs
As stated before, each operations amortized
cost = its real cost + the total change in r(T) it
causes, i.e., .
This ensures the Credit Invariant isnt violated.

Real cost is d, so amortized cost is d + .


The real cost of d even includes the cost of
binary tree operations such as searching.
Note: can be positive or negative (or zero).
If its positive, were overcharging.
If its negative, were undercharging.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 36

Another Look at
= the total change in r(T). r(T) r(v) lg(n(v))
vT

vT

lg

Consider this example:

n(v)

vT

splay(b)
n(a)

4
b

<0
3
c

splay(a)

2
d

1 c

2
d

>0
r(T) = lg(4 2 1 1)
= lg(8)

r(T) = lg(4 3 2 1)
= lg(24)
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 37

Unbalancing the Tree


In fact, a sequence of zig operations can result
in a completely unbalanced linear tree. Then a
search operation can take O(n) time, but this is
OK because at least n operations have been
performed up to this point.

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 38

A Bound on Amortized Cost


We have:
Amortized Cost of Splaying
=d+
d + (3(r(t) r(x)) d + 2)
{Prop. 13.7}
= 3(r(t) r(x)) + 2
< 3r(t) + 2
= 3lg(2n + 1) + 2
{Recall t is the root}
= O(lg n)

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 39

Finishing Up
Until now, weve just focused on splaying costs.
We also need to ensure that BST operations can be
charged in a way that maintains the Credit Invariant.
Three Cases:
Search: Not a problem doesnt change the tree.
Delete: Not a problem removing a node can only decrease
ranks, so existing credits are still fine.
Insert: As shown next, an Insert can cause r(T) to increase
by up to lg(2n+3) + lg 3. Thus, the Credit Invariant can be
maintained if Insert is assessed an O(lg n) charge.
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 40

Insert

k
Insert(78)
vd 44

44

88

88

65

65
Insert(k)

82

82
v2 76

76

80 v1

80
v = v0
Jim Anderson

Comp 750, Fall 2009

78
Splay Trees - 41

Insert

Lots of typos in book!

For i = 1, , d, let n(vi) and n'(vi) be sizes before and after insertion,
and
r(vi) and r'(vi) be ranks before and after insertion.
We have: n'(vi) = n(vi) + 2.

Leaf gets replaced by real


node and two leaves.

For i = 1, , d 1, n(vi) + 2 n(vi+1), and


r'(vi) = lg(n'(vi)) = lg(n(vi) + 2) lg(n(vi+1)) = r(vi+1).

Subtree at vi
doesnt include
vi+1 and its
other child.

Thus, i=1..d (r'(vi) r(vi)) r'(vd) r(vd) + i=1..d-1(r(vi+1) r(vi))


Note: v0 is excluded
= r'(vd) r(vd) + r(vd) r(v1)
here it doesnt have an
old rank! Its new rank is lg 3. lg(2n + 3).
Thus, the Credit Invariant can be maintained if Insert is assessed
a charge of at most lg(2n + Comp
3) +750,
lgFall
3. 2009
Jim Anderson
Splay Trees - 42

Proposition 13.8
Weve established the following:
Proposition
Proposition13.8:
13.8:Consider
Consideraasequence
sequenceof
ofm
moperations
operationson
onaasplay
splay
tree,
tree,each
eachaasearch,
search,insertion,
insertion,or
ordeletion,
deletion,starting
startingfrom
fromaasplay
splaytree
tree
with
withzero
zerokeys.
keys. Also,
Also,let
letnni ibe
bethe
thenumber
numberof
ofkeys
keysin
inthe
thetree
treeafter
after
operation
operationi,i,and
andnnbe
bethe
thetotal
totalnumber
numberof
ofinsertions.
insertions. The
Thetotal
total
running
runningtime
timefor
forperforming
performingthe
thesequence
sequenceof
ofoperations
operationsisis
O(m
lg n )
O(m++
i=1..m
i=1..m lg ni i)
which
whichisisO(m
O(mlg
lgn).
n).

Jim Anderson

Comp 750, Fall 2009

Splay Trees - 43

Proposition 13.9
The following can also be shown:
Proposition
Proposition13.9:
13.9:Consider
Consideraasequence
sequenceof
ofm
moperations
operationson
onaasplay
splay
tree,
tree,each
eachaasearch,
search,insertion,
insertion,or
ordeletion,
deletion,starting
startingfrom
fromaasplay
splaytree
tree
with
withzero
zerokeys.
keys. Also,
Also,let
letf(i)
f(i)denote
denotethe
thenumber
numberof
oftimes
timesitem
itemiiisis
accessed,
accessed,and
andlet
letnnbe
bethe
thetotal
totalnumber
numberof
ofinsertions.
insertions. Assuming
Assumingeach
each
item
itemisisaccessed
accessedatatleast
leastonce,
once,the
thetotal
totalrunning
runningtime
timefor
forperforming
performing
the
thesequence
sequenceof
ofoperations
operationsisis
O(m
f(i)lg(m/f(i))).
O(m++
i=1..n
i=1..n f(i)lg(m/f(i))).
In other words, splay trees adapt to the accesses being performed: the
more an item is accessed, the lower the associated amortized cost!!!
Jim Anderson

Comp 750, Fall 2009

Splay Trees - 44

You might also like