A Little Huffman Coding with Java TricksMonday, May 16, 2005
A Huffman code is a way to utilize a binary tree to construct a minimal-length encoding for messages where certain characters or groups of characters have known frequencies. The tree used for such an operation called a Huffman tree. Huffman codes are the most efficient compression method for random data and are often found as steps in other compression algorithms such as JPEG and Deflate (ZIP). Building such trees is a very common exercise for Computer Science and Math classes so I can skip the details. To construct a Huffman tree, first initialize a series of nodes, which are simply data structures holding the represented character and its frequency within the message. The series is sorted by frequency and the two lowest frequency nodes are picked from the list and combined into a composite node, which is given the sum of its child node frequencies as its frequency. The composite node is added back to the sorted list and the process is repeated until there is only one node in the list, which is the head of the Huffman tree. In Java, the treeset data structure maintains a sorted list of comparable objects and can be used for an implementation of the Huffman code algorithm in the last paragraph. One caveat is that a treeset is a set, which means that duplicate nodes will simply disappear. The work around is to rig the compareTo method, which is implemented by all comparable objects, so that equality is never returned. In this implementation getCount returns the frequency of the node. The treeset is an ideal data structure for helping build the Huffman coding tree because it maintains a sorted list with a "guaranteed log(n) time cost for the basic operations (add, remove and contains)." My construction method for the Huffman tree uses two removes and one add each time nodes are combined so the worst running time is log^3(n), which is better than sorting algorithms that take n*log(n) or even n^2 time.
The compression rates achieved by this simple algorithm are pretty good even though they are not nearly as good as more specific ones in common use, such as JPEG, gzip, b2zip, etc.
Posted by Frank Rietta at
7:00 AM
Comments
A few weeks ago I received an e-mail from Rubén, in Spain, asking about how one would implement b2zip in Java. Links to This Post |
"Whenever you find a man who says he doesn't believe in a
real Right and Wrong, you will find the same man going back on this a moment later."
Recent Posts
ArchivesApril 2005 / May 2005 / June 2005 / July 2005 / August 2005 / November 2005 / April 2006 / June 2006 / August 2006 / September 2006 / November 2006 / December 2006 / January 2007 / January 2008 /
About MeI am a software developer who has been marketing on the internet since 1999. I hold an MS in Information Security from the Georgia Institute of Technology, from where I previously earned a BS in Computer Science in 2005. I ran an Atlanta-based web hosting business from 1999 until I sold it in 2005. |
Home | Product List | Privacy | Contact