<s>
The	O
Garsia	B-Data_Structure
–	I-Data_Structure
Wachs	I-Data_Structure
algorithm	I-Data_Structure
is	O
an	O
efficient	O
method	O
for	O
computers	O
to	O
construct	O
optimal	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
trees	I-Data_Structure
and	O
alphabetic	B-General_Concept
Huffman	I-General_Concept
codes	I-General_Concept
,	O
in	O
linearithmic	O
time	O
.	O
</s>
<s>
Alternatively	O
,	O
the	O
output	O
of	O
the	O
problem	O
can	O
be	O
used	O
as	O
a	O
Huffman	B-General_Concept
code	I-General_Concept
,	O
a	O
method	O
for	O
encoding	O
given	O
values	O
unambiguously	O
by	O
using	O
variable-length	O
sequences	O
of	O
binary	O
values	O
.	O
</s>
<s>
Unlike	O
standard	O
Huffman	B-General_Concept
codes	I-General_Concept
,	O
the	O
ones	O
constructed	O
in	O
this	O
way	O
are	O
alphabetical	O
,	O
meaning	O
that	O
the	O
sorted	O
order	O
of	O
these	O
binary	O
codes	O
is	O
the	O
same	O
as	O
the	O
input	O
ordering	O
of	O
the	O
values	O
.	O
</s>
<s>
If	O
the	O
weight	O
of	O
a	O
value	O
is	O
its	O
frequency	O
in	O
a	O
message	O
to	O
be	O
encoded	O
,	O
then	O
the	O
output	O
of	O
the	O
Garsia	B-Data_Structure
–	I-Data_Structure
Wachs	I-Data_Structure
algorithm	I-Data_Structure
is	O
the	O
alphabetical	O
Huffman	B-General_Concept
code	I-General_Concept
that	O
compresses	B-General_Concept
the	O
message	O
to	O
the	O
shortest	O
possible	O
length	O
.	O
</s>
<s>
The	O
first	O
phase	O
of	O
the	O
algorithm	O
is	O
easier	O
to	O
describe	O
if	O
the	O
input	O
is	O
augmented	O
with	O
two	O
sentinel	B-Data_Structure
values	I-Data_Structure
,	O
(	O
or	O
any	O
sufficiently	O
large	O
finite	O
value	O
)	O
at	O
the	O
start	O
and	O
end	O
of	O
the	O
sequence	O
.	O
</s>
<s>
The	O
algorithm	O
maintains	O
a	O
sequence	O
of	O
these	O
values	O
,	O
with	O
the	O
two	O
sentinel	B-Data_Structure
values	I-Data_Structure
at	O
each	O
end	O
.	O
</s>
<s>
There	O
always	O
exists	O
such	O
a	O
triple	O
,	O
because	O
the	O
final	O
sentinel	B-Data_Structure
value	I-Data_Structure
is	O
larger	O
than	O
any	O
previous	O
two	O
finite	O
values	O
.	O
</s>
<s>
There	O
always	O
exists	O
such	O
a	O
position	O
,	O
because	O
of	O
the	O
left	O
sentinel	B-Data_Structure
value	I-Data_Structure
.	O
</s>
<s>
To	O
implement	O
this	O
phase	O
efficiently	O
,	O
the	O
algorithm	O
can	O
maintain	O
its	O
current	O
sequence	O
of	O
values	O
in	O
any	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
structure	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
position	O
to	O
reinsert	O
may	O
be	O
found	O
in	O
logarithmic	O
time	O
by	O
using	O
the	O
balanced	B-Data_Structure
tree	I-Data_Structure
to	O
perform	O
two	O
binary	O
searches	O
,	O
one	O
for	O
each	O
of	O
these	O
two	O
decreasing	O
sequences	O
.	O
</s>
<s>
The	O
search	O
for	O
the	O
first	O
position	O
for	O
which	O
can	O
be	O
performed	O
in	O
linear	O
total	O
time	O
by	O
using	O
a	O
sequential	B-Algorithm
search	I-Algorithm
that	O
begins	O
at	O
the	O
from	O
the	O
previous	O
triple	O
.	O
</s>
<s>
The	O
Garsia	B-Data_Structure
–	I-Data_Structure
Wachs	I-Data_Structure
algorithm	I-Data_Structure
is	O
named	O
after	O
Adriano	O
Garsia	O
and	O
Michelle	O
L	O
.	O
Wachs	O
,	O
who	O
published	O
it	O
in	O
1977	O
.	O
</s>
