<s>
A	O
tango	B-Data_Structure
tree	I-Data_Structure
is	O
a	O
type	O
of	O
binary	B-Language
search	I-Language
tree	I-Language
proposed	O
by	O
Erik	O
D	O
.	O
Demaine	O
,	O
Dion	O
Harmon	O
,	O
John	O
Iacono	O
,	O
and	O
Mihai	O
Pătrașcu	O
in	O
2004	O
.	O
</s>
<s>
It	O
is	O
named	O
after	O
Buenos	O
Aires	O
,	O
of	O
which	O
the	O
tango	B-Algorithm
is	O
emblematic	O
.	O
</s>
<s>
It	O
is	O
an	O
online	B-Algorithm
binary	B-Language
search	I-Language
tree	I-Language
that	O
achieves	O
an	O
competitive	B-Algorithm
ratio	I-Algorithm
relative	O
to	O
the	O
offline	B-Algorithm
optimal	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
,	O
while	O
only	O
using	O
additional	O
bits	O
of	O
memory	O
per	O
node	O
.	O
</s>
<s>
This	O
improved	O
upon	O
the	O
previous	O
best	O
known	O
competitive	B-Algorithm
ratio	I-Algorithm
,	O
which	O
was	O
.	O
</s>
<s>
Tango	B-Data_Structure
trees	I-Data_Structure
work	O
by	O
partitioning	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
into	O
a	O
set	O
of	O
preferred	O
paths	O
,	O
which	O
are	O
themselves	O
stored	O
in	O
auxiliary	O
trees	O
(	O
so	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
is	O
represented	O
as	O
a	O
tree	O
of	O
trees	O
)	O
.	O
</s>
<s>
To	O
construct	O
a	O
tango	B-Data_Structure
tree	I-Data_Structure
,	O
we	O
simulate	O
a	O
complete	O
binary	B-Language
search	I-Language
tree	I-Language
called	O
the	O
reference	O
tree	O
,	O
which	O
is	O
simply	O
a	O
traditional	O
binary	B-Language
search	I-Language
tree	I-Language
containing	O
all	O
the	O
elements	O
.	O
</s>
<s>
This	O
tree	O
never	O
shows	O
up	O
in	O
the	O
actual	O
implementation	O
,	O
but	O
is	O
the	O
conceptual	O
basis	O
behind	O
the	O
following	O
pieces	O
of	O
a	O
tango	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
By	O
keeping	O
the	O
auxiliary	O
trees	O
reasonably	O
balanced	B-Data_Structure
,	O
the	O
height	O
of	O
the	O
auxiliary	O
trees	O
can	O
be	O
bounded	O
to	O
This	O
is	O
the	O
source	O
of	O
the	O
algorithm	O
's	O
performance	O
guarantees	O
.	O
</s>
<s>
First	O
,	O
we	O
define	O
for	O
each	O
node	O
its	O
preferred	O
child	O
,	O
which	O
informally	O
is	O
the	O
most-recently	O
visited	O
child	O
by	O
a	O
traditional	O
binary	B-Language
search	I-Language
tree	I-Language
lookup	O
.	O
</s>
<s>
More	O
formally	O
,	O
consider	O
a	O
subtree	B-Application
T	O
,	O
rooted	O
at	O
p	O
,	O
with	O
children	O
l	O
(	O
left	O
)	O
and	O
r	O
(	O
right	O
)	O
.	O
</s>
<s>
We	O
set	O
r	O
as	O
the	O
preferred	O
child	O
of	O
p	O
if	O
the	O
most	O
recently	O
accessed	O
node	O
in	O
T	O
is	O
in	O
the	O
subtree	B-Application
rooted	O
at	O
r	O
,	O
and	O
l	O
as	O
the	O
preferred	O
child	O
otherwise	O
.	O
</s>
<s>
Removing	O
the	O
nodes	O
on	O
this	O
path	O
partitions	O
the	O
remainder	O
of	O
the	O
tree	O
into	O
a	O
number	O
of	O
subtrees	B-Application
,	O
and	O
we	O
recurse	O
on	O
each	O
subtree	B-Application
(	O
forming	O
a	O
preferred	O
path	O
from	O
its	O
root	O
,	O
which	O
partitions	O
the	O
subtree	B-Application
into	O
more	O
subtrees	B-Application
)	O
.	O
</s>
<s>
To	O
represent	O
a	O
preferred	O
path	O
,	O
we	O
store	O
its	O
nodes	O
in	O
a	O
balanced	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
,	O
specifically	O
a	O
red	O
–	O
black	O
tree	O
.	O
</s>
<s>
We	O
also	O
augment	O
the	O
auxiliary	O
tree	O
by	O
storing	O
at	O
each	O
node	O
the	O
minimum	O
and	O
maximum	O
depth	O
(	O
depth	O
in	O
the	O
reference	O
tree	O
,	O
that	O
is	O
)	O
of	O
nodes	O
in	O
the	O
subtree	B-Application
under	O
that	O
node	O
.	O
</s>
<s>
To	O
search	O
for	O
an	O
element	O
in	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
,	O
we	O
simply	O
simulate	O
searching	O
the	O
reference	O
tree	O
.	O
</s>
<s>
If	O
the	O
auxiliary	O
tree	O
does	O
n't	O
contain	O
the	O
desired	O
element	O
,	O
the	O
search	O
terminates	O
on	O
the	O
parent	O
of	O
the	O
root	O
of	O
the	O
subtree	B-Application
containing	O
the	O
desired	O
element	O
(	O
the	O
beginning	O
of	O
another	O
preferred	O
path	O
)	O
,	O
so	O
we	O
simply	O
proceed	O
by	O
searching	O
the	O
auxiliary	O
tree	O
for	O
that	O
preferred	O
path	O
,	O
and	O
so	O
forth	O
.	O
</s>
<s>
In	O
order	O
to	O
maintain	O
the	O
structure	O
of	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
(	O
auxiliary	O
trees	O
correspond	O
to	O
preferred	O
paths	O
)	O
,	O
we	O
must	O
do	O
some	O
updating	O
work	O
whenever	O
preferred	O
children	O
change	O
as	O
a	O
result	O
of	O
searches	O
.	O
</s>
<s>
In	O
order	O
to	O
bound	O
the	O
competitive	B-Algorithm
ratio	I-Algorithm
for	O
tango	B-Data_Structure
trees	I-Data_Structure
,	O
we	O
must	O
find	O
a	O
lower	O
bound	O
on	O
the	O
performance	O
of	O
the	O
optimal	O
offline	B-Algorithm
tree	O
that	O
we	O
use	O
as	O
a	O
benchmark	O
.	O
</s>
<s>
Once	O
we	O
find	O
an	O
upper	O
bound	O
on	O
the	O
performance	O
of	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
,	O
we	O
can	O
divide	O
them	O
to	O
bound	O
the	O
competitive	B-Algorithm
ratio	I-Algorithm
.	O
</s>
<s>
To	O
find	O
a	O
lower	O
bound	O
on	O
the	O
work	O
done	O
by	O
the	O
optimal	O
offline	B-Algorithm
binary	B-Language
search	I-Language
tree	I-Language
,	O
we	O
again	O
use	O
the	O
notion	O
of	O
preferred	O
children	O
.	O
</s>
<s>
The	O
total	O
number	O
of	O
switches	O
(	O
summed	O
over	O
all	O
nodes	O
)	O
gives	O
an	O
asymptotic	O
lower	O
bound	O
on	O
the	O
work	O
done	O
by	O
any	O
binary	B-Language
search	I-Language
tree	I-Language
algorithm	O
on	O
the	O
given	O
access	O
sequence	O
.	O
</s>
<s>
In	O
order	O
to	O
connect	O
this	O
to	O
tango	B-Data_Structure
trees	I-Data_Structure
,	O
we	O
will	O
find	O
an	O
upper	O
bound	O
on	O
the	O
work	O
done	O
by	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
for	O
a	O
given	O
access	O
sequence	O
.	O
</s>
<s>
The	O
total	O
cost	O
is	O
divided	O
into	O
two	O
parts	O
,	O
searching	O
for	O
the	O
element	O
,	O
and	O
updating	O
the	O
structure	O
of	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
to	O
maintain	O
the	O
proper	O
invariants	O
(	O
switching	O
preferred	O
children	O
and	O
re-arranging	O
preferred	O
paths	O
)	O
.	O
</s>
<s>
Tango	B-Data_Structure
trees	I-Data_Structure
are	O
-competitive	O
,	O
because	O
the	O
work	O
done	O
by	O
the	O
optimal	O
offline	B-Algorithm
binary	B-Language
search	I-Language
tree	I-Language
is	O
at	O
least	O
linear	O
in	O
k	O
(	O
the	O
total	O
number	O
of	O
preferred	O
child	O
switches	O
)	O
,	O
and	O
the	O
work	O
done	O
by	O
the	O
tango	B-Data_Structure
tree	I-Data_Structure
is	O
at	O
most	O
.	O
</s>
