This is supposed to go through a String and find the frequency of all the characters in the String, and create a table with the data found. I am getting out of bounds errors in the getFreq(). Someone else look at this please.
import java.util.StringTokenizer;
publicclass HuffmanTree
{
//Some class variables.
char[][] freqTable;
int row=0, col=0;
public HuffmanTree(String sentence){
freqTable=newchar[2][sentence.length()];//2d-array same size as the string arg.
getFreq(sentence, 0);
}
/*****************************************************
Name: getFreq()
Parameters: String, int
Declared variables:char current, bool doIt, int freqCount
char fc.
Return value: void
Algorithm: (assumes the user puts all the characters in the same case)
getFreq(entireString, starting indx 'initialy at 0')
remove white spaces if flag alows.
frequent count=0
current=char @ stIndx
put current in the 2d-frequency array
for(compare every char in the entire string to char @ stIndx)
every true comparison to the char @ stIndx add one to a
counter for that char.
end for.
remove every instance of the current char.
convert counter int to a char.
put number(counter) in the 2d-frequency array.
++row to drop the place down one.
if(startIndx is not at the end of the string)
recurse.
****************************************************/
publicvoid getFreq(String s, int stIndx){
char current;
boolean doIt=false;
if(doIt){//If doneBefore equals false.
s=removeWhiteSpaces(s);
doIt=true;
}
//while(stIndx!=s.length()+1){
int freqCount=0;
current=s.charAt(stIndx);
freqTable[row][col]=current;
for(int i=0; i<=s.length(); ++i){
if(current==s.charAt(i))
freqCount++;
}
s=removeChar(s,current);//Removes every instance of the current char from sentence.
doIt=false;//Remove all the white spaces again.
char fc=(char)freqCount;
//Adds the frequency to the same row one column over.
fc=freqTable[row][1];
++row;//Drops down a row, ready for the next one.
//}
if(stIndx<=s.length())//If not at the end
getFreq(s, ++stIndx);//recurse.
}
/*****************************************************
Name: printFreqTable()
Parameters: none.
Declared variables: none except the for loop counters
Return value: void
Purpose: To move through the entire Frequency table and
output it to the screen.
Recursive approach algorithm:
printFreqTable(int startRowIndx, int startColIndx)
print row and column
if col<=col.length
recurse(++startRow, ++startCol)
end.
****************************************************/
publicvoid printFreqTable(){
System.out.println("Frequency Table:");
for(int r=0; r<=freqTable[row].length; ++r){//Print the row.
System.out.println(freqTable[r]+" = ");
for(int c=0; c<=freqTable[col].length; ++c){//Print the column.
System.out.print(freqTable[c]);
}
}
}
public String removeWhiteSpaces(String s){
StringTokenizer sToken=new StringTokenizer(s," ",false);
StringBuffer sbuff=new StringBuffer();
while(sToken.hasMoreTokens()){
sbuff.append(sToken.nextToken());
}
s=sbuff.toString();//Converts the StringBuffer sbuff into a regular String.
return s;
}
//Removes at a specific indx.
publicstatic String removeCharAt(String s, int pos) {
return s.substring(0,pos)+s.substring(pos+1);
}
//Removes all related characters.
publicstatic String removeChar(String s, char c) {
String r = "";
for (int i = 0; i < s.length(); i ++) {
if (s.charAt(i) != c) r += s.charAt(i);
}
return r;
}
//The treeNode innner class.
publicclass treeNode
{
int frequency;
char value;
char code='0';
boolean isRoot;
public treeNode(int freq, char val, boolean isR)
{
this.frequency=freq;
this.value=val;
this.isRoot=isR;//Set this flag to true if node is root of tree.
}
}
//The treeLeaf inner class.
publicclass treeLeaf
{
int frequency;
char value;
char code='1';
public treeLeaf(int freq, char val)
{
this.frequency=freq;
this.value=val;
}
}
publicstaticvoid main(String[] args){
HuffmanTree tree=new HuffmanTree("susie says come over");
//tree.printFreqTable();
}
}