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 3
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
protected HashMap calculateFrequency (String message) {
int messageLength = messageLength();
char ch;
int count;
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 Tree temp; for(Map 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.Entry 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("}"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
