These are the classes that i'm working on now
PHP Code:
import java.io.*;
import java.util.*;
class Huffman
{
private PriorityQ theQueue;
private String inString; // input from user
private int strlen;
private String capsString; // converted to all caps
private Tree[] treeArray;
private Tree huffTree; // Huffman tree
private int freqTable[]; // letter frequencies
private int alphabetSize; // size of frequency table
private String codeTable[]; // code for each letter
private String codedMsg; // binary string
private String decodedMsg; // back to original msg
// -------------------------------------------------------------
Huffman(String s) // constructor
{
inString = s;
strlen = inString.length();
alphabetSize = 28; // 26 letters, cr, lf
theQueue = new PriorityQ(alphabetSize); // make the queue
capsString = "";
codeTable = new String[alphabetSize]; // make code table
makeFreqTable(); // construct frequency table
queueTrees(); // put one-node trees in queue
makeHuffTree(); // construct Huffman tree
} // end constructor
// -------------------------------------------------------------
public void displayTree()
{
if(huffTree != null)
huffTree.display();
}
// -------------------------------------------------------------
private void makeFreqTable()
{
freqTable = new int[alphabetSize];
int temp;
char sp = 32; // space
for(int j=0; j<strlen; j++) // for every char
{ // of user's input msg
char ch = inString.charAt(j);
if(ch>96 && ch<123) // lowercase (97-122)
{
temp = (int)ch - 32; // convert to upper
ch = (char)temp; // (65-90)
}
if(ch==32) // space becomes 91
ch = 91;
else if(ch==10) // linefeed becomes 92
ch = 92;
else if(ch<65 || ch>92) // if not uppercase
continue; // letter, ignore it
capsString = capsString + ch; // add to caps string
int index = (int)ch - 65; // convert to 0 to 27
freqTable[index]++; // keep count in
} // end for // frequency table
System.out.println(capsString); // all-caps string
for(int j=0; j<28; j++) // row of characters
System.out.print( (char)(j+65) + " ");
System.out.println("");
for(int j=0; j<28; j++) // row of frequencies
System.out.print(freqTable[j] + " ");
System.out.println("");
} // end makeFreqTable()
// -------------------------------------------------------------
private void queueTrees() // put trees in queue
{
HuffmanApp a = new HuffmanApp();
for (int i =0;i<freqTable.length;i++){
if (freqTable[i]==0)
continue;
else{
char l = codeTable[i].charAt(i);
Node newNode = new Node (l,freqTable[i]);
Tree t1 = new Tree(newNode);
theQueue.insert(t1);
}
}
// for each char in frequency table forget unused characters
// make node
// make tree
// put the tree in queue
} // end queueTrees()
// -------------------------------------------------------------
private void makeHuffTree() // make Huffman tree
{
for (int i=1;i<alphabetSize;i++){
Tree a=theQueue.remove();
int freq1=a.getFreq();
Tree b =theQueue.remove();
int freq2=b.getFreq();
int newfreq =freq1+freq2;
Node newNode = new Node(' ',newfreq);
Tree c = new Tree(newNode);
theQueue.insert(c);
}
} // end makeHuffTree()
// -------------------------------------------------------------
private void makeCodeTable(Node nd, String bc)
{
if (nd.leftChild!=null && nd.rightChild!=null){
makeCodeTable(nd.leftChild,bc+"0");
makeCodeTable(nd.rightChild,bc+"1");
}
// else
// codeTable=codeTable+bc;
} // end makeCodeTable()
// -------------------------------------------------------------
public void code() // encode the msg
{
makeCodeTable(huffTree.root, "");
for(int j=0; j<alphabetSize; j++) // display code table
{
if(codeTable[j] == null)
continue;
char ch = (char)(j+65);
System.out.println(ch + " " + codeTable[j]);
}
codedMsg = ""; // encode the msg
for(int j=0; j<capsString.length(); j++) // for each char
{ // get the code
int index = (int)capsString.charAt(j) - 65;
codedMsg = codedMsg + codeTable[index]; // add to msg
}
System.out.println("Coded message:\n" + codedMsg);
} // end code()
// -------------------------------------------------------------
public void decode()
{
} // end decode()
// -------------------------------------------------------------
} // end class Huffman
PHP Code:
import java.io.*;
import java.util.*;
class HuffmanApp
{
public static void main(String[] args) throws IOException
{
Huffman huff = null;
int value;
String str;
while(true)
{
System.out.print("Enter first letter of ");
System.out.print("enter, show, code, or decode: ");
int choice = getChar();
switch(choice)
{
case 'e':
System.out.println(
"Enter text lines, terminate with $");
str = getText();
huff = new Huffman(str);
break;
case 's':
huff.displayTree();
break;
case 'c':
huff.code();
break;
case 'd':
huff.decode();
break;
default:
System.out.print("Invalid entry\n");
} // end switch
} // end while
} // end main()
// -------------------------------------------------------------
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
// -------------------------------------------------------------
public static String getText() throws IOException
{
String outStr="", str = "";
while(true)
{
str = getString();
if( str.equals("$") )
return outStr;
outStr = outStr + str + "\n";
}
} // end getText()
// -------------------------------------------------------------
public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
//-------------------------------------------------------------
public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}
// -------------------------------------------------------------
} // end class HuffmanApp
PHP Code:
// huffman.java
// demonstrates Huffman trees
// to run this program: C>java HuffmanApp
import java.io.*;
import java.util.*; // for Stack class
////////////////////////////////////////////////////////////////
class Node
{
public char ch; // letter
public int freq; // instances of this letter
public Node leftChild; // this node's left child
public Node rightChild; // this node's right child
// -------------------------------------------------------------
Node(char c, int f) // constructor
{
ch = c;
freq = f;
}
// -------------------------------------------------------------
public void displayNode() // display ourself
{ System.out.print( ch + "" + freq ); }
} // end class Node
////////////////////////////////////////////////////////////////
class Tree
{
public Node root; // first node of tree
// -------------------------------------------------------------
public Tree(Node nd) // constructor
{ root = nd; } // root of tree from argument
// -------------------------------------------------------------
public int getFreq() // returns node's word freq
{ return root.freq; }
// -------------------------------------------------------------
public void display()
{
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<nBlanks; j++)
System.out.print(' ');
while(globalStack.isEmpty()==false)
{
Node temp = (Node)globalStack.pop();
if(temp != null)
{
temp.displayNode();
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<nBlanks*2-2; j++)
System.out.print(' ');
} // end while globalStack not empty
System.out.println();
nBlanks /= 2;
while(localStack.isEmpty()==false)
globalStack.push( localStack.pop() );
} // end while isRowEmpty is false
System.out.println(
"......................................................");
} // end displayTree()
// -------------------------------------------------------------
} // end class Tree
////////////////////////////////////////////////////////////////
class PriorityQ
{
// array in sorted order, from max at 0 to min at size-1
private int maxSize;
private Tree[] queArray;
private int nItems;
//-------------------------------------------------------------
public PriorityQ(int s) // constructor
{
maxSize = s;
queArray = new Tree[maxSize];
nItems = 0;
}
//-------------------------------------------------------------
public void insert(Tree item) // insert item
{
int j;
if(nItems==0) // if no items,
queArray[nItems++] = item; // insert at 0
else // if items,
{
for(j=nItems-1; j>=0; j--) // start at end,
{ // if new item larger,
if( item.root.freq > queArray[j].root.freq )
queArray[j+1] = queArray[j]; // shift upward
else // if smaller,
break; // done shifting
} // end for
queArray[j+1] = item; // insert it
nItems++;
} // end else (nItems > 0)
} // end insert()
//-------------------------------------------------------------
public Tree remove() // remove minimum item
{ return queArray[--nItems]; }
//-------------------------------------------------------------
public Tree peekMin() // peek at minimum item
{ return queArray[nItems-1]; }
//-------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{ return (nItems==0); }
//-------------------------------------------------------------
public boolean isFull() // true if queue is full
{ return (nItems == maxSize); }
//-------------------------------------------------------------
public int getSize()
{ return nItems; }
//-------------------------------------------------------------
} // end class PriorityQ
////////////////////////////////////////////////////////////////
thx for your help