CHPT 3 Splay Trees
CHPT 3 Splay Trees
6
v
3 8
z
4
Splay Trees 1
Splay Trees are
Binary Search Trees (20,Z) all the keys in the blue
note that two keys of region are 20
equal value may be well-
separated
(10,A) (35,R)
BST Rules:
items stored only at internal (14,J)
(7,T) (21,O) (37,P)
nodes
keys stored at nodes in the
left subtree of v are less (1,Q) (8,N) (36,L) (40,X)
than or equal to the key
stored at v
keys stored at nodes in the (1,C) (5,H) (7,P) (10,U)
right subtree of v are
greater than or equal to the
key stored at v all the keys in the yellow
(2,R) (5,G)
region are 20
An inorder traversal will
return the keys in order (5,I) (6,Y)
Splay Trees 2
Searching in a Splay Tree:
Starts the Same as in a BST
(20,Z)
Search proceeds down
the tree to found item (10,A) (35,R)
or an external node.
Example: Search for (7,T)
(14,J)
(21,O) (37,P)
time with key 11.
(1,Q) (8,N) (36,L) (40,X)
(2,R) (5,G)
(5,I) (6,Y)
Splay Trees 3
Example Searching in a BST,
continued
(20,Z)
(14,J)
(7,T) (21,O) (37,P)
(2,R) (5,G)
(5,I) (6,Y)
Splay Trees 4
Splay Trees do Rotations after
Every Operation (Even Search)
new operation: splay
splaying moves a node to the root using rotations
y x
a right rotation about y a left rotation about x
x y
x y
T3 T1
y x
T1 T2 T2 T3
T1 T3
Splay Trees 5
Splaying: “x is a left-left grandchild” means x is a left child of its
parent, which is itself a left child of its parent
p is x’s parent; g is p’s parent
start with
node x
is x a left-left
is x the yes
stop grandchild? zig-zig
root?
yes right-rotate about g,
no right-rotate about p
is x a right-right
is x a child of no grandchild? zig-zig
the root? left-rotate about g,
yes
left-rotate about p
yes
is x a right-left
grandchild? zig-zag
is x the left
no yes left-rotate about p,
child of the
right-rotate about g
root?
is x a left-right
yes zig zig grandchild? zig-zag
right-rotate left-rotate about yes right-rotate about p,
about the root the root left-rotate about g
Splay Trees 6
Visualizing the
Splaying Cases zig-zag x
z
z
y z y
y
T4 T1 x
x T4 T1 T2 T3 T4
T3
zig-zig T2 T3
T1 T2
x y
zig
x x
T1 y
T4
z w w y
T2
T3
T3 T4 T1 T2 T3 T4
T1 T2
Splay Trees 7
Splaying Example (10,A)
(20,Z)
(35,R)
let x = (8,N) g
x
left-rotate around p, then right-
(1,C)
rotate around g
(5,H) (7,P) (10,U)
(2,R) (5,G)
(20,Z)
(5,I) (6,Y)
(20,Z)
g (10,A) (35,R)
(14,J) (35,R)
x (8,N)
(21,O) (37,P)
x (8,N)
g
p (7,T) (10,U) (36,L) (40,X) p (7,T) (10,A) (21,O) (37,P)
(1,C)
2. (1,C) (10,U)
3.
(5,H) (5,H)
(after first rotation)
(after second
(2,R) (5,G) (2,R) (5,G)
rotation)
(5,I) (6,Y) (5,I) (6,Y) x is not yet the root, so
Splay Trees we splay again 8
Splaying Example, Continued
now x is the left child of the root
right-rotate around root
(20,Z)
(35,R)
x (8,N)
rotation)
(5,I) (6,Y)
(1,C) (5,H) (14,J)
(21,O) (37,P)
(5,I) (6,Y)
(10,A) (35,R)
of Splaying before
(7,T)
(14,J)
(21,O) (37,P)
(10,A) (37,P)
(10,A) (40,X)
(14,J) (35,R)
(14,J) (37,P) (7,T)
(7,T)
(1,Q) (8,N)
(1,Q) (8,N) (35,R) (21,O) (36,L)
(2,R) (5,G)
(2,R) (5,G)
after first splay after second
(6,Y)
(5,I) (6,Y) splay
(5,I)
Splay Trees 10
Splay Tree Definition
a splay tree is a binary search tree where a
node is splayed after it is accessed (for a
search or update)
deepest internal node accessed is splayed
splaying costs O(h), where h is height of the tree
– which is still O(n) worst-case
O(h) rotations, each of which is O(1)
Splay Trees 11
Splay Trees & Ordered
Dictionaries
which nodes are splayed after each operation?
Splay Trees 12
Amortized Analysis of
Splay Trees
Running time of each operation is proportional to time
for splaying.
Define rank(v) as the logarithm (base 2) of the number
of nodes in subtree rooted at v.
Costs: zig = $1, zig-zig = $2, zig-zag = $2.
Thus, cost for playing a node at depth d = $d.
Imagine that we store rank(v) cyber-dollars at each
node v of the splay tree (just for the sake of analysis).
Splay Trees 13
Cost per zig
y zig
x x
T4
w w y
T3
T1 T2 T3 T4
T1 T2
Splay Trees 14
Cost per zig-zig and zig-zag
z x
y
T4
zig-zig T1 y
x z
T3 T2
T1 T2 T3 T4
z zig-zag x
y z y
T1 x
T4 T1 T2 T3 T4
T2 T3
Splay Trees 15
Cost of Splaying
Cost of splaying a node x at depth d of a tree
rooted at r:
at most 3(rank(r) - rank(x)) - d + 2:
Proof: Splaying x takes d/2 splaying substeps:
d /2
cost cost i
i 1
d /2
(3( rank i ( x ) rank i 1 ( x )) 2) 2
i 1
3( rank ( r ) rank 0 ( x )) 2( d / d ) 2
3( rank ( r ) rank ( x )) d 2.
Splay Trees 16
Performance of
Splay Trees
Recall: rank of a node is logarithm of its size.
Thus, amortized cost of any splay operation is
O(log n).
In fact, the analysis goes through for any
reasonable definition of rank(x).
This implies that splay trees can actually adapt
to perform searches on frequently-requested
items much faster than O(log n) in some
cases. (See Theorems 3.10 and 3.11.)
Splay Trees 17