Question
I am trying to complete this Huffman coding program. I am trying to print the letters and their codes in alphabetical order and decode the
I am trying to complete this Huffman coding program. I am trying to print the letters and their codes in alphabetical order and decode the binary string. I tried converting the string to a char array so I can easily sort the array and then decode the binary string. I posted the code I am working with and the text files.
a4-1.txt:
9
a 15
b 7
c 8
d 9
e 20
f 6
g 3
h 12
k 4
a4-2.txt: 10111000111011101101011011101111 Correct Decode: EAEFEAEAHADEAA
#include
struct node { node * leftChild; node * rightChild; double frequency; string content; string code; };
vector
node extractMin() { double temp = (double) INT_MAX; vector
if(temp>(*i1).frequency) { pos = i1; temp = (*i1).frequency; } }
node tempNode = (*pos); nodeArray.erase(pos);
return tempNode; }
node getHuffmanTree() {
while(!nodeArray.empty()) { node * tempNode = new node; node * tempNode1 = new node; node * tempNode2 = new node; *tempNode1 = extractMin(); *tempNode2 = extractMin();
tempNode->leftChild = tempNode1; tempNode->rightChild = tempNode2; tempNode->frequency = tempNode1->frequency+tempNode2->frequency; nodeArray.push_back(*tempNode); if(nodeArray.size() == 1)//only the root node exsits { break; } } return nodeArray[0]; }
void BFS(node * temproot,string s) { node * root1 = new node; root1 = temproot; string codes[50]; //string decodeCodes; string letters[50]; int i = 0;
root1->code = s; if(root1 == NULL) {
} else if(root1->leftChild == NULL && root1->rightChild == NULL) { letters[i] = root1->content; codes[i] = root1->code;
//cout<
BFS(root1->leftChild,s.append("1")); s.erase(s.end()-1); BFS(root1->rightChild,s.append("0")); s.erase(s.end()-1); } // for(i=0;i<15;i++) // { // cout< void getHuffmanCode() { int size,i; double tempDouble; string tempString = ""; ifstream file; file.open("a4-1.txt"); string dummyLine; getline(file, dummyLine); //-----------------------------------------------------------String to Int int result = 0; for (int i = 0; i < dummyLine.size(); i++) { result += (dummyLine[i] - 48) * pow(10, (dummyLine.size() - i - 1)); } //cout << result << endl; //-------------------------------------------------------------- for(i = 0;i file >> tempString >> tempDouble; cout << tempString << " " << tempDouble< tempNode.frequency = tempDouble; tempNode.content = tempString; tempNode.leftChild = NULL; tempNode.rightChild = NULL; nodeArray.push_back(tempNode); } cout< node root = getHuffmanTree(); BFS(&root,""); file.close(); } int main() { getHuffmanCode(); string temp = "holder"; char chars[52]; strcpy(chars,temp.c_str()); //--------------------Read in bit file from a4-2.txt ifstream file2; file2.open("a4-2.txt"); string binaryLine; getline(file2, binaryLine); //------------------------------Decode binary file string plain = ""; //take empty string /* int i; //to iterate //loop till binaryLine length is not zero while(binaryLine.length()>0) { //check which symbol is matching for(i=0;i<15;i++) { int n = decodeCodes[i].length(); //take length //if substring of length codes[i] is equals to codes[i], then append symbol to plain if(binaryLine.substr(0,n)==decodeCodes[i]) { plain = plain + chars[i]; binaryLine.erase (0,n); //remove that substring break; //break loop } } } */ //print decoded value cout<<"Decoded value: "< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started