Search code examples
c#-4.0huffman-coderosetta-code

Trying to implement Huffman Encoding in C# - Is it correct?


I'm trying to implement a Huffman encoding in Visual Studio using C# as programming language.

I have found an example on rosettacode.org: http://rosettacode.org/wiki/Huffman_coding#C.23

but I'm not sure if this is a correct implementation. The output given by rosettacode under this link http://rosettacode.org/wiki/File:CSharpHuffman.jpg for the input "this is an example for huffman encoding" seems not correct to me because chars with less frequency have less bits than chars with higher frequency.

What is your opinion on that?


Solution

  • You are correct that symbols that are less frequent should have codes with more bits, and symbols that are more frequent should have codes with less bits.

    The example you point to is perfectly fine. There are no symbols whose bit lengths are shorter than any other symbol whose frequency is higher.