简体   繁体   English

使用霍夫曼树解码算法/实现

[英]Decoding algorithm/implementation using Huffman tree

Start with a text file that looks like this: 从一个如下所示的文本文件开始:

a: 0
b: 100
c: 101
d: 11

0 0 100 100 11 101

So this would decode: aabbdc 所以这会解码: aabbdc

What decoding algorithm could I use that builds a Huffman tree and then uses it to decode the message? 我可以使用什么解码算法构建一个霍夫曼树然后用它来解码消息? Sample code would be highly appreciated as well! 示例代码也将受到高度赞赏!

Here is what I was thinking: 这就是我的想法:

  • create a lookup table that maps each symbol to its bits 创建一个查找表,将每个符号映射到其位
  • create a root node 创建根节点
  • to build the tree, read in each bit from the encoded 构建树,从编码中读取每个位
    • if 0, create a left child. 如果为0,则创建一个左子项。 if 1, create a right child 如果1,创造一个合适的孩子
    • if space is reached, indicate a leaf somehow (null left and right pointers) 如果到达空间,以某种方式指示一片叶子(空左右指针)
      • use the bits we read up until that space and see what this is the lookup table 使用我们读取的位直到该空间,看看这是什么查找表
      • insert the character at that leaf 在该叶子处插入角色

then, I could just read in each bit again and have it move through the tree. 然后,我可以再次阅读每一位并让它在树中移动。 when it hits a space, I would just return the character at the leaf it reached? 当它撞到一个空间时,我会在它到达的叶子处返回角色?

There aren't / shouldn't be any spaces in the input. 输入中没有/不应该有任何空格。 You should just get something like 0010010011101 . 你应该得到像0010010011101这样的东西。

To create the tree, for each character, start at the root, and, for each bit, go left if it's a 0 or go right if it's a 1 (creating nodes where required). 要创建树,对于每个字符,从根开始,对于每个位,如果它是0则向左移动,如果是1则向右移动(在需要时创建节点)。 When you've reached the end of some character, set the value of the node we're at to the character. 当您到达某个字符的末尾时,将我们所在节点的值设置为该字符。

Then, go through the input, starting from the root in the tree - do the same as above, but, rather than creating nodes, just stop when you reach a leaf, output the value at that node and go back to the root. 然后,从树中的根开始查看输入 - 执行与上面相同的操作,但是,而不是创建节点,只需在到达叶子时停止,在该节点输出值并返回到根。

Example: 例:

a = 0 - just create a left child for the root. a = 0 - 只为根创建一个左子项。

  .
 /
a

b = 100 - go right (for the 1 ), then left (for the 0 ), then left again (for the 0 ). b = 100 - 向右(对于1 ),然后向左(对于0 ),然后再向左(对于0 )。

  .
 / \
a   .
   /
  .
 /
b

c = 101 - go right, then left, then right. c = 101 - 向右,然后向左,然后向右。

  .
 / \
a   .
   /
  .
 / \
b   c

d = 11 - go right, then right. d = 11 - 向右走,然后向右走。

  .
 / \
a   .
   / \
  .   d
 / \
b   c

When processing the input 00100 ... 处理输入00100 ......

Start at the root. 从根开始。

We get a 0 , so go left. 我们得到0 ,所以左转。
We get to a leaf that's value is a , so output it and go back to the root. 我们得到一个叶子这是值是a ,所以其输出并返回到根。

We get a 0 , so go left. 我们得到0 ,所以左转。
We get to a leaf that's value is a , so output it and go back to the root. 我们得到一个叶子这是值是a ,所以其输出并返回到根。

We get a 1 , so go right. 我们得到1 ,所以走吧。
We get a 0 , so go left. 我们得到0 ,所以左转。
We get a 0 , so go left. 我们得到0 ,所以左转。
We get to a leaf that's value is b , so output it and go back to the root. 我们得到一个值为b的叶子,所以输出它并返回到根。

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

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