Professional Documents
Culture Documents
Splay
Splay Step
Splaying pushing a node to the root.
If q = null or q is the root, do nothing (splay is
over). Else
If q is at level 2, do a one-level move and
terminate the splay operation.
q
p
a,b,c,d - subtrees
p
b
Splay Step
If q is at a level > 2, do a two-level move and continue the
splay operation.
gp
gp
c
b
q is a left child of left child of gp,
or
gp
gp
d
a
q
b
Performing insert( x )
Performing search(k)
20
20
10
6
40
15
10
30
25
40
47
30
25
If there is already a node contains key x, then this node is the splay
node.
Otherwise, the newly inserted node is the splay node.
Example insert(47)
15
y
x
T1
T1
T2
T2
split(k)
Split generate two subtrees, one with all its keys larger than k,
and one with all keys smaller than k.
Use the unbalanced binary search tree insert algorithm to insert a a
new key k.
Then splay on k.
Following the splay, the left and right subtrees are the output trees.
TS
L
T
BR
T1
kill x and
concat
Splay x
T2
T1
T2
Main Result
Theorem: Any sequence of m operations on an
initially empty splay tree that never contains
more than n nodes takes O(m log n)