Professional Documents
Culture Documents
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
Splay Trees - 2
BST: Insert
Insert(78)
44
17
88
32
28
65
54
29
97
82
76
80
78
Jim Anderson
Splay Trees - 3
BST: Delete
44
Delete(32)
17
88
32
65
54
29
97
82
76
80
78
Jim Anderson
Splay Trees - 4
BST: Delete
44
Delete(32)
17
88
28
65
29
54
97
82
76
80
78
Jim Anderson
Splay Trees - 5
BST: Delete
44
Delete(65)
17
88
32
28
65
54
29
97
82
76
80
78
Jim Anderson
Splay Trees - 6
BST: Delete
44
Delete(65)
17
88
32
28
76
54
97
82
80
29
78
Jim Anderson
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
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
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
Splay Trees - 10
Zig
x has no grandparent (so, y is the root)
y
x
20
w
T1
30
20
10
30
T2
T3
T1
T4
T2
T3
T4
Splay Trees - 11
Complete Example
44
50
Splay(78)
17
88
32
65
97
zig-zag
28
54
29
82
76
x
Jim Anderson
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
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
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
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
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
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
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
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
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).
Jim Anderson
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.
Splay Trees - 22
sk
items
Jim Anderson
Multipop(S, k)
sk
items
Multipop(S, k)
0 items
Comp 750, Fall 2009
Splay Trees - 23
Splay Trees - 24
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
All O(1).
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.
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
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).
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.
Jim Anderson
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
Splay Trees - 30
Case 1: zig-zig
z
10
20
T1
y
30
20
z 10
T4
T1
T4
T3
T2
T3
30
T2
Splay Trees - 31
Case 2: zig-zag
z 10
y
x
T1
x 20
30
20
T2
T4
T3
z 10
T1
y 30
T2
T3
T4
Splay Trees - 32
Case 3: zig
y 10
20
x 20
T1
30
T2
T3
T4
y 10
T1
w 30
T2
T3
T4
Jim Anderson
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
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
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.
Splay Trees - 36
Another Look at
= the total change in r(T). r(T) r(v) lg(n(v))
vT
vT
lg
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
Splay Trees - 37
Jim Anderson
Splay Trees - 38
Jim Anderson
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
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
78
Splay Trees - 41
Insert
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.
Subtree at vi
doesnt include
vi+1 and its
other child.
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
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
Splay Trees - 44