简体   繁体   English

对于三元霍夫曼问题,我们可以为“4”个字符制作一棵树(或编码方案)吗?

[英]For Ternary Huffman problem, can we make a tree (or encoding scheme) for "4" characters?

For Ternary Huffman problem, can we make a tree (or encoding scheme) for " 4 " characters?" Say I have 4 characters with these frequencies: freq(a)=5 freq(b)=3 freq(c)=2 freq(d)=2对于三元霍夫曼问题,我们可以为“ 4 ”个字符制作一棵树(或编码方案)吗?假设我有 4 个字符具有这些频率: freq(a)=5 freq(b)=3 freq(c)=2 freq (d)=2

How will I encode them in the form of 0,1,2 such that no code word is a prefix of another code word?我将如何以 0,1,2 的形式对它们进行编码,以便没有码字是另一个码字的前缀?

The standard algorithm for generating the optimal ternary Huffman code (as alluded to by rici) involves first making sure there are an odd number of symbols -- by adding a dummy symbol (of frequency 0) if necessary.生成最优三元霍夫曼码的标准算法(如 rici 所暗示的)首先包括确保有奇数个符号——如有必要,通过添加一个虚拟符号(频率为 0)。

In this case, we start with an even number of symbols, so we need to add the dummy symbol that I call Z: freq(a)=5 freq(b)=3 freq(c)=2 freq(d)=2 freq(Z)=0.在这种情况下,我们从偶数个符号开始,因此我们需要添加我称为 Z 的虚拟符号: freq(a)=5 freq(b)=3 freq(c)=2 freq(d)=2频率(Z)=0。

Then as Photon described, we repeatedly combine the 3 nodes with the lowest frequencies into 1 combined symbol.然后如 Photon 所述,我们反复将频率最低的 3 个节点组合成 1 个组合符号。 Each time we replace 3 nodes with 1 node, we reduce the total number of nodes by 2, and so the total number of nodes remains odd at each step.每次我们用 1 个节点替换 3 个节点时,我们将节点总数减少 2,因此每一步的节点总数保持奇数。 In the last step (if we've added the correct number of dummy symbols) we will combine 3 final nodes into a single root node.在最后一步(如果我们添加了正确数量的虚拟符号),我们将把 3 个最终节点组合成一个根节点。

         abcdZ:12
         /  |  \
       2/  1|  0\
    cdZ:4  b:3  a:5
    / | \
  2/ 1| 0\
Z:0  d:2  c:2

So in this case one optimal (Huffman) ternary coding is:因此,在这种情况下,一种最佳(霍夫曼)三元编码是:

a: 0
b: 1
c: 20
d: 21
Z: 22 (should never occur).

See https://en.wikipedia.org/wiki/Huffman_coding#n-ary_Huffman_coding for more details.有关更多详细信息,请参阅https://en.wikipedia.org/wiki/Huffman_coding#n-ary_Huffman_coding

Well for classical huffman you just keep merging 2 lowest frequency nodes at a time to build a tree, when assign 1 to left (or right) edge and 0 to other edge and dfs path to some node is that nodes code. 好吧,对于经典霍夫曼,您只需一次合并两个最低频率的节点以构建一棵树,当将1分配给左(或右)边,将0分配给其他边,并且到某个节点的dfs路径是节点代码时。

ie

在此处输入图片说明

So in this case coding is: 因此,在这种情况下,编码为:

a - 1 1-
b - 01 b-01
c - 001 c-001
d - 000 d-000

On ternary huffman you just join nodes 3 lowest frequencies at a time (and less nodes if not enough nodes for last step) 在三元霍夫曼上,您一次只加入3个最低频率的节点(如果没有足够的节点用于最后一步,则加入较少的节点)

ie

在此处输入图片说明

So in this case coding is: 因此,在这种情况下,编码为:

a - 2 a2
b - 12 b-12
c - 11 c-11
d - 10 d-10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM