S Play Trees
S Play Trees
Splay Trees
15
40
55
56
77
95
12/14/2014
12/14/2014
Splay Trees
If any particular operation is O( N ) and we still want
amortized O( lg N ) performance, then whenever a node is
accessed it must be moved. Otherwise its access time is
always O( N ).
The basic idea of the splay tree is that every time a node is
accessed, it is pushed to the root by a series of tree
rotations. This series of tree rotations is knowing as
splaying.
Rotating k1 around k2
Splay Operation
To splay node x, traverse up the tree from node x to root,
rotating along the way until x is the root
Each rotation
If x is root, do nothing.
If x has no grandparent, rotate x about its parent
If x has a grandparent,
if x and its parent are both left children or both right children,
rotate the parent about the grandparent, then rotate x about its
parent
if x and its parent are opposite type children (one left and the
other right), rotate x about its parent, then rotate x about its
new parent (former grandparent)
12/14/2014
12/14/2014
10
12/14/2014
11
12/14/2014
12
Exercise - find( 65 )
50
40
20
60
43
70
65
16
63
12/14/2014
66
13
Exercise - remove( 25 )
50
40
20
16
60
43
70
65
25
63
12/14/2014
66
14
15
12/14/2014
16
12/14/2014
17
18