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 . . .

Again any help you can give would be great ASAP.

My code

import java.util.*;

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;

while (inFile.ready())

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;

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)
sortedTrees.add(new BinaryTree(new LetterFreq2(initAlphabet.charAt(i),

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

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

newTree = new BinaryTree(new LetterFreq2('*', value1.freq+value2.freq));
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
char ch;
int pos;
int outChar=0;
int bitCount = 0;

FileWriter outFile = new FileWriter(outFileName);

while (inFile.ready()) {
ch = (char);
pos = alphabet.indexOf(ch);
for (int i=0; i<codes[pos].length(); i++){
if (bitCount >= 32){
outChar = 0;
bitCount = 0;
outChar = (outChar << 1) | (int)codes[pos].charAt(i);
if (bitCount > 0){
outChar = outChar << (32 - bitCount);
return outFile;

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

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

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 or visit my site Send some more questions and tell me what is the error.


This article has been dead for over six months. Start a new discussion instead.