Valid Huffman Codes?

2020-04-23 06:44发布

I'm trying to solve a Huffman Coding problem, but I'm not completely sure I understand the topic completely. I am trying to figure out if the following are is a valid Huffman Code:

A: 0  
B: 01  
C: 11  
D: 110  
E: 111  

What I'm thinking is that it is not valid, because A, or 1, would infringe on B, or 01. I'm not positive though. Could someone enlighten me on this?

Edit: I'm sorry I meant to type A as 0 and not 1.

1条回答
Evening l夕情丶
2楼-- · 2020-04-23 07:11

No. A Huffman code is a prefix code, which means that no code can be a prefix of any other code. In your example, A is a prefix of B, and C is a prefix of both D and E.

A valid prefix code would be:

A: 0
B: 10
C: 11

That's as far as you can go with codes of length 1, 2, and 2. Any other codes would be a prefix of those. It is not possible to have a prefix code with lengths 1, 2, 2, 3, and 3.

This is a valid prefix code for five symbols:

A: 0
B: 10
C: 110
D: 1110
E: 1111

as is this:

A: 00
B: 01
C: 10
D: 110
E: 111
查看更多
登录 后发表回答