简体   繁体   中英

Huffman code with lookup table

I have read an article on Internet and know that the natural way of decoding by traversing from root but I want to do it faster with a lookup table.

input:"abcdaabaaabaaa"
code   data
0      a
10     b
110    c
111    d

For your example, the maximum code length is 3 bits. So you take the first 3 bits from your stream (010) and use that to index the table. This gives code, 'a' and bits = 1. You consume 1 bit from your input stream, output the code, and carry on. On the second go around you will get (101), which indexes as 'b' and 2 bits, etc.

To build the table, make it as large as 1 << max_code_length, and fill in details as if you are decoding the index as a huffman code. If you look at your example all the indices which begin '0' are a, indices beginning '10' are b, and so on.

I'm a liitle confused here, if we carry on with the sequence and go to the third which is '011' it decodes to 'C' not 'A' as in the example table. I've been trying for ages to figure out a way to create a huffman table. I've managed to create a list of characters that are most often used ie frequency order. But I want a method to create the table in RAM. So i can have a simple assembly program look th value up. But most importantly a logical method to create the Huffman table. It's not for any important project just something I wanted to get to understand.

"

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