<s>
A	O
splay	B-Data_Structure
tree	I-Data_Structure
is	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
with	O
the	O
additional	O
property	O
that	O
recently	O
accessed	O
elements	O
are	O
quick	O
to	O
access	O
again	O
.	O
</s>
<s>
Like	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
,	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
performs	O
basic	O
operations	O
such	O
as	O
insertion	O
,	O
look-up	O
and	O
removal	O
in	O
O(log n )	O
amortized	B-General_Concept
time	I-General_Concept
.	O
</s>
<s>
For	O
random	O
access	O
patterns	O
drawn	O
from	O
a	O
non-uniform	O
random	O
distribution	O
,	O
their	O
amortized	B-General_Concept
time	I-General_Concept
can	O
be	O
faster	O
than	O
logarithmic	O
,	O
proportional	O
to	O
the	O
entropy	O
of	O
the	O
access	O
pattern	O
.	O
</s>
<s>
For	O
many	O
patterns	O
of	O
non-random	O
operations	O
,	O
also	O
,	O
splay	B-Data_Structure
trees	I-Data_Structure
can	O
take	O
better	O
than	O
logarithmic	O
time	O
,	O
without	O
requiring	O
advance	O
knowledge	O
of	O
the	O
pattern	O
.	O
</s>
<s>
According	O
to	O
the	O
unproven	O
dynamic	O
optimality	O
conjecture	O
,	O
their	O
performance	O
on	O
all	O
access	O
patterns	O
is	O
within	O
a	O
constant	O
factor	O
of	O
the	O
best	O
possible	O
performance	O
that	O
could	O
be	O
achieved	O
by	O
any	O
other	O
self-adjusting	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
,	O
even	O
one	O
selected	O
to	O
fit	O
that	O
pattern	O
.	O
</s>
<s>
The	O
splay	B-Data_Structure
tree	I-Data_Structure
was	O
invented	O
by	O
Daniel	O
Sleator	O
and	O
Robert	O
Tarjan	O
in	O
1985	O
.	O
</s>
<s>
All	O
normal	O
operations	O
on	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
are	O
combined	O
with	O
one	O
basic	O
operation	O
,	O
called	O
splaying	B-Data_Structure
.	O
</s>
<s>
Splaying	B-Data_Structure
the	O
tree	O
for	O
a	O
certain	O
element	O
rearranges	O
the	O
tree	O
so	O
that	O
the	O
element	O
is	O
placed	O
at	O
the	O
root	O
of	O
the	O
tree	O
.	O
</s>
<s>
One	O
way	O
to	O
do	O
this	O
with	O
the	O
basic	O
search	O
operation	O
is	O
to	O
first	O
perform	O
a	O
standard	O
binary	O
tree	O
search	O
for	O
the	O
element	O
in	O
question	O
,	O
and	O
then	O
use	O
tree	B-Data_Structure
rotations	I-Data_Structure
in	O
a	O
specific	O
fashion	O
to	O
bring	O
the	O
element	O
to	O
the	O
top	O
.	O
</s>
<s>
Good	O
performance	O
for	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
depends	O
on	O
the	O
fact	O
that	O
it	O
is	O
self-optimizing	O
,	O
in	O
that	O
frequently	O
accessed	O
nodes	O
will	O
move	O
nearer	O
to	O
the	O
root	O
where	O
they	O
can	O
be	O
accessed	O
more	O
quickly	O
.	O
</s>
<s>
Having	O
frequently-used	O
nodes	O
near	O
the	O
root	O
is	O
an	O
advantage	O
for	O
many	O
practical	O
applications	O
(	O
also	O
see	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
)	O
,	O
and	O
is	O
particularly	O
useful	O
for	O
implementing	O
caches	B-General_Concept
and	O
garbage	B-General_Concept
collection	I-General_Concept
algorithms	O
.	O
</s>
<s>
Comparable	O
performance	O
:	O
Average-case	O
performance	O
is	O
as	O
efficient	O
as	O
other	O
trees	B-Application
.	O
</s>
<s>
Small	O
memory	O
footprint	O
:	O
Splay	B-Data_Structure
trees	I-Data_Structure
do	O
not	O
need	O
to	O
store	O
any	O
bookkeeping	O
data	O
.	O
</s>
<s>
The	O
most	O
significant	O
disadvantage	O
of	O
splay	B-Data_Structure
trees	I-Data_Structure
is	O
that	O
the	O
height	O
of	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
can	O
be	O
linear	O
.	O
</s>
<s>
However	O
the	O
amortized	B-General_Concept
access	O
cost	O
of	O
this	O
worst	O
case	O
is	O
logarithmic	O
,	O
O(log n )	O
.	O
</s>
<s>
The	O
representation	O
of	O
splay	B-Data_Structure
trees	I-Data_Structure
can	O
change	O
even	O
when	O
they	O
are	O
accessed	O
in	O
a	O
'	O
read-only	O
'	O
manner	O
(	O
i.e.	O
</s>
<s>
This	O
complicates	O
the	O
use	O
of	O
such	O
splay	B-Data_Structure
trees	I-Data_Structure
in	O
a	O
multi-threaded	O
environment	O
.	O
</s>
<s>
Finally	O
,	O
when	O
the	O
access	O
pattern	O
is	O
random	O
,	O
the	O
additional	O
splaying	B-Data_Structure
overhead	O
adds	O
a	O
significant	O
constant	O
factor	O
to	O
the	O
cost	O
compared	O
to	O
less-dynamic	O
alternatives	O
.	O
</s>
<s>
By	O
performing	O
a	O
splay	O
operation	O
on	O
the	O
node	O
of	O
interest	O
after	O
every	O
access	O
,	O
the	O
recently	O
accessed	O
nodes	O
are	O
kept	O
near	O
the	O
root	O
and	O
the	O
tree	O
remains	O
roughly	O
balanced	O
,	O
so	O
that	O
we	O
achieve	O
the	O
desired	O
amortized	B-General_Concept
time	I-General_Concept
bounds	O
.	O
</s>
<s>
Whether	O
x	O
is	O
the	O
left	O
or	O
right	O
child	O
of	O
its	O
parent	B-Application
node	I-Application
,	O
p	O
,	O
</s>
<s>
(	O
In	O
the	O
following	O
diagrams	O
,	O
circles	O
indicate	O
nodes	O
of	O
interest	O
and	O
triangles	O
indicate	O
sub-trees	O
of	O
arbitrary	O
size	O
.	O
)	O
</s>
<s>
The	O
tree	O
is	O
rotated	O
on	O
the	O
edge	O
joining	O
p	O
with	O
its	O
parent	O
g	O
,	O
then	O
rotated	O
on	O
the	O
edge	O
joining	O
x	O
with	O
p	O
.	O
Note	O
that	O
zig-zig	O
steps	O
are	O
the	O
only	O
thing	O
that	O
differentiate	O
splay	B-Data_Structure
trees	I-Data_Structure
from	O
the	O
rotate	O
to	O
root	O
method	O
introduced	O
by	O
Allen	O
and	O
Munro	O
prior	O
to	O
the	O
introduction	O
of	O
splay	B-Data_Structure
trees	I-Data_Structure
.	O
</s>
<s>
Given	O
two	O
trees	B-Application
S	O
and	O
T	O
such	O
that	O
all	O
elements	O
of	O
S	O
are	O
smaller	O
than	O
the	O
elements	O
of	O
T	O
,	O
the	O
following	O
steps	O
can	O
be	O
used	O
to	O
join	O
them	O
to	O
a	O
single	O
tree	O
:	O
</s>
<s>
Given	O
a	O
tree	O
and	O
an	O
element	O
x	O
,	O
return	O
two	O
new	O
trees	B-Application
:	O
one	O
containing	O
all	O
elements	O
less	O
than	O
or	O
equal	O
to	O
x	O
and	O
the	O
other	O
containing	O
all	O
elements	O
greater	O
than	O
x	O
.	O
</s>
<s>
Split	O
the	O
right	O
subtree	B-Application
from	O
the	O
rest	O
of	O
the	O
tree	O
.	O
</s>
<s>
To	O
insert	O
a	O
value	O
x	O
into	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
:	O
</s>
<s>
Insert	O
x	O
as	O
with	O
a	O
normal	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
Use	O
the	O
split	O
operation	O
to	O
split	O
the	O
tree	O
at	O
the	O
value	O
of	O
x	O
to	O
two	O
sub-trees	O
:	O
S	O
and	O
T	O
.	O
</s>
<s>
To	O
delete	O
a	O
node	O
x	O
,	O
use	O
the	O
same	O
method	O
as	O
with	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
:	O
</s>
<s>
Swap	O
its	O
value	O
with	O
that	O
of	O
either	O
the	O
rightmost	O
node	O
of	O
its	O
left	O
sub	O
tree	O
(	O
its	O
in-order	O
predecessor	O
)	O
or	O
the	O
leftmost	O
node	O
of	O
its	O
right	O
subtree	B-Application
(	O
its	O
in-order	O
successor	O
)	O
.	O
</s>
<s>
Unlike	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
in	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
after	O
deletion	O
,	O
we	O
splay	O
the	O
parent	O
of	O
the	O
removed	O
node	O
to	O
the	O
top	O
of	O
the	O
tree	O
.	O
</s>
<s>
leaves	O
the	O
tree	O
with	O
two	O
sub	O
trees	B-Application
.	O
</s>
<s>
The	O
two	O
sub-trees	O
are	O
then	O
joined	O
using	O
a	O
"	O
join	O
"	O
operation	O
.	O
</s>
<s>
Splaying	B-Data_Structure
,	O
as	O
mentioned	O
above	O
,	O
is	O
performed	O
during	O
a	O
second	O
,	O
bottom-up	O
pass	O
over	O
the	O
access	O
path	O
of	O
a	O
node	O
.	O
</s>
<s>
This	O
top-down	O
splaying	B-Data_Structure
routine	O
uses	O
three	O
sets	O
of	O
nodes	O
–	O
left	O
tree	O
,	O
right	O
tree	O
and	O
middle	O
tree	O
.	O
</s>
<s>
Below	O
there	O
is	O
an	O
implementation	O
of	O
splay	B-Data_Structure
trees	I-Data_Structure
in	O
C++	O
,	O
which	O
uses	O
pointers	O
to	O
represent	O
each	O
node	O
on	O
the	O
tree	O
.	O
</s>
<s>
This	O
implementation	O
is	O
based	O
on	O
bottom-up	O
splaying	B-Data_Structure
version	O
and	O
uses	O
the	O
second	O
method	O
of	O
deletion	O
on	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
A	O
simple	O
amortized	B-General_Concept
analysis	I-General_Concept
of	O
static	O
splay	B-Data_Structure
trees	I-Data_Structure
can	O
be	O
carried	O
out	O
using	O
the	O
potential	B-General_Concept
method	I-General_Concept
.	O
</s>
<s>
Φ	O
will	O
tend	O
to	O
be	O
high	O
for	O
poorly	O
balanced	B-Data_Structure
trees	I-Data_Structure
and	O
low	O
for	O
well-balanced	O
trees	O
.	O
</s>
<s>
To	O
apply	O
the	O
potential	B-General_Concept
method	I-General_Concept
,	O
we	O
first	O
calculate	O
ΔΦ	O
:	O
the	O
change	O
in	O
the	O
potential	O
caused	O
by	O
a	O
splay	O
operation	O
.	O
</s>
<s>
The	O
amortized	B-General_Concept
cost	O
of	O
any	O
operation	O
is	O
ΔΦ	O
plus	O
the	O
actual	O
cost	O
.	O
</s>
<s>
The	O
Zig	O
operation	O
adds	O
an	O
amortized	B-General_Concept
cost	O
of	O
1	O
,	O
but	O
there	O
's	O
at	O
most	O
one	O
such	O
operation	O
.	O
</s>
<s>
So	O
now	O
we	O
know	O
that	O
the	O
total	O
amortized	B-General_Concept
time	I-General_Concept
for	O
a	O
sequence	O
of	O
m	O
operations	O
is	O
:	O
</s>
<s>
To	O
go	O
from	O
the	O
amortized	B-General_Concept
time	I-General_Concept
to	O
the	O
actual	O
time	O
,	O
we	O
must	O
add	O
the	O
decrease	O
in	O
potential	O
from	O
the	O
initial	O
state	O
before	O
any	O
operation	O
is	O
done	O
( Φi	O
)	O
to	O
the	O
final	O
state	O
after	O
all	O
operations	O
are	O
completed	O
( Φf	O
)	O
.	O
</s>
<s>
The	O
same	O
analysis	O
applies	O
and	O
the	O
amortized	B-General_Concept
cost	O
of	O
a	O
splaying	B-Data_Structure
operation	O
is	O
again	O
:	O
</s>
<s>
There	O
are	O
several	O
theorems	O
and	O
conjectures	O
regarding	O
the	O
worst-case	O
runtime	O
for	O
performing	O
a	O
sequence	O
S	O
of	O
m	O
accesses	O
in	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
containing	O
n	O
elements	O
.	O
</s>
<s>
In	O
addition	O
to	O
the	O
proven	O
performance	O
guarantees	O
for	O
splay	B-Data_Structure
trees	I-Data_Structure
there	O
is	O
an	O
unproven	O
conjecture	O
of	O
great	O
interest	O
from	O
the	O
original	O
Sleator	O
and	O
Tarjan	O
paper	O
.	O
</s>
<s>
This	O
conjecture	O
is	O
known	O
as	O
the	O
dynamic	O
optimality	O
conjecture	O
and	O
it	O
basically	O
claims	O
that	O
splay	B-Data_Structure
trees	I-Data_Structure
perform	O
as	O
well	O
as	O
any	O
other	O
binary	B-Language
search	I-Language
tree	I-Language
algorithm	O
up	O
to	O
a	O
constant	O
factor	O
.	O
</s>
<s>
Dynamic	O
Optimality	O
Conjecture	O
:	O
Let	O
be	O
any	O
binary	B-Language
search	I-Language
tree	I-Language
algorithm	O
that	O
accesses	O
an	O
element	O
by	O
traversing	O
the	O
path	O
from	O
the	O
root	O
to	O
at	O
a	O
cost	O
of	O
,	O
and	O
that	O
between	O
accesses	O
can	O
make	O
any	O
rotations	O
in	O
the	O
tree	O
at	O
a	O
cost	O
of	O
1	O
per	O
rotation	O
.	O
</s>
<s>
Then	O
the	O
cost	O
for	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
to	O
perform	O
the	O
same	O
accesses	O
is	O
.	O
</s>
<s>
Traversal	O
Conjecture	O
:	O
Let	O
and	O
be	O
two	O
splay	B-Data_Structure
trees	I-Data_Structure
containing	O
the	O
same	O
elements	O
.	O
</s>
<s>
Deque	B-Application
Conjecture:..	O
Let	O
be	O
a	O
sequence	O
of	O
double-ended	B-Application
queue	I-Application
operations	O
(	O
push	O
,	O
pop	O
,	O
inject	O
,	O
eject	O
)	O
.	O
</s>
<s>
Then	O
the	O
cost	O
of	O
performing	O
on	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
is	O
.	O
</s>
<s>
Let	O
be	O
any	O
permutation	O
of	O
the	O
elements	O
of	O
the	O
splay	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
In	O
order	O
to	O
reduce	O
the	O
number	O
of	O
restructuring	O
operations	O
,	O
it	O
is	O
possible	O
to	O
replace	O
the	O
splaying	B-Data_Structure
with	O
semi-splaying	O
,	O
in	O
which	O
an	O
element	O
is	O
splayed	O
only	O
halfway	O
towards	O
the	O
root	O
.	O
</s>
<s>
Another	O
way	O
to	O
reduce	O
restructuring	O
is	O
to	O
do	O
full	O
splaying	B-Data_Structure
,	O
but	O
only	O
in	O
some	O
of	O
the	O
access	O
operations	O
–	O
only	O
when	O
the	O
access	O
path	O
is	O
longer	O
than	O
a	O
threshold	O
,	O
or	O
only	O
in	O
the	O
first	O
m	O
access	O
operations	O
.	O
</s>
