<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
(	O
BST	O
)	O
,	O
also	O
called	O
an	O
ordered	O
or	O
sorted	O
binary	O
tree	O
,	O
is	O
a	O
rooted	O
binary	O
tree	B-Application
data	I-Application
structure	I-Application
with	O
the	O
key	O
of	O
each	O
internal	O
node	O
being	O
greater	O
than	O
all	O
the	O
keys	O
in	O
the	O
respective	O
node	O
's	O
left	O
subtree	B-Application
and	O
less	O
than	O
the	O
ones	O
in	O
its	O
right	O
subtree	B-Application
.	O
</s>
<s>
The	O
time	O
complexity	O
of	O
operations	O
on	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
is	O
directly	O
proportional	O
to	O
the	O
height	O
of	O
the	O
tree	O
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
allow	O
binary	O
search	O
for	O
fast	O
lookup	B-Data_Structure
,	O
addition	O
,	O
and	O
removal	O
of	O
data	O
items	O
.	O
</s>
<s>
Since	O
the	O
nodes	O
in	O
a	O
BST	O
are	O
laid	O
out	O
so	O
that	O
each	O
comparison	O
skips	O
about	O
half	O
of	O
the	O
remaining	O
tree	O
,	O
the	O
lookup	B-Data_Structure
performance	O
is	O
proportional	O
to	O
that	O
of	O
binary	O
logarithm	O
.	O
</s>
<s>
BSTs	O
were	O
devised	O
in	O
the	O
1960s	O
for	O
the	O
problem	O
of	O
efficient	O
storage	O
of	O
labeled	B-General_Concept
data	I-General_Concept
and	O
are	O
attributed	O
to	O
Conway	O
Berners-Lee	O
and	O
David	O
Wheeler	O
.	O
</s>
<s>
The	O
performance	O
of	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
is	O
dependent	O
on	O
the	O
order	O
of	O
insertion	O
of	O
the	O
nodes	O
into	O
the	O
tree	O
since	O
arbitrary	O
insertions	O
may	O
lead	O
to	O
degeneracy	O
;	O
several	O
variations	O
of	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
can	O
be	O
built	O
with	O
guaranteed	O
worst-case	B-General_Concept
performance	I-General_Concept
.	O
</s>
<s>
BSTs	O
with	O
guaranteed	O
worst-case	B-General_Concept
complexities	O
perform	O
better	O
than	O
an	O
unsorted	O
array	O
,	O
which	O
would	O
require	O
linear	O
search	O
time	O
.	O
</s>
<s>
The	O
complexity	O
analysis	O
of	O
BST	O
shows	O
that	O
,	O
on	B-General_Concept
average	I-General_Concept
,	O
the	O
insert	O
,	O
delete	O
and	O
search	O
takes	O
for	O
nodes	O
.	O
</s>
<s>
In	O
the	O
worst	B-General_Concept
case	I-General_Concept
,	O
they	O
degrade	O
to	O
that	O
of	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
:	O
.	O
</s>
<s>
To	O
address	O
the	O
boundless	O
increase	O
of	O
the	O
tree	O
height	O
with	O
arbitrary	O
insertions	O
and	O
deletions	O
,	O
self-balancing	O
variants	O
of	O
BSTs	O
are	O
introduced	O
to	O
bound	O
the	O
worst	O
lookup	B-Data_Structure
complexity	O
to	O
that	O
of	O
the	O
binary	O
logarithm	O
.	O
</s>
<s>
AVL	O
trees	O
were	O
the	O
first	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
,	O
invented	O
in	O
1962	O
by	O
Georgy	O
Adelson-Velsky	O
and	O
Evgenii	O
Landis	O
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
can	O
be	O
used	O
to	O
implement	O
abstract	O
data	O
types	O
such	O
as	O
dynamic	O
sets	O
,	O
lookup	B-Data_Structure
tables	I-Data_Structure
and	O
priority	B-Application
queues	I-Application
,	O
and	O
used	O
in	O
sorting	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
tree	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
binary	B-Language
search	I-Language
tree	I-Language
algorithm	O
was	O
discovered	O
independently	O
by	O
several	O
researchers	O
,	O
including	O
P.F.	O
</s>
<s>
The	O
algorithm	O
is	O
attributed	O
to	O
Conway	O
Berners-Lee	O
and	O
David	O
Wheeler	O
,	O
who	O
used	O
it	O
for	O
storing	O
labeled	B-General_Concept
data	I-General_Concept
in	O
magnetic	O
tapes	O
in	O
1960	O
.	O
</s>
<s>
One	O
of	O
the	O
earliest	O
and	O
popular	O
binary	B-Language
search	I-Language
tree	I-Language
algorithm	O
is	O
that	O
of	O
Hibbard	O
.	O
</s>
<s>
The	O
time	O
complexities	O
of	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
increases	O
boundlessly	O
with	O
the	O
tree	O
height	O
if	O
the	O
nodes	O
are	O
inserted	O
in	O
an	O
arbitrary	O
order	O
,	O
therefore	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
were	O
introduced	O
to	O
bound	O
the	O
height	O
of	O
the	O
tree	O
to	O
.	O
</s>
<s>
Various	O
height-balanced	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
were	O
introduced	O
to	O
confine	O
the	O
tree	O
height	O
,	O
such	O
as	O
AVL	O
trees	O
,	O
Treaps	B-Application
,	O
and	O
red	O
–	O
black	O
trees	O
.	O
</s>
<s>
It	O
was	O
the	O
first	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
to	O
be	O
invented	O
.	O
</s>
<s>
A	O
binary	B-Language
search	I-Language
tree	I-Language
is	O
a	O
rooted	O
binary	O
tree	O
in	O
which	O
the	O
nodes	O
are	O
arranged	O
in	O
strict	O
total	O
order	O
in	O
which	O
the	O
nodes	O
with	O
keys	O
greater	O
than	O
any	O
particular	O
node	O
is	O
stored	O
on	O
the	O
right	O
sub-trees	O
and	O
the	O
ones	O
with	O
equal	O
to	O
or	O
less	O
than	O
are	O
stored	O
on	O
the	O
left	O
sub-tree	O
satisfying	O
the	O
binary	O
search	O
property	O
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
are	O
also	O
efficacious	O
in	O
sortings	B-Algorithm
and	O
search	B-Application
algorithms	I-Application
.	O
</s>
<s>
However	O
,	O
the	O
search	O
complexity	O
of	O
a	O
BST	O
depends	O
upon	O
the	O
order	O
in	O
which	O
the	O
nodes	O
are	O
inserted	O
and	O
deleted	O
;	O
since	O
in	O
worst	B-General_Concept
case	I-General_Concept
,	O
successive	O
operations	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
may	O
lead	O
to	O
degeneracy	O
and	O
form	O
a	O
singly	O
linked	B-Data_Structure
list	I-Data_Structure
(	O
or	O
"	O
unbalanced	O
tree	O
"	O
)	O
like	O
structure	O
,	O
thus	O
has	O
the	O
same	O
worst-case	B-General_Concept
complexity	O
as	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
are	O
also	O
a	O
fundamental	O
data	B-General_Concept
structure	I-General_Concept
used	O
in	O
construction	O
of	O
abstract	O
data	B-General_Concept
structures	I-General_Concept
such	O
as	O
sets	O
,	O
multisets	O
,	O
and	O
associative	B-Application
arrays	I-Application
.	O
</s>
<s>
Searching	O
in	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
for	O
a	O
specific	O
key	O
can	O
be	O
programmed	O
recursively	O
or	O
iteratively	O
.	O
</s>
<s>
Searching	O
begins	O
by	O
examining	O
the	O
root	B-Application
node	I-Application
.	O
</s>
<s>
If	O
the	O
key	O
is	O
less	O
than	O
that	O
of	O
the	O
root	O
,	O
the	O
search	O
proceeds	O
by	O
examining	O
the	O
left	O
subtree	B-Application
.	O
</s>
<s>
Similarly	O
,	O
if	O
the	O
key	O
is	O
greater	O
than	O
that	O
of	O
the	O
root	O
,	O
the	O
search	O
proceeds	O
by	O
examining	O
the	O
right	O
subtree	B-Application
.	O
</s>
<s>
This	O
process	O
is	O
repeated	O
until	O
the	O
key	O
is	O
found	O
or	O
the	O
remaining	O
subtree	B-Application
is	O
.	O
</s>
<s>
If	O
the	O
searched	O
key	O
is	O
not	O
found	O
after	O
a	O
subtree	B-Application
is	O
reached	O
,	O
then	O
the	O
key	O
is	O
not	O
present	O
in	O
the	O
tree	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
implements	O
the	O
BST	O
search	O
procedure	O
through	O
recursion	O
.	O
</s>
<s>
Since	O
the	O
search	O
may	O
proceed	O
till	O
some	O
leaf	B-Data_Structure
node	I-Data_Structure
,	O
the	O
running	O
time	O
complexity	O
of	O
BST	O
search	O
is	O
where	O
is	O
the	O
height	O
of	O
the	O
tree	O
.	O
</s>
<s>
However	O
,	O
the	O
worst	B-General_Concept
case	I-General_Concept
for	O
BST	O
search	O
is	O
where	O
is	O
the	O
total	O
number	O
of	O
nodes	O
in	O
the	O
BST	O
,	O
because	O
an	O
unbalanced	O
BST	O
may	O
degenerate	O
to	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
.	O
</s>
<s>
However	O
,	O
if	O
the	O
BST	O
is	O
height-balanced	B-Data_Structure
the	O
height	O
is	O
.	O
</s>
<s>
Following	O
is	O
pseudocode	B-Language
for	O
finding	O
the	O
successor	O
and	O
predecessor	O
of	O
a	O
node	O
in	O
BST	O
.	O
</s>
<s>
Following	O
is	O
the	O
pseudocode	B-Language
for	O
the	O
operations	O
.	O
</s>
<s>
The	O
data	B-General_Concept
structure	I-General_Concept
must	O
be	O
modified	O
in	O
such	O
a	O
way	O
that	O
the	O
properties	O
of	O
BST	O
continue	O
to	O
hold	O
.	O
</s>
<s>
New	O
nodes	O
are	O
inserted	O
as	O
leaf	B-Application
nodes	I-Application
in	O
the	O
BST	O
.	O
</s>
<s>
If	O
is	O
,	O
the	O
BST	O
is	O
empty	O
,	O
thus	O
is	O
inserted	O
as	O
the	O
root	B-Application
node	I-Application
of	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
if	O
it	O
is	O
not	O
,	O
insertion	O
proceeds	O
by	O
comparing	O
the	O
keys	O
to	O
that	O
of	O
on	O
the	O
lines	O
15-19	O
and	O
the	O
node	O
is	O
inserted	O
accordingly	O
.	O
</s>
<s>
Deletion	O
of	O
a	O
node	O
,	O
say	O
,	O
from	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
should	O
abide	O
three	O
cases	O
:	O
</s>
<s>
If	O
is	O
a	O
leaf	B-Data_Structure
node	I-Data_Structure
,	O
the	O
parent	B-Application
node	I-Application
′s	O
pointer	O
to	O
gets	O
replaced	O
with	O
and	O
consequently	O
gets	O
removed	O
from	O
the	O
tree	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
implements	O
the	O
deletion	O
operation	O
in	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
The	O
helper	O
function	O
is	O
used	O
within	O
the	O
deletion	O
algorithm	O
for	O
the	O
purpose	O
of	O
replacing	O
the	O
node	O
with	O
in	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
A	O
BST	O
can	O
be	O
traversed	B-Algorithm
through	O
three	O
basic	O
algorithms	O
:	O
inorder	B-Algorithm
,	O
preorder	O
,	O
and	O
postorder	B-Algorithm
tree	I-Algorithm
walks	I-Algorithm
.	O
</s>
<s>
Inorder	B-Algorithm
tree	I-Algorithm
walk	I-Algorithm
:	O
Nodes	O
from	O
the	O
left	O
subtree	B-Application
get	O
visited	O
first	O
,	O
followed	O
by	O
the	O
root	B-Application
node	I-Application
and	O
right	O
subtree	B-Application
.	O
</s>
<s>
Preorder	B-Algorithm
tree	I-Algorithm
walk	I-Algorithm
:	O
The	O
root	B-Application
node	I-Application
gets	O
visited	O
first	O
,	O
followed	O
by	O
left	O
and	O
right	O
subtrees	B-Application
.	O
</s>
<s>
Postorder	B-Algorithm
tree	I-Algorithm
walk	I-Algorithm
:	O
Nodes	O
from	O
the	O
left	O
subtree	B-Application
get	O
visited	O
first	O
,	O
followed	O
by	O
the	O
right	O
subtree	B-Application
,	O
and	O
finally	O
the	O
root	O
.	O
</s>
<s>
Following	O
is	O
a	O
recursive	O
implementation	O
of	O
the	O
tree	B-Algorithm
walks	I-Algorithm
.	O
</s>
<s>
Without	O
rebalancing	O
,	O
insertions	O
or	O
deletions	O
in	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
may	O
lead	O
to	O
degeneration	O
,	O
resulting	O
in	O
a	O
height	O
of	O
the	O
tree	O
(	O
where	O
is	O
number	O
of	O
items	O
in	O
a	O
tree	O
)	O
,	O
so	O
that	O
the	O
lookup	B-Data_Structure
performance	O
is	O
deteriorated	O
to	O
that	O
of	O
a	O
linear	O
search	O
.	O
</s>
<s>
Keeping	O
the	O
search	B-Data_Structure
tree	I-Data_Structure
balanced	O
and	O
height	O
bounded	O
by	O
is	O
a	O
key	O
to	O
the	O
usefulness	O
of	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
.	O
</s>
<s>
A	O
tree	O
is	O
height-balanced	B-Data_Structure
if	O
the	O
heights	O
of	O
the	O
left	O
sub-tree	O
and	O
right	O
sub-tree	O
are	O
guaranteed	O
to	O
be	O
related	O
by	O
a	O
constant	O
factor	O
.	O
</s>
<s>
The	O
heights	O
of	O
all	O
the	O
nodes	O
on	O
the	O
path	O
from	O
the	O
root	O
to	O
the	O
modified	O
leaf	B-Data_Structure
node	I-Data_Structure
have	O
to	O
be	O
observed	O
and	O
possibly	O
corrected	O
on	O
every	O
insert	O
and	O
delete	O
operation	O
to	O
the	O
tree	O
.	O
</s>
<s>
In	O
a	O
weight-balanced	O
tree	O
,	O
the	O
criterion	O
of	O
a	O
balanced	B-Data_Structure
tree	I-Data_Structure
is	O
the	O
number	O
of	O
leaves	O
of	O
the	O
subtrees	B-Application
.	O
</s>
<s>
The	O
weights	O
of	O
the	O
left	O
and	O
right	O
subtrees	B-Application
differ	O
at	O
most	O
by	O
.	O
</s>
<s>
The	O
-weight-balanced	O
trees	O
gives	O
an	O
entire	O
family	O
of	O
balance	O
conditions	O
,	O
where	O
each	O
left	O
and	O
right	O
subtrees	B-Application
have	O
each	O
at	O
least	O
a	O
fraction	O
of	O
of	O
the	O
total	O
weight	O
of	O
the	O
subtree	B-Application
.	O
</s>
<s>
There	O
are	O
several	O
self-balanced	O
binary	O
search	O
trees	O
,	O
including	O
T-tree	B-Data_Structure
,	O
treap	B-Application
,	O
red-black	O
tree	O
,	O
B-tree	B-Architecture
,	O
2	B-Data_Structure
–	I-Data_Structure
3	I-Data_Structure
tree	I-Data_Structure
,	O
and	O
Splay	B-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
are	O
used	O
in	O
sorting	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
tree	B-Algorithm
sort	I-Algorithm
,	O
where	O
all	O
the	O
elements	O
are	O
inserted	O
at	O
once	O
and	O
the	O
tree	O
is	O
traversed	B-Algorithm
at	O
an	O
in-order	O
fashion	O
.	O
</s>
<s>
BSTs	O
are	O
also	O
used	O
in	O
quicksort	B-Algorithm
.	O
</s>
<s>
Binary	B-Language
search	I-Language
trees	I-Language
are	O
used	O
in	O
implementing	O
priority	B-Application
queues	I-Application
,	O
using	O
the	O
node	O
's	O
key	O
as	O
priorities	O
.	O
</s>
<s>
Adding	O
new	O
elements	O
to	O
the	O
queue	O
follows	O
the	O
regular	O
BST	O
insertion	O
operation	O
but	O
the	O
removal	O
operation	O
depends	O
on	O
the	O
type	O
of	O
priority	B-Application
queue	I-Application
:	O
</s>
<s>
If	O
it	O
is	O
an	O
ascending	O
order	O
priority	B-Application
queue	I-Application
,	O
removal	O
of	O
an	O
element	O
with	O
the	O
lowest	O
priority	O
is	O
done	O
through	O
leftward	O
traversal	O
of	O
the	O
BST	O
.	O
</s>
<s>
If	O
it	O
is	O
a	O
descending	O
order	O
priority	B-Application
queue	I-Application
,	O
removal	O
of	O
an	O
element	O
with	O
the	O
highest	O
priority	O
is	O
done	O
through	O
rightward	O
traversal	O
of	O
the	O
BST	O
.	O
</s>
