I am doing this project to create a huffman encoder and decoder, but I am stuck in the part where every character from an input file has to be assigned the huffman code.
// FILE: huffman.cc
// This file contains the implementation of Huffman coding and decoding program.
int main()
{
////////////////////// Read Probability Data /////////////////////
// Open the probability file for reading
string probfilename = "probability.txt";
ifstream probfile;
probfile.open(probfilename.c_str(), ifstream::in);
assert(probfile);
// Read in letters and associated probabilities, create a list of nodes
list<node*> node_list;
node* tempNode;
char y[6];
char x;
double probability;
for (int i = 0; i <= 25; i++)
{
x = probfile.get();
probfile.get(y, 8);
probability = floor(stod(y) * 10000) / 10000;
tempNode = new node(x, probability);
node_list.push_front(tempNode);
probfile.get();
}
// Close the probability file
probfile.close();
/////////////////// Construct Huffman Coding Tree ////////////////////
// sort the node list in the order of ascending probability
node_list.sort(comp_prob);
// Repeat the following until there is only one node left in the node list,
// this node is also the root of the huffman coding tree:
// Extract the two nodes with the lowest probabilities,
// connect them to a new node, and insert the new node back into the list
while (node_list.size() > 1)
{
tempNode = node_list.front();
node_list.pop_front();
tempNode = combine(tempNode, node_list.front());
node_list.pop_front();
node_list.push_front(tempNode);
node_list.sort(comp_prob);
}
// Traverse the huffman tree to generate the huffman coding table
list<node*> huffman_list;
string huffman_code;
generateCode(node_list.front(), huffman_code, huffman_list);
// Open the text file for reading
string textfilename = "input.txt";
ifstream textfile;
textfile.open(textfilename.c_str(), ifstream::in);
assert(textfile);
// Open the file for writing encoded text
string encodedfilename = "encoded.txt";
fstream encodedfile;
encodedfile.open(encodedfilename.c_str(), fstream::out | fstream::in);
assert(encodedfile);
// Input and encode each character from the input file one by one
// and output them to the output file
// TODO: add your code here ...
char a[1000];
for (int i = 0; i < 10000; i++)
textfile.get(a, 2);
// Close the text file
textfile.close();
//////////////////////// Decode the Encoded File ///////////////////////////
// Reset the encoded text file for reading
encodedfile.clear();
encodedfile.seekg(0, ios::beg);
// Open the file for writing decoded text
string decodedfilename = "decoded.txt";
ofstream decodedfile;
decodedfile.open(decodedfilename.c_str(), ofstream::out);
assert(decodedfile);
// Decode the text from the encoded file
// TODO: add your code here ...
// Close the input and output files for decoding
encodedfile.close();
decodedfile.close();
// Delete all the node objects
// TODO: add your code here ...
return 0;
}
// FILE: node.h
// CLASS PROVIDED: node
// Each node on a binarytree has the following features:
// letter: a character. If an internal node, the NULL character ('\0') is used.
// probability: a double value showing the probability of occurrence
// left: a pointer to the left child
// right: a pointer to the right child
node(char c, double p)
{
letter = c;
probability = p;
}
};
// NON-MEMBER FUNCTIONS
// Attaches two nodes together to a common root
node* combine(node* left_child, node* right_child)
{
node* parent;
parent = new node(left_child->letter + right_child->letter, left_child->probability + right_child->probability);
parent->left = left_child;
parent->right = right_child;
return parent;
}
// Clean all nodes attached to the root
void clean(node* root);
// Define a function comparing the probability
// purpose: to sort the node list in the order of ascending probability
bool comp_prob(node* first, node* second)
{
if ( first->probability < second->probability )
return true;