<s>
In	O
computer	B-General_Concept
science	I-General_Concept
and	O
information	O
theory	O
,	O
a	O
canonical	B-Algorithm
Huffman	I-Algorithm
code	I-Algorithm
is	O
a	O
particular	O
type	O
of	O
Huffman	B-General_Concept
code	I-General_Concept
with	O
unique	O
properties	O
which	O
allow	O
it	O
to	O
be	O
described	O
in	O
a	O
very	O
compact	O
manner	O
.	O
</s>
<s>
Rather	O
than	O
storing	O
the	O
structure	O
of	O
the	O
code	O
tree	O
explicitly	O
,	O
canonical	B-Algorithm
Huffman	I-Algorithm
codes	I-Algorithm
are	O
ordered	O
in	O
such	O
a	O
way	O
that	O
it	O
suffices	O
to	O
only	O
store	O
the	O
lengths	O
of	O
the	O
codewords	O
,	O
which	O
reduces	O
the	O
overhead	O
of	O
the	O
codebook	B-Algorithm
.	O
</s>
<s>
Either	O
the	O
decompressor	O
can	O
infer	O
what	O
codebook	B-Algorithm
the	O
compressor	O
has	O
used	O
from	O
previous	O
context	O
,	O
or	O
the	O
compressor	O
must	O
tell	O
the	O
decompressor	O
what	O
the	O
codebook	B-Algorithm
is	O
.	O
</s>
<s>
Since	O
a	O
canonical	O
Huffman	O
codebook	B-Algorithm
can	O
be	O
stored	O
especially	O
efficiently	O
,	O
most	O
compressors	O
start	O
by	O
generating	O
a	O
"	O
normal	O
"	O
Huffman	O
codebook	B-Algorithm
,	O
and	O
then	O
convert	O
it	O
to	O
canonical	O
Huffman	O
before	O
using	O
it	O
.	O
</s>
<s>
In	O
order	O
for	O
a	O
symbol	O
code	O
scheme	O
such	O
as	O
the	O
Huffman	B-General_Concept
code	I-General_Concept
to	O
be	O
decompressed	O
,	O
the	O
same	O
model	O
that	O
the	O
encoding	O
algorithm	O
used	O
to	O
compress	O
the	O
source	O
data	O
must	O
be	O
provided	O
to	O
the	O
decoding	O
algorithm	O
so	O
that	O
it	O
can	O
use	O
it	O
to	O
decompress	O
the	O
encoded	O
data	O
.	O
</s>
<s>
In	O
standard	O
Huffman	B-General_Concept
coding	I-General_Concept
this	O
model	O
takes	O
the	O
form	O
of	O
a	O
tree	O
of	O
variable-length	O
codes	O
,	O
with	O
the	O
most	O
frequent	O
symbols	O
located	O
at	O
the	O
top	O
of	O
the	O
structure	O
and	O
being	O
represented	O
by	O
the	O
fewest	O
bits	O
.	O
</s>
<s>
Canonical	B-Algorithm
Huffman	I-Algorithm
codes	I-Algorithm
address	O
these	O
two	O
issues	O
by	O
generating	O
the	O
codes	O
in	O
a	O
clear	O
standardized	O
format	O
;	O
all	O
the	O
codes	O
for	O
a	O
given	O
length	O
are	O
assigned	O
their	O
values	O
sequentially	O
.	O
</s>
<s>
The	O
normal	O
Huffman	B-General_Concept
coding	I-General_Concept
algorithm	O
assigns	O
a	O
variable	O
length	O
code	O
to	O
every	O
symbol	O
in	O
the	O
alphabet	O
.	O
</s>
<s>
For	O
example	O
,	O
suppose	O
we	O
have	O
the	O
following	O
non-canonical	O
codebook	B-Algorithm
:	O
</s>
<s>
To	O
make	O
the	O
code	O
a	O
canonical	B-Algorithm
Huffman	I-Algorithm
code	I-Algorithm
,	O
the	O
codes	O
are	O
renumbered	O
.	O
</s>
<s>
The	O
bit	B-Algorithm
lengths	I-Algorithm
stay	O
the	O
same	O
with	O
the	O
code	O
book	O
being	O
sorted	O
first	O
by	O
codeword	O
length	O
and	O
secondly	O
by	O
alphabetical	O
value	O
of	O
the	O
letter	O
:	O
</s>
<s>
Another	O
perspective	O
on	O
the	O
canonical	O
codewords	O
is	O
that	O
they	O
are	O
the	O
digits	O
past	O
the	O
radix	B-Algorithm
point	I-Algorithm
(	O
binary	O
decimal	O
point	O
)	O
in	O
a	O
binary	O
representation	O
of	O
a	O
certain	O
series	O
.	O
</s>
<s>
The	O
advantage	O
of	O
a	O
canonical	O
Huffman	B-General_Concept
tree	I-General_Concept
is	O
that	O
it	O
can	O
be	O
encoded	O
in	O
fewer	O
bits	O
than	O
an	O
arbitrary	O
tree	O
.	O
</s>
<s>
Let	O
us	O
take	O
our	O
original	O
Huffman	O
codebook	B-Algorithm
:	O
</s>
<s>
There	O
are	O
several	O
ways	O
we	O
could	O
encode	O
this	O
Huffman	B-General_Concept
tree	I-General_Concept
.	O
</s>
<s>
The	O
only	O
parts	O
left	O
to	O
transmit	O
are	O
the	O
bit-lengths	B-Algorithm
(	O
number	O
of	O
bits	O
)	O
for	O
each	O
symbol	O
.	O
</s>
<s>
Note	O
that	O
our	O
canonical	O
Huffman	B-General_Concept
tree	I-General_Concept
always	O
has	O
higher	O
values	O
for	O
longer	O
bit	B-Algorithm
lengths	I-Algorithm
and	O
that	O
any	O
symbols	O
of	O
the	O
same	O
bit	B-Algorithm
length	I-Algorithm
(	O
C	O
and	O
D	O
)	O
have	O
higher	O
code	O
values	O
for	O
higher	O
symbols	O
:	O
</s>
<s>
With	O
knowledge	O
of	O
the	O
canonical	O
Huffman	B-General_Concept
algorithm	I-General_Concept
,	O
it	O
is	O
then	O
possible	O
to	O
recreate	O
the	O
entire	O
table	O
(	O
symbol	O
and	O
code	O
values	O
)	O
from	O
just	O
the	O
bit-lengths	B-Algorithm
.	O
</s>
<s>
Unused	O
symbols	O
are	O
normally	O
transmitted	O
as	O
having	O
zero	O
bit	B-Algorithm
length	I-Algorithm
.	O
</s>
<s>
Another	O
efficient	O
way	O
representing	O
the	O
codebook	B-Algorithm
is	O
to	O
list	O
all	O
symbols	O
in	O
increasing	O
order	O
by	O
their	O
bit-lengths	B-Algorithm
,	O
and	O
record	O
the	O
number	O
of	O
symbols	O
for	O
each	O
bit-length	B-Algorithm
.	O
</s>
<s>
Since	O
the	O
symbols	O
are	O
sorted	O
by	O
bit-length	B-Algorithm
,	O
we	O
can	O
efficiently	O
reconstruct	O
the	O
codebook	B-Algorithm
.	O
</s>
<s>
A	O
pseudo	B-Language
code	I-Language
describing	O
the	O
reconstruction	O
is	O
introduced	O
on	O
the	O
next	O
section	O
.	O
</s>
<s>
For	O
example	O
,	O
suppose	O
the	O
codebook	B-Algorithm
contains	O
only	O
4	O
letters	O
C	O
,	O
O	O
,	O
D	O
and	O
E	O
,	O
each	O
of	O
length	O
2	O
.	O
</s>
<s>
The	O
JPEG	O
File	O
Interchange	O
Format	O
uses	O
this	O
method	O
of	O
encoding	O
,	O
because	O
at	O
most	O
only	O
162	O
symbols	O
out	O
of	O
the	O
8-bit	O
alphabet	O
,	O
which	O
has	O
size	O
256	O
,	O
will	O
be	O
in	O
the	O
codebook	B-Algorithm
.	O
</s>
<s>
Given	O
a	O
list	O
of	O
symbols	O
sorted	O
by	O
bit-length	B-Algorithm
,	O
the	O
following	O
pseudocode	B-Language
will	O
print	O
a	O
canonical	B-Algorithm
Huffman	I-Algorithm
code	I-Algorithm
book	O
:	O
</s>
