[英]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: 这就是我的想法:
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.