- Joined
- Jul 18, 2010
- Messages
- 1
- Reaction score
- 0
Hello everyone,
I've programmed my own implementation of Huffman Compression and Decompression, but am having trouble writing out my huffman tree to the compressed file's header. Everything works, and I can compress/decompress a number of files with no change in output, but many of my "compressed" files end up larger than the input files were.
The reason for this has to do with how I am writing out my huffman tree to the header. Right now, I am writing byte values of 0 to represent parent nodes, 1 to represent leaf nodes, and a 2 to represent my pseudo eof node. After each 1 I also write the character value associated with each node. Below is a link to my code for this method. I also included the method I use to rebuild the tree for decompression. It won't let me link to it so you'll have to copy paste it.
I think that I can fix this problem by writing 1 and 0 bits to represent my nodes and then following all 1 bits with the 8 bit value associated with each node. So I would end up with something like 001(8 bit value)00001(8 bit value).... My question is, how would I store my pseudo eof node?
Right now I have an array of 257 values so that I can store the frequencies of characters 0-255 and a pseudo eof ('char' 256). My issue is that I don't know how to write a char value of 256 to a file. If anyone has any insight or advice for me, it would be appreciated. This is the last bit of code I have to write before this project is done, but I'm having a hard time figuring it out.
I can upload more code if requested.
Thank you
I've programmed my own implementation of Huffman Compression and Decompression, but am having trouble writing out my huffman tree to the compressed file's header. Everything works, and I can compress/decompress a number of files with no change in output, but many of my "compressed" files end up larger than the input files were.
The reason for this has to do with how I am writing out my huffman tree to the header. Right now, I am writing byte values of 0 to represent parent nodes, 1 to represent leaf nodes, and a 2 to represent my pseudo eof node. After each 1 I also write the character value associated with each node. Below is a link to my code for this method. I also included the method I use to rebuild the tree for decompression. It won't let me link to it so you'll have to copy paste it.
Code:
pastebin.ca/1903023
I think that I can fix this problem by writing 1 and 0 bits to represent my nodes and then following all 1 bits with the 8 bit value associated with each node. So I would end up with something like 001(8 bit value)00001(8 bit value).... My question is, how would I store my pseudo eof node?
Right now I have an array of 257 values so that I can store the frequencies of characters 0-255 and a pseudo eof ('char' 256). My issue is that I don't know how to write a char value of 256 to a file. If anyone has any insight or advice for me, it would be appreciated. This is the last bit of code I have to write before this project is done, but I'm having a hard time figuring it out.
I can upload more code if requested.
Thank you