Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Write an application to test the HuffmanTree class. Your application will n

ID: 3940898 • Letter: J

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 characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file, get the frequencies, create the huffman tree. Print the code for each character. Create the encoding for the file. Decode the file. Write the decoded file to another text file.

To get the ascii code of a character: char somechar;

//here you get somechar....reading from file...int i = Integer.parseInt(somechar);

Explanation / Answer

public class Huffman { // alphabet size of extended ASCII private static final int R = 256; // Do not instantiate. private Huffman() { } // Huffman trie node private static class Node implements Comparable { private final char ch; private final int freq; private final Node left, right; Node(char ch, int freq, Node left, Node right) { this.ch = ch; this.freq = freq; this.left = left; this.right = right; } // is the node a leaf node? private boolean isLeaf() { assert ((left == null) && (right == null)) || ((left != null) && (right != null)); return (left == null) && (right == null); } // compare, based on frequency public int compareTo(Node that) { return this.freq - that.freq; } } /** * Reads a sequence of 8-bit bytes from standard input; compresses them * using Huffman codes with an 8-bit alphabet; and writes the results * to standard output. */ public static void compress() { // read the input String s = BinaryStdIn.readString(); char[] input = s.toCharArray(); // tabulate frequency counts int[] freq = new int[R]; for (int i = 0; i