CSE 326 Splay Trees

CSE 326
Splay Trees
David Kaplan
Dept of Computer Science & Engineering
Autumn 2001
Motivation for Splay Trees
Problems with AVL Trees
 extra storage/complexity for height fields
 ugly delete code
Solution: splay trees




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
2
Splay Tree Idea
10
You’re forced to make
a really deep access:
17
Since you’re down there anyway,
fix up a lot of deep nodes!
5
2
9
3
Splay Trees
CSE 326 Autumn 2001
3
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
Splay Trees
CSE 326 Autumn 2001
4
Access root:
Do nothing (that was easy!)
X
Splay Trees
root
root
n
n
Y
X
CSE 326 Autumn 2001
Y
5
Access child of root:
Zig (AVL single rotation)
root
p
root
n
n
p
Z
X
Splay Trees
Y
X
Y
CSE 326 Autumn 2001
Z
6
Access (LR, RL) grandchild:
Zig-Zag (AVL double rotation)
g
n
p
X
g
p
n
W
X
Y
Splay Trees
Y
Z
W
Z
CSE 326 Autumn 2001
7
Access (LL, RR) grandchild:
Zig-Zig
Rotate top-down – why?
n
g
p
p
W
Z
n
g
X
Y
Y
Splay Trees
Z
CSE 326 Autumn 2001
W
X
8
Splaying Example:
Find(6)
1
1
2
2
zig-zig
3
3
Find(6)
4
6
5
Splay Trees
5
6
CSE 326 Autumn 2001
4
9
… still splaying …
1
1
2
6
zig-zig
3
3
6
2
5
Splay Trees
4
5
4
CSE 326 Autumn 2001
10
… 6 splayed out!
1
6
6
1
zig
3
2
3
5
2
4
Splay Trees
5
4
CSE 326 Autumn 2001
11
Splay it Again, Sam!
Find (4)
6
6
1
1
zig-zag
3
4
Find(4)
2
5
4
Splay Trees
3
5
2
CSE 326 Autumn 2001
12
… 4 splayed out!
6
4
1
1
6
zig-zag
3
4
3
5
5
2
2
Splay Trees
CSE 326 Autumn 2001
13
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.)
Splay Trees
CSE 326 Autumn 2001
14
Splay Operations: Find
 Find the node in normal BST manner
 Splay the node to the root
Splay Trees
CSE 326 Autumn 2001
15
Splay Operations: Insert
 Ideas?
 Can we just do BST insert?
Splay Trees
CSE 326 Autumn 2001
16
Digression: Splitting
 Split(T, x) creates two BSTs L and R:




Splay Trees
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
17
Splitting in Splay Trees
How can we split?





Splay Trees
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
18
Split
split(x)
splay
T
L
R
OR
L
x
Splay Trees
CSE 326 Autumn 2001
R
L
>x
<x
R
x
19
Back to Insert
x
split(x)
L
<x
R
>x
L
R
void insert(Node *& root, Object x)
{
Node * left, * right;
split(root, left, right, x);
root = new Node(x, left, right);
}
Splay Trees
CSE 326 Autumn 2001
20
Splay Operations: Delete
x
find(x)
delete x
L
R
L
<x
R
>x
Now what?
Splay Trees
CSE 326 Autumn 2001
21
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
Splay Trees
CSE 326 Autumn 2001
22
Delete Completed
x
find(x)
T
delete x
L
R
L
<x
R
>x
Join(L,R)
T-x
Splay Trees
CSE 326 Autumn 2001
23
Insert Example
4
4
6
6
split(5)
1
1
6
1
9
9
9
2
4
2
7
7
7
5
2
4
Insert(5)
6
1
Splay Trees
CSE 326 Autumn 2001
9
2
7
24
Delete Example
6
6
1
1
9
4
4
find(4)
6
9
2
7
9
2
2
1
7
Find max
7
2
2
6
Delete(4)
Splay Trees
1
1
6
9
7
CSE 326 Autumn 2001
9
7
25
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
Splay Trees
CSE 326 Autumn 2001
26
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)
Splay Trees
CSE 326 Autumn 2001
27