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
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. if 1, create a right child
- 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?