Question: Hello, I am stuck on developing a program for Huffman Encoding. I have a majority of the code written, but I am having trouble implementing

Hello, I am stuck on developing a program for Huffman Encoding. I have a majority of the code written, but I am having trouble implementing and compiling this code.

Encoded Texts:

A - 19

B - 16

C - 17

D - 11

E - 42

F - 12

G - 14

H - 17

I - 16

J - 5

K - 10

L - 20

M - 19

N - 24

O - 18

P - 13

Q - 1

R - 25

S - 35

T - 25

U - 15

V - 5

W - 21

X - 2

Y - 8

Z 3Hello, I am stuck on developing a program for Huffman Encoding. I

import java.util*;

package HuffmanEncoding.java;

public class stackProgram {

private Node root; //1st Node

public Tree (char data, int frequency) {

root = new Node();

root.iData=frequency;

root.iData=data;

}

public Tree(Tree leftChild, Tree rightChild)

{

root = new Node();

root.leftChild = leftChild.root;

root.rightChild = rightChild.root;

root.iData = leftChild.root.iData + rightChild.root.iData;

}

protected Tree(Node root)

{

this.root = root;

}

public Tree getRightTree()

{

if(root != null)

return new Tree(root.rightChild);

else

return null;

}

public char getRootChar()

{

if(root != null)

return root.dData;

else

return '\0';

}

public void displayTree()

{

Stack globalStack = new Stack();

globalStack.push(root);

int nBlanks = 32;

boolean isRowEmpty = false;

System.out.println("........................................");

while(isRowEmpty == false)

{

Stack localStack = new Stack();

isRowEmpty = true;

for(int j = 0; j

System.out.print(' ');

while(globalStack.isEmpty() == false)

{

Node temp = (Node)globalStack.pop();

if(temp != null)

{

if(temp.dData == '\0')

{

System.out.print("\\0");

}

else if(temp.dData == ' ')

{

System.out.print("sp");

}

else if(temp.dData == ' ')

{

System.out.print("If");

}

else

{

System.out.print(" "

+ temp.dData);

}

localStack.push(temp.leftChild);

localStack.push(temp.rightChild);

if(temp.leftChild != null ||

temp.rightChild != null)

isRowEmpty = false;

}

else

{

System.out.print("--");

localStack.push(null);

localStack.push(null);

}

for(int j = 0; j

System.out.print(' ');

}

System.out.println();

nBlanks /= 2;

while(localStack.isEmpty()==false)

globalStack.push(localStack.pop());

}

System.out.println("........................................");

}

public int compareTo(Tree arg0)

{

Integer freq1 = new Integer(this.root.iData);

Integer freq2 = new Integer(arg0.root.iData);

return freq1.compareTo(freq2);

}

@Override

public int compareTo(Object arg0) {

// TODO Auto-generated method stub

return 0;

}

}

}

import java.util.HashMap;

import java.util.Map;

import java.util.PriorityQueue;

import java.util.TreeMap;

import ProgrammingAssignment4.txt;

import ///Texts with decoded tree;

public class HuffmanEncoding {

Tree tree;

TreeMap codeTable;

protected HashMap calculateFrequency (String message) {

int messageLength = messageLength();

char ch;

int count;

HashMap charCounts = new HashMap();

for (int i = 0; i

ch= message.charAt(i);

if(charCounts.containsKey(ch)) {

count=Integer.parseInt((String)charCounts.get(ch));

++count;

charCounts.put(ch,count);

}

else {

charCounts.put(ch,1);

}

}

return charCounts;

}

protected void creatHuffmanTree(String message) {

HashMap charCounts = caluclateFrequency(message);

PriorityQueue trees = new PriorityQueue();

Tree temp;

for(Map.Entrye:charCounts.entrySet()) { //initiates values of K and V

temp = new Tree((Tree)e.getKey(),(Tree)e.getValue());

trees.add(temp);

}

while(trees.size()>1) {

temp= new Tree((Tree)trees.remove(), (Tree)trees.remove());

trees.add(temp);

}

tree=(Tree) trees.remove();

}

public void createCodeTable(String message) {

create HuffmanTree(message);

codeTable = new TreeMap();

createCodeTable(tree,"");

}

protected void createCodeTable(Tree tree, String code) {

if(tree == null) {

return;

}

char c= tree.getRootChar();

if(c!='\0') {

codeTable.put(c, code);

} else {

createCodeTable(tree.getLeftTree(), code + "0"); //method implementation

createCodeTable(tree.getLeftTree(), code +"1"); //method implementation

}

}

public void displayCodeTable() {

System,out.println("Character Code: ");

for (Map.Entryentry:codeTable.entrySet()) {

char key = ((String)entry.getKey()).charAt(0);

if (key=='') {

System.out.println("If" + entry.getValue());

} else {

System.out.println(key + "" + entry.getValue()) ;

}

}

}

public String encodeMessage(String message) {

if(codeTable ==null) {

System.out.println("No code table created.");

return "";

}

String codedMessage ="";

for (int i=0; i

char c = message.charAt(i);

codedMessage += codeTable.get(c);

}

return codedMessage;

}

public String decodeMessage(String codedMessage) {

String decodedMessage = "";

Tree temp = tree;

for (int i =0; i

if (codedMessage.charAt(i)=='0') {

temp = temp.getLeftTree(); //implement

} else {

temp = temp.getRight Tree();

}

if (temp.getRootChar() != '\0') {

decodedMessage += temp.getRootChar();

temp=tree;

}

}

return decodedMessage;

}

}

public class Node {

public int iData;

public char dData; //hold char

public Node leftChild;

public Node rightChild;

public void displayNode {

System.out.print("{");

System.out.print(iData);

System.out.print(", ");

System.out.print(dData);

System.out.print("}");

}

}

605.202 Data Structures LAB 3 Using the frequency table shown below, build a Huffman Encoding Tree. Resolve ties by giving single letter groups precedence (put to the left) over multiple letter groups, then alphabetically. Do not worry about punctuation or capitalization. For this lab only, you may use string methods to work with the groups of letters. This program will need to be able to read three different files: a file containing the frequency table, a file containing clear text to be encoded and a file containing encoded strings. Print out the tree by doing a preorder traversal. Print the resulting encoding generated by the tree. An example is given below for a 3-letter alphabet. You may use any reasonable format to print the encoding XYZ:6 ?? YZ:3 Z:2 X-3 Y-1 Z-2 The tree in preorder is: XYZ: 6, X: 3, YZ: 3, Y: 1, Z: 2 The code is X = 0; Y = 10, Z-11; In your write-up, consider whether you achieved any useful data compression with this method. Compare to conventional encoding. How would your results be affected by using a different scheme to break ties, for example, if you had given precedence to alphabetical ordering and then to the number of letters in the key? What other structures did you use and why? Encode the strings in the supplied ClearText.txt file, plus several others of your choice: Decode the strings the supplied Encoded.txt file, plus some others of your choice Use the frequency table in the supplied FreqTable.txt file. Consider experimenting with other frequency tables. As a check, here is a simple string in clear text and encoded form Hello World

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!