按照如下概率进行三元huffman编码:space:0.1956,e:0.105,t:0.072,o:0.0654,a:0.063,n:0.059,l:0.055,r:0.054,s:0.052,h:0.047,d:0.035,i:0.029,c:0.023,f:0.0225,u:0.0225,m:0.021,p:0.0175,y:0.012,w:0.012,g:0.011,b:0.0105,v:0.008,k:0.003,x:0.002,j:0.001,q:0.001,z:0.001
时间: 2024-04-29 21:20:15 浏览: 197
首先,我们需要将这些概率按照从小到大的顺序排列,然后将每个符号看做一个节点。
| 符号 | 概率 |
| --- | --- |
| q | 0.001 |
| z | 0.001 |
| j | 0.001 |
| x | 0.002 |
| k | 0.003 |
| b | 0.0105 |
| g | 0.011 |
| w | 0.012 |
| y | 0.012 |
| p | 0.0175 |
| v | 0.008 |
| m | 0.021 |
| u | 0.0225 |
| f | 0.0225 |
| c | 0.023 |
| i | 0.029 |
| d | 0.035 |
| h | 0.047 |
| s | 0.052 |
| r | 0.054 |
| l | 0.055 |
| n | 0.059 |
| a | 0.063 |
| o | 0.0654 |
| t | 0.072 |
| e | 0.105 |
| space | 0.1956 |
接下来,我们将每两个概率最小的节点合并成一个新的节点,直到只剩下一个节点为止。在每一步中,我们将两个节点看做左右两个子节点,并将它们的概率相加,作为新节点的概率。
第一步:
| 符号 | 概率 |
| --- | --- |
| q | 0.001 |
| z | 0.001 |
| j | 0.001 |
| x | 0.002 |
| k | 0.003 |
| b | 0.0105 |
| g | 0.011 |
| w | 0.012 |
| y | 0.012 |
| p | 0.0175 |
| v | 0.008 |
| m | 0.021 |
| u | 0.0225 |
| f | 0.0225 |
| c | 0.023 |
| i | 0.029 |
| d | 0.035 |
| h | 0.047 |
| s | 0.052 |
| r | 0.054 |
| l | 0.055 |
| n | 0.059 |
| a | 0.063 |
| o | 0.0654 |
| t | 0.072 |
| e | 0.105 |
| space | 0.1956 |
| q,z | 0.002 |
| j,x | 0.003 |
| k,q,z | 0.005 |
| j,x,k,q,z | 0.008 |
| b,g | 0.0215 |
| y,w | 0.024 |
| p,v | 0.0255 |
| b,g,y,w,p,v | 0.0715 |
| m,u | 0.0445 |
| f,c | 0.045 |
| i,d | 0.064 |
| m,u,f,c,i,d | 0.1535 |
| h,s | 0.099 |
| r,l | 0.109 |
| n,a | 0.122 |
| o,t | 0.1374 |
| e,space | 0.3016 |
第二步:
| 符号 | 概率 |
| --- | --- |
| q | 0.001 |
| z | 0.001 |
| j | 0.001 |
| x | 0.002 |
| k | 0.003 |
| b | 0.0105 |
| g | 0.011 |
| w | 0.012 |
| y | 0.012 |
| p | 0.0175 |
| v | 0.008 |
| m | 0.021 |
| u | 0.0225 |
| f | 0.0225 |
| c | 0.023 |
| i | 0.029 |
| d | 0.035 |
| h | 0.047 |
| s | 0.052 |
| r | 0.054 |
| l | 0.055 |
| n | 0.059 |
| a | 0.063 |
| o | 0.0654 |
| t | 0.072 |
| e | 0.105 |
| space | 0.1956 |
| q,z,j,x,k | 0.008 |
| b,g,y,w,p,v | 0.0715 |
| m,u,f,c,i,d | 0.1535 |
| h,s,r,l,n,a,o,t,e,space | 0.7985 |
第三步:
| 符号 | 概率 |
| --- | --- |
| q,z,j,x,k | 0.008 |
| b,g,y,w,p,v | 0.0715 |
| m,u,f,c,i,d | 0.1535 |
| h,s,r,l,n,a,o,t,e,space | 0.7985 |
| b,g,y,w,p,v,q,z,j,x,k | 0.0795 |
| m,u,f,c,i,d,b,g,y,w,p,v,q,z,j,x,k | 0.233 |
| h,s,r,l,n,a,o,t,e,space,m,u,f,c,i,d,b,g,y,w,p,v,q,z,j,x,k | 1 |
现在我们可以构造每个符号的Huffman编码。从根节点开始,向左走为0,向右走为1。符号的编码是从根节点到符号节点的所有路径上的0和1的序列。编码的长度等于路径的长度。
| 符号 | 概率 | Huffman编码 |
| --- | --- | --- |
| q | 0.001 | 111111101 |
| z | 0.001 | 111111100 |
| j | 0.001 | 111111011 |
| x | 0.002 | 111111000 |
| k | 0.003 | 111110111 |
| b | 0.0105 | 1111001 |
| g | 0.011 | 1111000 |
| w | 0.012 | 1111011 |
| y | 0.012 | 1111010 |
| p | 0.0175 | 11111010 |
| v | 0.008 | 11111001 |
| m | 0.021 | 11111100 |
| u | 0.0225 | 111110110 |
| f | 0.0225 | 111110101 |
| c | 0.023 | 111110100 |
| i | 0.029 | 111110011 |
| d | 0.035 | 111110010 |
| h | 0.047 | 11110011 |
| s | 0.052 | 11110010 |
| r | 0.054 | 11110001 |
| l | 0.055 | 111100001 |
| n | 0.059 | 111100000 |
| a | 0.063 | 11110101 |
| o | 0.0654 | 11110100 |
| t | 0.072 | 11110111 |
| e | 0.105 | 1111111 |
| space | 0.1956 | 11110110 |
这就是按照给定概率进行三元Huffman编码后得到的结果。
阅读全文