- Find the frequency of each character
abracadabra
a:5
b:2
c:1
d:1
r:2
|
- For each character,
form a binary tree that consists of
one singleton node:
(I ordered the tree by their
frequencies because I will need that
in the next step)
- Find the 2 trees with the
lowest frequencies
(break ties arbitrarily) and form
new tree with
a root node equal to the
sum of their
frequencies:
(Now we have 4 binary trees !!!)
- Find the 2 trees with the
lowest frequencies
(break ties arbitrarily) and form
new tree with
a root node equal to the
sum of their
frequencies:
(Now we have 3 binary trees !!!)
- Find the 2 trees with the
lowest frequencies
(break ties arbitrarily) and form
new tree with
a root node equal to the
sum of their
frequencies:
(Now there are 2 binary trees !!!)
- Find the 2 trees with the
lowest frequencies
(break ties arbitrarily) and form
new tree with
a root node equal to the
sum of their
frequencies:
(Now there is only 1 binary tree,
and we are done !!!)
- Assign the Huffman code
as follows:
- When you take a left branch,
append
a 0 to the code word
- When you take a right branch,
append
a 1 to the code word
- The path from the root
to a leaf node
(that store the characters)
is the code word for
that character
|
Example:
- Huffman code for
abracadabra:
a b r a c a d a b r a
1 011 00 1 0100 1 0101 1 011 00 1
Or: 10110010100101011011001
|
|