
Samples of Huffman encode/decode?
Quote:
>The main reason for requesting such code is to help me get a grasp on
>the file format. There is a lot of info on the web which describes
>the theory, yet very little(none that I've found) that deals with
>the format.
There isn't _A_ format - Huffman encoding (as you have seen) is just
the theory, or way of compressing information. Every compression format
that uses it (be it zip lzh or whatever) will decide on its own way to
store the data.
I would guess that by convergent evolution, most formats will come out
about the same - but here is the method I used:
(I can't remember which flavour of Huffman encoding this is - but
you'll get the idea)
1) Skim through the file once to get the frequencies of each character.
2) Normalise these to give each character a relative frequency between 0
and 255. (Those which actually appear _must_ be given a value of at
least one)
3) Sort these pairs into order based on relative frequency.
4) Go through the list, counting until you reach a zero frequency
character.
*********
Now there is enough information to form the header:
1 byte: 'n' The value from (4) saying how many pairs there will be
n * 2 bytes: Pairs of character and its relative frequency.
*********
5) Construct the tree using the sorted information from (3)
6) Run through the input file, outputting the huffman code for each
character.
Decoding is quite easy - read the header, reconstruct the tree, and run
through the data.
I must admit that the hardest part of all of this for me was learning
how to construct the tree out of the data pairs - but that's another
story :-)
Hope this helps a little, but do feel free to contact!
Howard.
--
Homepage: http://www.astraware.com/
New Game for Win95! http://www.astraware.com/bzzz.html