Huffman Tree Nodes

Chapter: Huffman Tree Nodes

Huffman Encoding creates the optimal coding for an alphabet given an input string that uses this alphabet. The encoding process begins by obtaining a frequency count for each of the letters of the alphabet. A node on terminology: We do consider punctuation characters also to be "letters" and will encode them also. We will discuss how to build a huffman tree in lecture.

We make each letter into a leaf node recording its frequency, and an empty string representing its "encoding so far". You will see my code for this in the first constructor in HuffmanTreeNode.java.

In the next chapter we will build a huffman tree by inserting all these leaves into a priority queue and then repeatedly merging nodes, always removing the two lowest frequency nodes and replacing them with a single node representing the union of the characters with the sum of the frequencies. The left child prepends a 0 to its "encoding so far", and the right child prepends a 1. In this way we build a huffman tree with the original characters at the leaves, each encoded by a string o 0 and 1 that indicate how to get from the root to the leaf by following left links (0) or right links (1).

The second constructor in HuffmanTreeNode.java shows you how I make a new node by merging two existing nodes.


Exercise 2

Either


rhyspj@gwu.edu