public class HuffmanTreeUtilities
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
HuffmanTreeUtilities.HuffmanLeaf
A class that defines a leaf node of a HuffmanTree.
|
private static class |
HuffmanTreeUtilities.HuffmanNode
A class that defines an inner node of a HuffmanTree.
|
| Modifier and Type | Field and Description |
|---|---|
private static int |
ASCII_VALUES
Default number of characters in extended ASCII
|
private static java.util.HashMap<java.lang.Character,java.lang.String> |
CODE_TABLE
HashMap that who's entries contain an ASCII character and a String containing
it's Huffman binary code.
|
| Constructor and Description |
|---|
HuffmanTreeUtilities() |
| Modifier and Type | Method and Description |
|---|---|
private static void |
buildTable(HuffmanTree tree,
java.lang.StringBuffer prefix)
A helper method that builds a table of mappings for ASCII characters and their Huffman
encodings from a complete Huffman Tree
|
static HuffmanTree |
buildTree(int[] characterFrequencies)
A method that builds a Huffman Tree, based on an array of character frequencies.
|
static int[] |
calculateFrequencies(java.lang.String input)
A method that calculates the frequencies of all of the characters in a given String
and stores those characters in an array, using the character's ASCII numbers as
their index.
|
static void |
compress(HuffmanTree tree,
java.lang.String filename,
java.lang.String input)
A method that saves text stored in a String to a compressed binary file using
Huffman encoding
|
static java.lang.String |
decompress(HuffmanTree tree,
java.io.File file)
A method that decompresses a binary file created using Huffman encoding.
|
private static void |
inorderTraversal(HuffmanTree tree,
java.lang.StringBuffer prefix,
Traverse traverse)
A helper method that does an in-order traversal of a Huffman tree.
|
static void |
printCodes(HuffmanTree tree,
java.lang.StringBuffer prefix)
A helper method that prints a table of the ASCII character and Huffman Code mapping from a
given Huffman Tree
|
private static final int ASCII_VALUES
private static final java.util.HashMap<java.lang.Character,java.lang.String> CODE_TABLE
public static int[] calculateFrequencies(java.lang.String input)
input - The String to calculate frequencies frompublic static HuffmanTree buildTree(int[] characterFrequencies)
characterFrequencies - The frequencies used to build the Huffman Treepublic static void printCodes(HuffmanTree tree, java.lang.StringBuffer prefix)
tree - A complete Huffman Treeprefix - A StringBuffer used to build the binary mappingprivate static void buildTable(HuffmanTree tree, java.lang.StringBuffer prefix)
tree - The complete Huffman Tree containing the encodingprefix - A StringBuffer used to build the tablepublic static void compress(HuffmanTree tree, java.lang.String filename, java.lang.String input)
tree - The Huffman Tree that contains the encodingfilename - The name of the file to save the compressed data toinput - A String containing text to save to the binary filepublic static java.lang.String decompress(HuffmanTree tree, java.io.File file)
tree - The Huffman Tree containing the encodingfile - The binary file containing the encoded textprivate static void inorderTraversal(HuffmanTree tree, java.lang.StringBuffer prefix, Traverse traverse)
tree - The Huffman Tree to be traversedprefix - A StringBuffer used to keep track of the current pathtraverse - A functional interface that defines what occurs when visiting a leaf node