-2

I coded a Huffman Algorithm.

I read a file into a String and then built a Huffman Tree and codes with the encoded data.

I put this in a new String and wrote it to new file, but it increases the size of file instead of decreasing it!!

I know what's wrong:

When input string is like "abc" it is 3*8=24 bit

However, when it is coded to "000111" it is 6*8=68 bit

Does anyone have any ideas?

How should I write the "000111" string in the output file so that it only needs 6 bits?

I am using Java.

Please Help.

eboix
  • 5,113
  • 1
  • 27
  • 38
  • 2
    You've got a very big misunderstanding here. The Huffman-encoded output is supposed to be a raw binary stream, not a "string" in any normal sense of the term. You're supposed to be doing binary I/O with that. It's not entirely trivial to do in Java, but you should find plenty of examples out there (http://stackoverflow.com/questions/903375/huffman-compression-algorithm for instance). – Mat Dec 31 '11 at 22:10

2 Answers2

1

It's been a very long time since i was taught about huffman encoding but I'm pretty sure you encode and save in binary not a string representation of binary!

Antony Scott
  • 21,690
  • 12
  • 62
  • 94
1

you need to pick a way to pack bytes, LSB or MSB first, and implement it using bit-shift operators. and remember that Java has no concept of unsigned bytes, and that strings are not equivalent to byte arrays.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107