CHPT 3 Splay Trees
CHPT 3 Splay Trees
v
3
z
4
Splay Trees
BST Rules:
items stored only at internal nodes keys stored at nodes in the left subtree of v are less than or equal to the key stored at v keys stored at nodes in the right subtree of v are greater than or equal to the key stored at v
(7,T) (1,Q)
(14,J)
(21,O)
(36,L)
(37,P) (40,X)
(8,N)
(1,C)
(5,H)
(7,P)
(10,U)
(2,R) (5,I)
(5,G)
(6,Y)
(20,Z)
(10,A)
(35,R)
(7,T)
(14,J)
(21,O) (36,L)
(37,P)
(8,N)
(40,X)
(1,C)
(5,H)
(7,P)
(10,U)
(2,R) (5,I)
(5,G) (6,Y)
Splay Trees
(10,A)
(35,R)
(14,J)
(21,O) (36,L)
(37,P)
(8,N)
(40,X)
(1,C)
(5,H)
(7,P)
(10,U)
(2,R) (5,I)
(5,G) (6,Y)
Splay Trees
right rotation
left rotation
makes the left child x of a node y into ys parent; y becomes the right child of x
y a right rotation about y
makes the right child y of a node x into xs parent; x becomes the left child of y
x y a left rotation about x
x x T3 y T1 T2 T1 T2 T1
x
T3 T3
T2
T3
T1
T2
Splay Trees
Splaying:
start with node x is x the root?
no yes
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 xs parent; g is ps parent
is x a left-left grandchild?
yes
stop
zig-zig
right-rotate about g, right-rotate about p
no
is x a right-right grandchild?
yes
zig-zig
left-rotate about g, left-rotate about p
zig-zag
left-rotate about p, right-rotate about g
zig
zig
left-rotate about the root
is x a left-right grandchild?
yes
zig-zag
right-rotate about p, left-rotate about g
6
Splay Trees
zig-zag
z
T1
x T4 T1 T2 T3 T4
zig-zig
T2
T3
zig
x T4 w y
T1
T2
T3
T4
Splay Trees
Splaying Example
(10,A)
(20,Z)
(35,R)
let x = (8,N) x is the right child of its parent, which is the left child of the grandparent left-rotate around p, then rightrotate around g
(20,Z)
g p
(1,Q) (7,T) (14,J) (21,O) (37,P)
1.
(before rotating)
(8,N)
(36,L)
(40,X)
x
(1,C) (5,H) (7,P) (10,U)
(2,R) (5,I)
g
x p
(1,Q) (8,N)
(10,A)
(35,R)
x
p
(1,Q)
(8,N)
(35,R)
g
(7,T)
(36,L) (40,X)
(7,T)
(10,A)
(21,O)
(37,P)
(7,P)
(7,P)
(14,J)
(36,L)
(40,X)
(1,C)
(5,H)
2.
(after first rotation)
(5,G)
(1,C)
(5,H)
(10,U)
3.
(after second rotation)
x is not yet the root, so we splay again 8
(2,R)
(5,I)
(5,G) (6,Y)
Splay Trees
x
(7,T)
(8,N)
(10,A)
(21,O)
(37,P)
(1,Q)
(7,P)
(14,J)
(36,L)
(40,X)
x
(7,T)
(8,N)
2.
(20,Z)
(1,C)
(5,H)
(10,U)
1.
(2,R) (5,I) (5,G) (6,Y)
(after rotation)
(35,R)
(1,Q)
(7,P)
(10,A)
(5,H)
(14,J)
(21,O)
(37,P)
(2,R) (5,I)
(5,G) (6,Y)
(10,U)
(36,L)
(40,X)
(20,Z)
(10,A)
(35,R)
before
(7,T) (14,J) (21,O) (8,N) (37,P) (36,L) (40,X)
tree might not be more balanced (1,Q) e.g. splay (40,X) before, the depth of the shallowest leaf is(1,C) (5,H) 3 and the deepest is 7 after, the depth of shallowest leaf is 1 (2,R) and deepest is 8
(5,I) (20,Z)
(7,P)
(10,U)
(37,P)
(7,T)
(14,J)
(37,P) (1,Q)
(7,T)
(1,Q)
(8,N)
(35,R)
(8,N)
(21,O)
(36,L)
(5,H)
(7,P)
(10,U)
(5,G)
Splay Trees
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
insertElement
removeElement
use the parent of the internal node that was actually removed from the tree (the parent of the node that the removed item was swapped with)
Splay Trees
12
Splay Trees
13
Doing a zig at x costs at most rank(x) - rank(x): cost = rank(x) + rank(y) - rank(y) - rank(x) < rank(x) - rank(x).
Splay Trees 14
zig-zig
T1 T2
y
z
T3
T4
Doing a zig-zig or zig-zag at x costs at most 3(rank(x) - rank(x)) - 2. Proof: See Theorem 3.9, Page 192.
z y T1 x T4 T1 T2 T3 T4
zig-zag
z
x y
T2
T3 Splay Trees 15
Cost of Splaying
Cost of splaying a node x at depth d of a tree rooted at r:
cost costi
i 1
d /2
d /2