here is the class huffman where i need help in the following functions
private void queueTrees(){
for each char in the frequancy table loop{
if freq =0 do nothing
else
creat a new node contains the char and the frequency
creat a tree for the node
insert node in priority queue
}
}
private void makeHuffTree(){
loop until one tree in the queue {
dequeu first node
deququ second node
add the frequency of one and two
creat a new tree with new frequencies as it's root
connect the new tree in the correct place in the queue
}
}
private void makeCodeTable(Node nd, String bc)
{
if the node is not a leaf{
(external node ) (add symbol like + to make it recognizable, nd.ch=='+')
call it recursively
makecodetable (nd.leftchild,bc+"0");
makecodetable (nd.rightchild,bc+"0");
}
else it is aa leaf node so put it in code table
}
public void decode()
-----------------------------------------------------------------------
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
{
// 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
{
} // end makeHuffTree()
// -------------------------------------------------------------
private void makeCodeTable(Node nd, String 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