0% found this document useful (0 votes)
58 views

Splay Tree

Splay trees are self-balancing binary search trees where each search, insert, or delete operation is followed by a splaying operation that moves the accessed node to the root, balancing the tree. The splaying operation performs rotations to rearrange nodes and maintain the binary search tree properties. This splaying after each operation ensures O(log n) amortized time for searches, inserts, and deletes across a sequence of operations on the tree.

Uploaded by

ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Splay Tree

Splay trees are self-balancing binary search trees where each search, insert, or delete operation is followed by a splaying operation that moves the accessed node to the root, balancing the tree. The splaying operation performs rotations to rearrange nodes and maintain the binary search tree properties. This splaying after each operation ensures O(log n) amortized time for searches, inserts, and deletes across a sequence of operations on the tree.

Uploaded by

ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 61

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. Sequence of m operations takes
O(m log n). In Binary Search Tree this takes O(m*n)
times.
Splaying

 In splay trees, after performing an ordinary BST


Search, Insert, or Delete, a splay operation is
performed on some node x.

 The Splay operation moves x to the root of the


tree.
Splay Trees

 There are six rotations possible in a splay tree:


1. Zig Rotation
2. Zag Rotation
3. Zig-Zig Rotation
4. Zag-Zag Rotation
5. Zig-Zag Rotation
6. Zag-Zig Rotation
Splay Trees

 Zig Rotation (Similar to LL, one level)

p x

x c a p

a b b c
Splay Trees

 Zig-Zig Rotation (Two level)


g x

p d a p

x c b g

a b c d
Splay Trees

 Zig-Zag Rotation (Similar to LR)


g
x
p d
p g
a x
a b c d
b c
Splay Tree
 Similarly the following rotations are defined
from right side.
 Zag (similar to RR)
 Zag-Zig (Similar to RL)
 Zag-Zag (Similar to Zig-Zig but from right side)
Complete Example
44

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

 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.
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.
Top-down Splaying
 Bottom-up splaying requires traversal from root to
the node that is to be splayed, and then rotating back
to the root – in other words, we make 2 tree
traversals. We would like to eliminate one of these
traversals.
 Rather than walking down the tree to first find the
value then splaying back up, we can splay on the
way down.
 We will be "pruning" the big tree into two smaller
trees as we walk, cutting off the unused pathways.
Rotations in top down splay
Rotations in top down splaying
Example
 Find 3
Steps
 1. Divide into three trees, Left, Right and
Middle.
2. Zag - Zig
3. Zig-Zag
Resulting Tree

 Check with Bottom-up Splaying.


Insert 11
Zig-Zig
Zag-Zag
Resultant Tree
Amortized Analysis
 Amortized analysis is a technique for analyzing an
algorithm’s behaviour over a sequence of
operations.
 Amortized Analysis is applied to algorithms where
an occasional operation is very slow, but most of
the other operations are faster.
 In Amortized Analysis, we analyze a sequence of
operations and guarantee a worst case average time
which is lower than the worst case time of a
particular expensive operation.
Amortized Analysis
 Amortized analysis is an upper bound. It's the
average performance of each operation in the
worst case.
 This is different from average case analysis,
wherein averaging argument is given over all
inputs for a specific operation. Inputs are
modelled using a suitable probability
distribution. In amortized analysis, no
probability is involved.
Three Methods of Amortized Analysis
 Aggregate analysis:
Total cost of n operations/n.

 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.

 The solution to this trade-off problem is to use


Dynamic Table (or Arrays). The idea is to
increase size of table whenever it becomes full.
Example Hash table insertion
 Following are the steps to follow when table
becomes full.
 Allocate memory for a larger table of size,
typically twice the old table.
 Copy the contents of old table to new table.
 Free the old table.
 If the table has space available, we simply insert
new item in available space.
Example Hash table insertion
Example Hash table insertion
 If we use simple analysis, the worst case cost of
an insertion is O(n). Therefore, worst case cost
of n inserts is n * O(n) which is O(n*n). This
analysis gives an upper bound, but not a tight
upper bound for n insertions.
Example Hash table insertion
Amortized Analysis
 Accounting Method
 Idea: When an operation’s 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.
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.

s≥k Multipop(S, k)
items s–k
items
Multipop(S, k)
sk
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

Pays for the push and a future pop.


Accounting Method (continued)
 The Push operation has cost 1; the additional 1
is stored as credit with the pushed element.

 There is always enough credit to pay for each


operation.

 Each amortized cost is O(1) Thus, the cost of


the entire sequence of n operations is O(n).
The Potential Method
• Same as accounting method: something
prepaid is used later.
• Different from accounting method
– The prepaid work not as credit, but as
“potential energy”, or “potential”.
– The potential is associated with the data
structure as a whole rather than with
specific objects within the data structure.
The Potential Method (cont.)

• Initial data structure D0,


• n operations, resulting in D0, D1,…, Dn with costs c1, c2,
…, cn.
• A potential function : {Di}  R (real numbers)
 (Di) is called the potential of Di.
• Amortized cost ci' of the ith operation is:
– ci' = ci + (Di) - (Di-1). (actual cost + potential change)
 i=1n ci' = i=1n (ci + (Di) - (Di-1))
• = i=1n ci + (Dn) - (D0)
The Potential Method (cont.)
• If (Dn)  (D0), then total amortized cost is an upper bound of
total actual cost.
• But we do not know how many operations, so (Di)  (D0) is
required for any i.
• It is convenient to define (D0)=0,and so (Di) 0, for all i.
• If the potential change is positive (i.e., (Di) - (Di-1)>0), then
ci' is an overcharge (so store the increase as potential),
• otherwise, undercharge (discharge the potential to pay the actual
cost).
Example Stack operation
• Potential for a stack is the number of objects in the stack.

• So (D0)=0, and (Di) 0

• Amortized cost of stack operations:

– 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)

 Potential change: (Di)- (Di-1) = –k'.

 Amortized cost: ci' = ci + (Di) - (Di-1)=k'+(-k')=0.

 Total amortized cost of n operations is O(n).

 Since total amortized cost is an upper bound of actual cost,


the worst case cost of n operations is O(n).
Splay Tree Amortized analysis
 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) = vT r(v).
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
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 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

 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.
Proposition 1
Proposition
Proposition1: Letbe
1:Let bethethechange
changeininr(T)
r(T)caused
causedby byaasingle
single
substep.
substep. Let
Letxxbebethe
the“x”
“x”in 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…


Case 1: zig-zig
z x
10 30
y y
Only the ranks of x, y, and 20 20
z change. Also, r(x) = r(z), T1 30
x z 10 T4
r(y)  r(x), and r(y)  r(x). T2 T3
Thus, T1 T2
T3 T4
 = 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). (*)
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,
r(z)  2r(x) – r(x) – 2. (**) then lg a + lg b  2 lg c – 2.

By (*) and (**),   r(x) + (2r(x) – r(x) – 2) – 2r(x)


= 3(r(x) – r(x)) – 2.
Case 2: zig-zag
z 10 x 20
y
30
x
Only the ranks of x, y, and T1 20 z 10 y 30
z change. Also, r(x) = r(z) T4
and r(x)  r(y). Thus, T2 T3 T1 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.
Case 3: zig
y 10 x 20
x
20
T1 30
w y 10 w 30
T2
T3 T4 T1 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)).
Proposition 2
Proposition
Proposition2: 2:Let
LetTTbe
beaasplay
splaytree
treewith
withroot
roott,t,and letbe
andlet bethe
the
total
totalvariation
variationof
ofr(T)
r(T)caused
causedbybysplaying
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) = x’s initial rank, ri(x) = x’s rank after the ith substep, and
i = the variation of r(T) caused by the ith substep, where 1  i  p.
p p

By Proposition 1, Δ   δ i    3(ri (x)  ri 1 (x))  2  2


i 1 i 1

 3(rp (x)  r0 (x))  2p  2


 3(r(t)  r(x))  d  2
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)
A Bound on Amortized Cost

We have:

Amortized Cost of Splaying


=d+
 d + (3(r(t) – r(x)) – d + 2) {Prop. 2}
= 3(r(t) – r(x)) + 2
< 3r(t) + 2
= 3lg(2n + 1) + 2 {Recall t is the root}
= O(lg n)

You might also like