CSC 378: Data Structures and Algorithm Analysis
Splay Trees
[ L & D pg 243 - 251 ]
A splay tree is a BST which is not necessarily balanced.
Operations on a splay tree of n nodes can take worst-case time in . However, a sequence of operations takes worst-case time in .
This means that the average time of one operation is . Also this guarantees an upper bound on the total time for a sequence of operations. So this is a stronger statement than one that gives only an average time per operation.
Member(k,S)
Insert(x,S)
Delete(k,S)
Join(S,S')
Split(S,S',S'')
Everything is based on the Splay operation:
Splay(k,S)
Here's how the operations are written in terms of Splay:
Split(k,S,S',S'')
left(x) = S'
right(x) = S''
Join(left(S), right(S)).
Splay( ,S)
Then do right(S) = S', which results in
right(S) = S'
Then S' is S + left(S) and S'' is right(S).
Note the each operation takes a constant number of splay operations, plus some constant number of minor operations, like pointer manipulation. So we'll only count the cost of Splay operations in analysis.
Let x be a node with key k. Splay(k,S) uses rotations to move x up to the root.
The obvious way is to keep rotating at the node containing k until it reaches the root. But if this is done, a sequence of m operations can take time in , rather than as claimed above.
So we must carefully choose the rotations that move a node up to the root.
Each time we move the node upward, we're faced with one of the following situations:
The operations produce the restructurings:
Here is an example in which we Splay(5,S):
The amortized analysis of the splay operation is quite involved. Those interested in it can find a clear presentation in The Design and Analysis of Algorithms, by Dexter Kozen, pages 58 to 64 (ISBN 0-387-97687-6, Springer Verlag, 1992).