Motivation for Splay Trees
Problems with AVL Trees
extra storage/complexity for height fields
ugly delete code
Solution: splay trees
blind adjusting version of AVL trees
amortized time for all operations is O(log n)
worst case time is O(n)
insert/find always rotates node to the root!
CSE 326 Autumn 2001
Splay Trees 1
Splay Tree Idea
10
You’re forced to make 17
a really deep access:
Since you’re down there anyway,
fix up a lot of deep nodes!
2 9
CSE 326 Autumn 2001
Splay Trees 2
Splaying Cases
Node being accessed (n) is:
Root
Child of root
Has both parent (p) and grandparent (g)
Zig-zig pattern: g p n is left-left or right-right
Zig-zag pattern: g p n is left-right or right-left
CSE 326 Autumn 2001
Splay Trees 3
Access root:
Do nothing (that was easy!)
root root
n n
X Y X Y
CSE 326 Autumn 2001
Splay Trees 4
Access child of root:
Zig (AVL single rotation)
root root
p n
n p
Z X
X Y Y Z
CSE 326 Autumn 2001
Splay Trees 5
Zig-Zag (AVL double
rotation)
n
p
X
g p
n
W
X Y Z W
Y Z
CSE 326 Autumn 2001
Splay Trees 6
Access (LL, RR) grandchild:
Zig-Zig
Rotate top-down – why?
g n
p p
W Z
n g
X Y
Y Z W X
CSE 326 Autumn 2001
Splay Trees 7
Splaying Example:
Find(6)
1 1
2 2
zig-zig
3 3
Find(6)
4 6
5 5
6
CSE 326 Autumn 2001 4
Splay Trees 8
… still splaying …
1 1
2 6
zig-zig
3 3
6 2 5
5 4
4 CSE 326 Autumn 2001
Splay Trees 9
… 6 splayed out!
1 6
6 1
zig
3 3
2 5 2 5
4 4
CSE 326 Autumn 2001
Splay Trees 10
Splay it Again, Sam!
Find (4)
6 6
1 1
zig-zag
3 4
Find(4)
2 5 3 5
4 2
CSE 326 Autumn 2001
Splay Trees 11
… 4 splayed out!
6 4
1 1 6
zig-zag
4 3 5
3 5 2
CSE 326 Autumn 2001
Splay Trees 12
Why Splaying Helps
If a node n on the access path is at depth d
before the splay, it’s at about depth d/2 after
the splay
Exceptions are the root, the child of the root, and the
node splayed
Overall, nodes which are below nodes on the
access path tend to move closer to the root
Splaying gets amortized O(log n) performance.
(Maybe not now, but soon, and for the rest of the operations.)
CSE 326 Autumn 2001
Splay Trees 13
Splay Operations: Find
Find the node in normal BST manner
Splay the node to the root
CSE 326 Autumn 2001
Splay Trees 14
Splay Operations: Insert
Ideas?
Can we just do BST insert?
CSE 326 Autumn 2001
Splay Trees 15
Digression: Splitting
Split(T, x) creates two BSTs L and R:
all elements of T are in either L or R (T = L
R)
all elements in L are x
all elements in R are x
L and R share no elements (L R = )
CSE 326 Autumn 2001
Splay Trees 16
Splitting in Splay Trees
How can we split?
We have the splay operation.
We can find x or the parent of where x should
be.
We can splay it to the root.
Now, what’s true about the left subtree of the
root?
And the right?
CSE 326 Autumn 2001
Splay Trees 17
Split
split(x)
splay
T L R
OR
L R L R
x
CSE 326 Autumn 2001
>x <x x
Splay Trees 18
Back to Insert
x
split(x)
L R
<x >x L R
void insert(Node *& root, Object x)
{
Node * left, * right;
split(root, left, right, x);
root = new Node(x, left, right);
}
CSE 326 Autumn 2001
Splay Trees 19
Splay Operations: Delete
x
find(x) delete x
L R
L R <x >x
Now what?
CSE 326 Autumn 2001
Splay Trees 20
Join
Join(L, R): given two trees such that L < R, merge them
L
splay
L R R
Splay on the maximum element in L, then attach
R
CSE 326 Autumn 2001
Splay Trees 21
Delete Completed
x
find(x) delete x
L R
T
L R <x >x
Join(L,R)
T-x
CSE 326 Autumn 2001
Splay Trees 22
Insert Example 4 4 6
6
split(5) 1 6 1 9
1 9
2 9 2 7
4 7
7
2 5
4 6
Insert(5)
1 9
CSE 326 Autumn 2001 2 7
Splay Trees 23
Delete Example4 6
6
1 6 1 9
1 9 find(4)
2 9 2 7
4 7 Find max
7
2 2 2
1 6 1 6
Delete(4) 9 9
CSE 326 Autumn 20017 7
Splay Trees 24
Splay Tree Summary
Can be shown that any M consecutive operations
starting from an empty tree take at most O(M
log(N))
All splay tree operations run in amortized O(log n) time
O(N) operations can occur, but splaying makes
them infrequent
Implements most-recently used (MRU) logic
Splay tree structure is self-tuning
CSE 326 Autumn 2001
Splay Trees 25
Splay Tree Summary (cont.)
Splaying can be done top-down; better because:
only one pass
no recursion or parent pointers necessary
There are alternatives to split/insert and join/delete
Splay trees are very effective search trees
relatively simple: no extra fields required
excellent locality properties:
frequently accessed keys are cheap to find (near top of tree)
infrequently accessed keys stay out of the way (near bottom of tree)
CSE 326 Autumn 2001
Splay Trees 26