Huffman coding help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Huffman coding help

 
0
  #1
Sep 12th, 2008
Hi,

I'm trying to code Huffman coding.I've formed my huffman tree but dont understand how I'm gonna assign bits to it.How do I do bit operations and how do i maintain bits of length 3,5 etc?

Any help appreciated
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Huffman coding help

 
0
  #2
Sep 12th, 2008
Take a look at <<, >> (shift) and &, |, ^ (mask)
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Huffman coding help

 
0
  #3
Sep 12th, 2008
Huffman codes are all variable-length --don't tack-on extra zeros (that would change the code).

Your tree should be organized top-down with branch 0 having a summed-weight <= branch 1. (Note: the tree will have a "random" balance.) The huffman code is just a list of branches taken in the path from root (MSB) to leaf (LSB).

To decode: starting at the root node, just peel one bit at a time from the input and follow that branch until you hit a leaf node, then emit the leaf's value. The next input bit will again be from the root node.

To encode is just a tad-bit trickier: for each leaf, collect all bits back to the root, then emit them in reverse order (without adding any other bits into the output stream).

For simple bit-packing and unpacking, use a bitvector (vector<bool>). You can then push and read individual bits easily enough...

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: Huffman coding help

 
0
  #4
Sep 12th, 2008
Thanks! I wanna do this without stl.Still not very clear on how I append odd bits
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Huffman coding help

 
1
  #5
Sep 13th, 2008
You need to keep track of how many bits you have "output" so far. When you get eight or more, output a byte.
  1. unsigned char bits = 0; // the unwritten bits
  2. int count = 0; // the number of unwritten bits
  3.  
  4. // write_bit()
  5. // Write a single bit to the output stream
  6. //
  7. void write_bit( ostream& outs, bool bit )
  8. {
  9. // Add the latest bit into our "to-output" bits
  10. bits |= bit << count;
  11.  
  12. // Track how many we have "output" so far
  13. if (++count < 8) return;
  14.  
  15. // If we have "output" a whole byte, then
  16. // actually write it to the output stream
  17. outs.put( bits );
  18.  
  19. // And reset
  20. bits = 0;
  21. count = 0;
  22. }
  23.  
  24. // flush_bits()
  25. // Write any left-over bits --filling
  26. // with zeros to the even byte.
  27. //
  28. void flush_bits( ostream& outs )
  29. {
  30. if (count) outs.put( bits );
  31. bits = 0;
  32. count = 0;
  33. }
I'm not sure how exactly you plan to avoid the STL in C++. The solution would be much nicer if you were to use it... but the above demonstrates the basics.

Notice how since we are dealing with a bit-stream, we must output bits in the same order as we get them -- meaning:
  1. bit # byte 1 byte 2
  2. 1 .......@ ........
  3. 2 ......@x ........ These bytes are DISPLAYED
  4. 3 .....@xx ........ in the way that we humans
  5. 4 ....@xxx ........ like to read them, but what
  6. 5 ...@xxxx ........ is really IMPORTANT is that
  7. 6 ..@xxxxx ........ the bits fill in from the
  8. 7 .@xxxxxx ........ LEAST-significant bit to the
  9. 8 @xxxxxxx ........ MOST-significant bit in each byte.
  10. 9 xxxxxxxx .......@
  11. 10 xxxxxxxx ......@x

Input from the bitstream works the same way: read bits in order: first to last byte, lsb-to-msb in each byte.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC