Splay Tree
Splay Tree
p x
x c a p
a b b c
Splay Trees
p d a p
x c b g
a b c d
Splay Trees
Splay(78) 50
17 88
32 65 97
zag-zig
28 54 82
z
29 76
y
80
x
78
Complete Example
44
Splay(78) 50
17 88
32 65 97
28 54 82
x
29 78
z 76 y
80
Complete Example
44
Splay(78) 50
17 88
32 z 65 97
zag-zig
y
28 54 82
29 78 x
76 80
Complete Example
44
Splay(78) 50
17 88
32 x 78 97
28 z 65 y 82
29 54 76 80
Complete Example
44
z
Splay(78) 50
17 88 y
32 x 78 97
zag-zig
28 65 82
29 54 76 80
Complete Example
44
x
Splay(78) 78
17 50 z 88 y
82
32 97
65
80
28
54 76
29
Complete Example
y 44
x
Splay(78) 78
17 50 88 w
82
32 97
zag 65
80
28
54 76
29
Complete Example
78 x
y 44
Splay(78)
17 50 88 w
82
32 97
65
80
28
54 76
29
Result of splaying
Accounting method:
Assign each type of operation an (different) amortized cost
overcharge some operations, store the overcharge as credit on
specific objects, then use the credit for compensation for some
later operations.
Potential method:
Same as accounting method. But store the credit as “potential
energy” and as a whole.
Aggregate method
Stack Example.
Consider an algorithm doing stack operations push,
pop and multipop.
Alg Stackoperations(Stack S)
{
OP1 : Push(S,a)
OP2 : Pop(s)
OP3 : multipop(min(S,k))
}
Aggregate method
Push and Pop takes O(1). Multipop takes O(n)
in worst case.
The worst case time complexity of the above
algorithm is O(n).
If we run the algorithm for sequence of n
operations, then the worst case time complexity
is O(n*n). But, this is over estimate.
For example, consider the following sequence
of operations (n=5).
Aggregate method
Push(10), Push(20), Push(30), Push(40), Multipop(4).
For each push operation it takes O(1) and for
Multipop it takes O(n), where n=4.
Multipop calls Pop operation 4 times
Total cost T(n) = 4+4=8 and total number of
operations, n =8.
Cost of each operation =T(n)/n = 8/8 = O(1).
Hence amortized cost is O(1) for each operation and
for sequence of n operations O(n) in worst case.
Example Hash table insertion
Let us consider an example of a simple hash
table insertions. How do we decide table size?
There is a trade-off between space and time, if
we make hash-table size big, search time
becomes fast, but space required becomes high.
s≥k Multipop(S, k)
items s–k
items
Multipop(S, k)
sk
items 0 items
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 non negative credit.
Accounting Method (Continued)
Stack Example:
Actual Costs:
Push: 1
Pop: 1
Multipop: min(s, k)
Amortized Costs:
Push: 2
Pop: 0 All O(1).
Multipop: 0
– PUSH:
• Potential change: (Di)- (Di-1) =(s+1)-s =1.
• Amortized cost: ci' = ci + (Di) - (Di-1)=1+1=2.
– POP:
• Potential change: (Di)- (Di-1) =(s-1) –s= -1.
• Amortized cost: ci' = ci + (Di) - (Di-1)=1+(-1)=0.
Example Stack Operation
MULTIPOP(S,k): k'=min(s,k)
Each
Each node
node vv of
ofTThas
has r(v)
r(v) credits
credits in
in its
its account.
account.
So, each operation’s amortized cost = its real cost + the total
change in r(T) it causes (positive or negative).
Let Ri = op. i’s 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.
Splay Tree Amortized Analysis
Proof:
We have: