Question: JAVA: Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the
JAVA: Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built, create a Huffman code tree and then a string consisting of '0' and '1' digit characters that represents the code string for that file. Read that string back in and recreate the contents of the original file.
HuffMan Tree Class:
import java.io.*; import java.util.*;
public class HuffmanTree implements Serializable { public static class HuffData implements Serializable { private double weight; private Character symbol;
public HuffData(double weight, Character symbol) { this.weight = weight; this.symbol = symbol; }
public Character getSymbol() {return symbol;} } protected BinaryTree
@Override public int compare(BinaryTree
public void buildTree(HuffData[] symbols) { Queue
// Build the tree. while (theQueue.size() > 1) { BinaryTree
private void printCode(PrintStream out, String code, BinaryTree
public void printCode(PrintStream out) { printCode(out, "", huffTree); } public String decode(String codedMessage) { StringBuilder result = new StringBuilder(); BinaryTree
try { HuffmanTree.HuffData hd[]=new HuffmanTree.HuffData[256]; Scanner input = new Scanner(new FileInputStream("huff.txt")); while(input.hasNext()) { System.out.println("pizza"); } } catch(FileNotFoundException e) { System.out.println("File huff.txt was not found "); System.out.println("or could not be opened. "); System.exit(0); } }
}
BINARY TREE CLASS:
import java.io.BufferedReader; import java.io.IOException; import java.io.Serializable;
public class BinaryTree
protected static class Node
public Node(E data) { this.data = data; left = null; right = null; }
@Override public String toString() { return data.toString(); } }
protected Node
public BinaryTree() { root = null; }
protected BinaryTree(Node
public BinaryTree(E data, BinaryTree
public BinaryTree
public BinaryTree
public E getData() { if (root != null) { return root.data; } else { return null; } }
public boolean isLeaf() { return (root == null || (root.left == null && root.right == null)); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); preOrderTraverse(root, 1, sb); return sb.toString(); }
private void preOrderTraverse(Node
public static BinaryTree
public String preorderToString() { StringBuilder stb = new StringBuilder(); preorderToString(stb, root); return stb.toString(); }
private void preorderToString(StringBuilder stb, Node
public String postorderToString() { StringBuilder stb = new StringBuilder(); postorderToString(stb, root); return stb.toString(); }
private void postorderToString(StringBuilder stb, Node
public String inorderToString() { StringBuilder stb = new StringBuilder(); inorderToString(stb, root); return stb.toString(); }
private void inorderToString(StringBuilder stb, Node
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
