Professional Documents
Culture Documents
10 40
6 15 30
2 8 25
10 40
6 15 30
2 8 25
10 40
6 15 30
2 8 25
S B
• m is set to null if it is the newly inserted pair.
Splay
a b b c
a b c d
• q right child of right child of gp is symmetric.
2-Level Move (case 2)
q
gp
p gp
p d
a b c d
a q
b c
1 1 2
2 1
Per Operation Actual Complexity
3 3
2
2 2 4
1 3
1 1
Per Operation Actual Complexity
• Worst-case height = n.
• Actual complexity of search, insert, delete,
and split is O(n).
Top-Down Splay Trees
• On the way down the tree, split the tree into
the binary search trees S (small elements)
and B (big elements).
Similar to split operation in an unbalanced binary
search tree.
However, a rotation is done whenever an LL or RR
move is made.
Move down 2 levels at a time, except (possibly) in the
end when a one level move is made.
• When the splay node is reached, S, B, and the
subtree rooted at the splay node are combined into
a single binary search tree.
Split A Binary Search Tree
A S B
a B
C b
c D
d E
m e
f g
Split A Binary Search Tree
S B
B A
C b
a
c D
d E
m e
f g
Split A Binary Search Tree
S B
A B
C a b
c D
d E
m e
f g
Split A Binary Search Tree
S B
A B
a C b
D
c
d E
m e
f g
Split A Binary Search Tree
S B
A B
a C b
c D
E
d
m e
f g
Split A Binary Search Tree
S B
A B
a C E b
e
c D
d
m
f g
Split A Binary Search Tree
S B
A B
a C E b
g e
c D
d f
m
Two-Level Moves
A • Let m be the splay node.
a B • RL move from A to C.
C b • RR move from C to E.
• L move from E to m.
c D
d E
m e
f g
RL Move
A S B
a B
C b
c D
d E
m e
f g
RL Move
S B
A B
C a b
c D
d E
m e
f g
RR Move
S B
A B
C a b
c D
d E
m e
f g
RR Move
S B
A B
a D b
C
E c d
m e
f g
L Move
S B
A B
a D b
C
E c d
m e
f g
L Move
S B
A B
a D E b
C e
c d
m
f g
Wrap Up
m S B
f g A B
a D E b
C e
c d
Wrap Up
m S B
A B
a D E b
C f g e
c d
Wrap Up
m
A B
a D E b
C f g e
c d
Bottom Up vs Top Down