Sunteți pe pagina 1din 1

CS231 Algorithms Handout # 31a

Prof. Lyn Turbak December 5, 2001


Wellesley College
Huffman Tree Example
The Huffman tree example I gave in class was very “linear”. Below is a “bushier” example of
a Huffman tree. For more examples see CLRS 16.3/CLR17.3.
Suppose we are given the following table of letter frequencies:
a b c d e f
1 2 3 4 5 6
Then the following diagrams show the steps in creating a Huffman coding tree for this example.
Each box indicates the contents of a priority queue of trees, where trees with lower frequency values
have higher priority. At each step, the two smallest frequency trees are merged to form a higher
frequency tree. This continues until there is only one tree.

3 3 4 5 6 7 8
1 2 3 4 5 6 7 8
⇒ c 1 2 d e f g h
a b c d e f g h
a b

4 5 6 6 7 8 6 6 7 8 9

d e f 3 3 g h f 3 3 g h 4 5
⇒ ⇒
c 1 2 c 1 2 d e

a b a b

7 8 9 12 9 12 15

g h 4 5 6 6 4 5 6 6 7 8

⇒ d e f 3 3 ⇒ d e f 3 3 g h

c 1 2 c 1 2

a b a b

36
15 21
15 21
7 8 9 12
7 8 9 12
g h 4 5 6 6
⇒ ⇒ g h 4 5 6 6
d e f 3 3
d e f 3 3
c 1 2
c 1 2
a b
a b

S-ar putea să vă placă și