Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Help regarding huffman compression

This topic is 5981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i am making a compression program using huffman algorithm.i understand how to create huffman tree for decoding.but for the purpose of compression i am having problem understanding how to assign codes.also how can i write on a file the codes in the form of bits.if any one out there can help please reply.

Share this post

Link to post
Share on other sites
First you figure out the "weights" of the bytes in the file. The weight of a byte is just the number of times it appears.

Next you start with n trees, where n is the number of bytes with non-zero weights. Each tree is of depth 1 and contains one of the bytes.

Now, you pick two trees with the lowest weight and combine them into a new tree (with the left branch one of the tree and right branch the other) the new weight of this tree is the sum of the weights of the combined trees. Keep doing that until there''s only one tree, and that''s it.

Also, to write out an arbitrary number of bits, just create a "write_bit" function which accumulates the bits until it''s got a complete byte, then writes that out. To write n-bits, just call your "write_bit" function n times. It''s not the fastest way, but it''s the easiest...

codeka.com - Just click it.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!