<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
(	O
BST	O
)	O
is	O
any	O
node-based	O
binary	B-Language
search	I-Language
tree	I-Language
that	O
automatically	O
keeps	O
its	O
height	O
(	O
maximal	O
number	O
of	O
levels	O
below	O
the	O
root	O
)	O
small	O
in	O
the	O
face	O
of	O
arbitrary	O
item	O
insertions	O
and	O
deletions	O
.	O
</s>
<s>
These	O
operations	O
when	O
designed	O
for	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
,	O
contain	O
precautionary	O
measures	O
against	O
boundlessly	O
increasing	O
tree	O
height	O
,	O
so	O
that	O
these	O
abstract	O
data	O
structures	O
receive	O
the	O
attribute	O
"	O
self-balancing	O
"	O
.	O
</s>
<s>
For	O
height-balanced	B-Data_Structure
binary	I-Data_Structure
trees	I-Data_Structure
,	O
the	O
height	O
is	O
defined	O
to	O
be	O
logarithmic	O
in	O
the	O
number	O
of	O
items	O
.	O
</s>
<s>
This	O
is	O
the	O
case	O
for	O
many	O
binary	B-Language
search	I-Language
trees	I-Language
,	O
such	O
as	O
AVL	O
trees	O
and	O
red	O
–	O
black	O
trees	O
.	O
</s>
<s>
Splay	B-Data_Structure
trees	I-Data_Structure
and	O
treaps	B-Application
are	O
self-balancing	O
but	O
not	O
height-balanced	O
,	O
as	O
their	O
height	O
is	O
not	O
guaranteed	O
to	O
be	O
logarithmic	O
in	O
the	O
number	O
of	O
items	O
.	O
</s>
<s>
Self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
provide	O
efficient	O
implementations	O
for	O
mutable	O
ordered	O
lists	O
,	O
and	O
can	O
be	O
used	O
for	O
other	O
abstract	O
data	O
structures	O
such	O
as	O
associative	B-Application
arrays	I-Application
,	O
priority	B-Application
queues	I-Application
and	O
sets	O
.	O
</s>
<s>
Most	O
operations	O
on	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
(	O
BST	O
)	O
take	O
time	O
directly	O
proportional	O
to	O
the	O
height	O
of	O
the	O
tree	O
,	O
so	O
it	O
is	O
desirable	O
to	O
keep	O
the	O
height	O
small	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
the	O
items	O
are	O
inserted	O
in	O
sorted	O
key	B-Application
order	O
,	O
the	O
tree	O
degenerates	O
into	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
with	O
n	O
nodes	O
.	O
</s>
<s>
If	O
the	O
data	O
items	O
are	O
known	O
ahead	O
of	O
time	O
,	O
the	O
height	O
can	O
be	O
kept	O
small	O
,	O
in	O
the	O
average	O
sense	O
,	O
by	O
adding	O
values	O
in	O
a	O
random	O
order	O
,	O
resulting	O
in	O
a	O
random	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
However	O
,	O
there	O
are	O
many	O
situations	O
(	O
such	O
as	O
online	B-Algorithm
algorithms	I-Algorithm
)	O
where	O
this	O
randomization	B-General_Concept
is	O
not	O
viable	O
.	O
</s>
<s>
Self-balancing	B-Data_Structure
binary	I-Data_Structure
trees	I-Data_Structure
solve	O
this	O
problem	O
by	O
performing	O
transformations	O
on	O
the	O
tree	O
(	O
such	O
as	O
tree	B-Data_Structure
rotations	I-Data_Structure
)	O
at	O
key	B-Application
insertion	O
times	O
,	O
in	O
order	O
to	O
keep	O
the	O
height	O
proportional	O
to	O
Although	O
a	O
certain	O
overhead	O
is	O
involved	O
,	O
it	O
is	O
not	O
bigger	O
than	O
the	O
always	O
necessary	O
lookup	O
cost	O
and	O
may	O
be	O
justified	O
by	O
ensuring	O
fast	O
execution	O
of	O
all	O
operations	O
.	O
</s>
<s>
In	O
the	O
asymptotic	O
(	O
"	O
Big-O	O
"	O
)	O
sense	O
,	O
a	O
self-balancing	O
BST	O
structure	O
containing	O
n	O
items	O
allows	O
the	O
lookup	O
,	O
insertion	O
,	O
and	O
removal	O
of	O
an	O
item	O
in	O
worst-case	O
time	O
,	O
and	O
ordered	B-Algorithm
enumeration	I-Algorithm
of	O
all	O
items	O
in	O
time	O
.	O
</s>
<s>
For	O
some	O
implementations	O
these	O
are	O
per-operation	O
time	O
bounds	O
,	O
while	O
for	O
others	O
they	O
are	O
amortized	B-General_Concept
bounds	O
over	O
a	O
sequence	O
of	O
operations	O
.	O
</s>
<s>
These	O
times	O
are	O
asymptotically	B-General_Concept
optimal	I-General_Concept
among	O
all	O
data	O
structures	O
that	O
manipulate	O
the	O
key	B-Application
only	O
through	O
comparisons	O
.	O
</s>
<s>
Self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
can	O
be	O
used	O
in	O
a	O
natural	O
way	O
to	O
construct	O
and	O
maintain	O
ordered	O
lists	O
,	O
such	O
as	O
priority	B-Application
queues	I-Application
.	O
</s>
<s>
They	O
can	O
also	O
be	O
used	O
for	O
associative	B-Application
arrays	I-Application
;	O
key-value	O
pairs	O
are	O
simply	O
inserted	O
with	O
an	O
ordering	O
based	O
on	O
the	O
key	B-Application
alone	O
.	O
</s>
<s>
In	O
this	O
capacity	O
,	O
self-balancing	O
BSTs	O
have	O
a	O
number	O
of	O
advantages	O
and	O
disadvantages	O
over	O
their	O
main	O
competitor	O
,	O
hash	B-Algorithm
tables	I-Algorithm
.	O
</s>
<s>
One	O
advantage	O
of	O
self-balancing	O
BSTs	O
is	O
that	O
they	O
allow	O
fast	O
(	O
indeed	O
,	O
asymptotically	B-General_Concept
optimal	I-General_Concept
)	O
enumeration	O
of	O
the	O
items	O
in	O
key	B-Application
order	O
,	O
which	O
hash	B-Algorithm
tables	I-Algorithm
do	O
not	O
provide	O
.	O
</s>
<s>
One	O
disadvantage	O
is	O
that	O
their	O
lookup	O
algorithms	O
get	O
more	O
complicated	O
when	O
there	O
may	O
be	O
multiple	O
items	O
with	O
the	O
same	O
key	B-Application
.	O
</s>
<s>
Self-balancing	O
BSTs	O
have	O
better	O
worst-case	O
lookup	O
performance	O
than	O
most	O
hash	B-Algorithm
tables	I-Algorithm
(	O
compared	O
to	O
)	O
,	O
but	O
have	O
worse	O
average-case	O
performance	O
(	O
compared	O
to	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
binary	B-Algorithm
tree	I-Algorithm
sort	I-Algorithm
is	O
implemented	O
with	O
a	O
self-balancing	O
BST	O
,	O
we	O
have	O
a	O
very	O
simple-to-describe	O
yet	O
asymptotically	B-General_Concept
optimal	I-General_Concept
sorting	B-Algorithm
algorithm	O
.	O
</s>
<s>
Similarly	O
,	O
many	O
algorithms	O
in	O
computational	O
geometry	O
exploit	O
variations	O
on	O
self-balancing	O
BSTs	O
to	O
solve	O
problems	O
such	O
as	O
the	O
line	O
segment	O
intersection	O
problem	O
and	O
the	O
point	B-Data_Structure
location	I-Data_Structure
problem	I-Data_Structure
efficiently	O
.	O
</s>
<s>
Binary	B-Algorithm
tree	I-Algorithm
sort	I-Algorithm
,	O
in	O
particular	O
,	O
is	O
likely	O
to	O
be	O
slower	O
than	O
merge	B-Algorithm
sort	I-Algorithm
,	O
quicksort	B-Algorithm
,	O
or	O
heapsort	B-Application
,	O
because	O
of	O
the	O
tree-balancing	O
overhead	O
as	O
well	O
as	O
cache	B-General_Concept
access	O
patterns	O
.	O
)	O
</s>
<s>
For	O
example	O
,	O
one	O
can	O
record	O
the	O
number	O
of	O
nodes	O
in	O
each	O
subtree	O
having	O
a	O
certain	O
property	O
,	O
allowing	O
one	O
to	O
count	O
the	O
number	O
of	O
nodes	O
in	O
a	O
certain	O
key	B-Application
range	O
with	O
that	O
property	O
in	O
time	O
.	O
</s>
