Huffman (C)
1. n=|C|
2. Q ← C
3. for i=1 to n-1
4. do
5. z= allocate-Node ()
6. x= left[z]=Extract-Min(Q)
7. y= right[z] =Extract-Min(Q)
8. f [z]=f[x]+f[y]
9. Insert (Q, z)
10. return Extract-Min (Q)
示例:为以下一组频率找到最佳霍夫曼码:
a: 50 b: 25 c: 15 d: 40 e: 75
解:
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code.png)
即
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code2.png)
再次对于i = 2
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code3.png)
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code4.png)
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code5.png)
同样, 我们采用相同的过程
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code6.png)
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code7.png)
因此, 最终输出为:
![霍夫曼编码算法](http://www.srcmini02.com/wp-content/uploads/2020/03/algorithm-of-huffman-code8.png)