Project 4: Implement a fast Huffman encoding algorithm
Due: Monday 11/9/2009
For this
assignment you will implement the "huffTree" class, a modified "AVLTree"
class, and a modified "minHeap" class, to implement a fast Huffman
encoding algorithm. The final result
will be a program that reads in a text file and writes to an output file the
binary encoding for each character in the file according to Huffman's
algorithm. For efficient grading, please
use my test code given below.
#include <fstream>
#include "AVLTree.h"
#include "minHeap.h"
#include "huffTree.h"
using namespace std;
void
readAndInsert(AVLTree &avltree, ifstream & ifile)
{
while( ! ifile.eof() )
{
char tmp = ifile.get();
avltree.insertItem( tmp );
}
}
int main()
{
ifstream ifile;
ofstream ofile;
//open input and output files
ifile.open("story.txt");
ofile.open("report.txt");
//read in each character of input
file, insert into a BST
AVLTree avltree;
readAndInsert(avltree, ifile);
//For each node in the BST, create a
huffman tree,
//and insert into a min heap
minHeap heap(1000);
avltree.loadHeap(heap);
//while the heap has more than 1
huffTree, get 2 smallest,
//merge them together, and put merged
tree back in heap
huffTree * h1;
huffTree * h2;
while( heap.getSize() > 1 )
{
h1 =heap.extractMin();
h2 =heap.extractMin();
heap.insert(new
huffTree(h1,h2));
}
//take remaining huffTree, use to
create a table of
//codes for each char, write to
output file
huffTree * finalTree = heap.extractMin();
finalTree->makeReport(ofile);
//close files
ifile.close();
ofile.close();
return 0;
}