用哈夫曼编码对如下文本编码:The Wind And The Sun One day the wind said to the sun, “Look at that man walking along the road. I can get his cloak off more quickly than you can.” “We will see about that,” said the sun. “I will let you try first.” So the wind tried to make the man take off his cloak. He blew and blew, but the man only pulled his cloak more closely around himself. “I give up,” said the wind at last. “I cannot get his cloak off.” Then the sun tried. He shone as hard as he could. The man soon became hot and took off his cloak.
时间: 2023-06-24 13:03:15 浏览: 43
首先,计算文本中每个字符出现的次数:
| 字符 | 出现次数 |
|------|----------|
| | 47 |
| e | 22 |
| t | 19 |
| o | 16 |
| h | 14 |
| n | 13 |
| a | 11 |
| s | 10 |
| i | 8 |
| l | 7 |
| u | 6 |
| d | 5 |
| y | 4 |
| f | 3 |
| c | 3 |
| k | 2 |
| r | 2 |
| m | 1 |
| w | 1 |
| g | 1 |
| b | 1 |
| p | 1 |
然后,构建哈夫曼树。从出现次数最少的字符开始构建,每次选取频率最小的两个字符作为左右子节点,构建新的父节点,直到只剩下一个节点为止。
![Huffman Tree](https://i.imgur.com/0SjDjLZ.png)
根据哈夫曼树,得到每个字符的编码:
| 字符 | 频率 | 编码 |
|------|------|------|
| | 47 | 0 |
| e | 22 | 101 |
| t | 19 | 111 |
| o | 16 | 100 |
| h | 14 | 1101 |
| n | 13 | 011 |
| a | 11 | 001 |
| s | 10 | 1100 |
| i | 8 | 0101 |
| l | 7 | 0100 |
| u | 6 | 11001|
| d | 5 | 0001 |
| y | 4 | 110001|
| f | 3 | 00000|
| c | 3 | 110010|
| k | 2 | 110011|
| r | 2 | 00001|
| m | 1 | 000001|
| w | 1 | 000000|
| g | 1 | 110000|
| b | 1 | 110101|
| p | 1 | 110100|
最终,将文本编码为:1011001110100101111110010000010000101110111011111010101011011110110111111111111001000101010011110001110010110100001101111111001101011011000111101010100100010111010010101110100111110111001011001001001011111001011011111001101010011011101010110101110100111101011011001101101000111011011000100100101
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)