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.

Here is my HuffTree class:

import java.io.PrintStream; import java.io.Serializable; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 huffTree; private static class CompareHuffmanTrees implements Comparator> {

@Override public int compare(BinaryTree treeLeft,BinaryTree treeRight) { double wLeft = treeLeft.getData().weight; double wRight = treeRight.getData().weight; return Double.compare(wLeft, wRight); } }

public void buildTree(HuffData[] symbols) { Queue> theQueue = new PriorityQueue>(symbols.length,new CompareHuffmanTrees()); for (HuffData nextSymbol : symbols) { BinaryTree aBinaryTree = new BinaryTree(nextSymbol, null, null); theQueue.offer(aBinaryTree); }

// Build the tree. while (theQueue.size() > 1) { BinaryTree left = theQueue.poll(); BinaryTree right = theQueue.poll(); double wl = left.getData().weight; double wr = right.getData().weight; HuffData sum = new HuffData(wl + wr, null); BinaryTree newTree = new BinaryTree(sum, left, right); theQueue.offer(newTree); } huffTree = theQueue.poll(); }

private void printCode(PrintStream out, String code, BinaryTree tree) { HuffData theData = tree.getData(); if (theData.symbol != null) { if (theData.symbol.equals(' ')) { out.println("space: " + code); } else { out.println(theData.symbol + ": " + code); } } else { printCode(out, code + "0", tree.getLeftSubtree()); printCode(out, code + "1", tree.getRightSubtree()); } }

public void printCode(PrintStream out) { printCode(out, "", huffTree); } public String decode(String codedMessage) { StringBuilder result = new StringBuilder(); BinaryTree currentTree = huffTree; for (int i = 0; i < codedMessage.length(); i++) { if (codedMessage.charAt(i) == '1') { currentTree = currentTree.getRightSubtree(); } else { currentTree = currentTree.getLeftSubtree(); } if (currentTree.isLeaf()) { HuffData theData = currentTree.getData(); result.append(theData.symbol); currentTree = huffTree; } } return result.toString(); } public static void main(String [] args) { /* try { input = new Scanner(new FileInputStream("huff.txt")); } catch(FileNotFoundException e) { System.out.println("File data.txt was not found "); System.out.println("or could not be opened. "); System.exit(0); }*/

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!