简体   繁体   中英

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

How will I encode them in the form of 0,1,2 such that no code word is a prefix of another code word?

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.

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.

Then as Photon described, we repeatedly combine the 3 nodes with the lowest frequencies into 1 combined symbol. 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. 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.

         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.

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.

ie

在此处输入图片说明

So in this case coding is:

a - 1
b - 01
c - 001
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)

ie

在此处输入图片说明

So in this case coding is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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