Please Help my code for this program is listed below what the program is supposed to do is also listed below. I can't get it to work.

The problem

Write a program to compress a message consisting of the ten symbols A, B, C, D, E, F, G, H, I, and J. You should create a variable-length code for each symbol to replace the traditional 8-bit ASCII code. Your code should have the “prefix property discussed in Section 4.6 (no code symbol can appear at the beginning of any other). Huffman’s algorithm will give you a code of optimal length. (When you build your subtrees in the algorithm, put the previous subtree of smaller weight on the left, so that everyone will get the same answer). If you input a string of characters, the program should output a list of each symbol followed by its frequency and code, and the compression string. The input string GGABBHGHGCHDHGFHGFBAGEDGDHFFEHGAABCBGBAAHHEEHFHFBA A, for example, would yield the table

Letter Frequency Code

A 8 110

B 7 101

C 2 11110

D 3 11111

E 4 1110

F 6 100

G 10 00

H 11 01

I 0 –

J 0 –

And the compression string would begin 00001101011010100010011110 . . .

My code

``````import java.util.*;
import java.io.*;

public class HuffmanTree2 {
BinaryTree htree;
int frequency[];
String alphabet;
String codes[];

public HuffmanTree2 (FileReader inFile) throws Exception {
int charSet[] = new int[128];

for (int i=0; i<128; i++)
charSet[i] = 0;

int count=0;
for (int i=0; i<128; i++)
if (charSet[i] > 0) count++;

frequency = new int[count];
codes = new String[count];
alphabet = "";

count = 0;
for (int i=0; i<128; i++){
if (charSet[i] > 0) {
frequency[count] = charSet[i];
alphabet = alphabet + (char)i;
count++;
}
}

htree = makeTree(frequency, alphabet);
setCodes(htree, "");
}

public HuffmanTree2 (int frequencies[], String initAlphabet){
frequency = new int[initAlphabet.length()];
alphabet = new String(initAlphabet);
codes = new String[initAlphabet.length()];

htree = makeTree(frequency, alphabet);
setCodes(htree, "");
}

public BinaryTree makeTree (int frequencies[], String initAlphabet){
SortedSet sortedTrees = new TreeSet();
BinaryTree tree1, tree2, newTree;
LetterFreq2 value1, value2;

for (int i=0; i<frequencies.length; i++){
if (frequencies[i] > 0)
frequencies[i])));
}

while (sortedTrees.size() > 1){
tree1 = (BinaryTree)sortedTrees.first();
sortedTrees.remove(tree1);
tree2 = (BinaryTree)sortedTrees.first();
sortedTrees.remove(tree2);

value1 = (LetterFreq2)tree1.getRootItem();
value2 = (LetterFreq2)tree2.getRootItem();

newTree = new BinaryTree(new LetterFreq2('*', value1.freq+value2.freq));
newTree.attachLeftSubtree(tree1);
newTree.attachRightSubtree(tree2);
}
return (BinaryTree)sortedTrees.first();
}

private void setCodes(BinaryTree currentTree, String currentCode){
if (currentTree.isLeaf()){
LetterFreq2 value = (LetterFreq2)currentTree.getRootItem();
int pos = alphabet.indexOf(value.letter);
codes[pos] = currentCode;
}
else {
setCodes(currentTree.getLeftTree(), currentCode+"0");
setCodes(currentTree.getRightTree(), currentCode+"1");
}
}
public void printCodes(){
for (int i=0; i<codes.length; i++)
System.out.println(alphabet.charAt(i) + ": " + codes[i]);
}

public String[] getCodes(){
String newCodes[] = new String[codes.length];
for (int i=0; i<codes.length; i++)
newCodes[i] = codes[i];
return newCodes;
};
public String getAlphabet(){
return new String(alphabet);
};
public FileWriter encryptFile(FileReader inFile, String outFileName) throws
Exception{
char ch;
int pos;
int outChar=0;
int bitCount = 0;

FileWriter outFile = new FileWriter(outFileName);

pos = alphabet.indexOf(ch);
for (int i=0; i<codes[pos].length(); i++){
if (bitCount >= 32){
outFile.write(outChar);
outChar = 0;
bitCount = 0;
}
else{
outChar = (outChar << 1) | (int)codes[pos].charAt(i);
bitCount++;
}
}
};
if (bitCount > 0){
outChar = outChar << (32 - bitCount);
outFile.write(outChar);
}
return outFile;
}
}``````
2
Contributors
1
4
Views
13 Years
Discussion Span
Last Post by NPH

1.
Its unnecessary to write:
for (int i=0; i<128; i++)
charSet = 0;

The int[] is already initialized with zeros. Unlike c++.

2.
In the constructor, why do you do this code? The frequencies are already computed in charSet?
for (int i=0; i<128; i++){
if (charSet > 0) {
frequency[count] = charSet;
alphabet = alphabet + (char)i;

If you want to copy it to the frequency array then write:
for (int i=0; i<128; i++){
frequency = charSet;

The rest of your code seems to make sense, but that Constructor with the FileReader is a bit messy. If you need one on one help please email me at NeedProgrammingHelp@hotmail.com or visit my site www.NeedProgrammingHelp.com. Send some more questions and tell me what is the error.

Alex.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.