Question: 1 9 . 1 3 Lab 1 0 Huffman Tree Puzzler Overview You will write a function that given an in - order and a

19.13 Lab 10 Huffman Tree Puzzler
Overview
You will write a function that given an in-order and a level-order traversal builds the binary tree.
This will complete a program that reads three text files given as command line parameters. The first file is an inorder
traversal of a Huffman code tree and the second parameter is the levelorder traversal of the same tree. The third file is the
encoded text, given as ASCII 0s and 1s.
All but the function are provided for you.
Details
Your program will:
Read the input files (DONE FOR YOU)
Compute the Huffman code tree from the two traversals. (TODO)
Decode the text, writing the output to standard output (cout).(DONE FOR YOU)
The format of the in-order and level-order traversals will be integer values separated by whitespace. The leaves of the tree
will be values 128, representing the ASCII value of the letter. The internal nodes of the tree will be values 128 and greater.
Stated another way: the smaller values are characters in the leaf nodes and the larger values are non-leaf nodes.
Examples
An example run:
./decode inorder0.txt levelorder0.txt encoded0.txt
inorder0.txt:
{:[66,129,76,128,77,130,65]:}
Interpreted as: 'B'129'L'128'M'130'A'
levelorder0.txt:
{:[130,129,65,66,128,76,77]:}
Interpreted as: 130129 A B 128 LM
encoded0.txt:
101010010111
Output:
ALABAMA
The Huffman tree:
This project given
#include
#include
#include
using namespace std;
class HuffNode {
public:
int val;
HuffNode *left;
HuffNode *right;
HuffNode(int v =0, HuffNode *l = nullptr, HuffNode *r = nullptr){
val = v;
left = l;
right = r;
}
};
//TODO: All you have to do is build the tree ... complete this function
HuffNode *createTreeFromInAndLevel(const vector &inorder, const vector &levelorder){
//build the tree
//level order gives you the root
//inorder gives you left/right of root
//construct vectors for left / right and recursively build the tree
}
/*
The rest is complete.
*/
void decode(HuffNode *q, HuffNode *root, string encoded){
//if (q->val 128){
if (q->left == nullptr && q->right == nullptr){
char ascii =(char)q->val;
cout ascii;
q = root;
}
if (encoded.length()==0) return;
char dir = encoded[0];
if (dir =='0'){
decode(q->left, root, encoded.substr(1));
}
else if (dir =='1'){
decode(q->right, root, encoded.substr(1));
}
}
int main(int argc, char *argv[]){
ifstream inorderFile(argv[1]);
ifstream levelorderFile(argv[2]);
ifstream encodedFile(argv[3]);
if (inorderFile.fail()|| levelorderFile.fail()|| encodedFile.fail()){
cout "Could not open input file(s)" endl;
return 1;
}
vector inorder;
vector levelorder;
string encoded;
int val;
char ch;
//read the inorder file
while (inorderFile >> val){
inorder.push_back(val);
}
//read the level order file
while (levelorderFile >> val){
levelorder.push_back(val);
}
//read the encoded file
while (encodedFile >> ch){
encoded += ch;
}
//close the files
inorderFile.close();
levelorderFile.close();
encodedFile.close();
//build the decoding tree
HuffNode *root = createTreeFromInAndLevel(inorder, levelorder);
//decode the message
HuffNode *q = root;
for (int i =0; i (int)encoded.length(); i++){
//read the encoded bit
char dir = encoded[i];
//zero == move left
if (dir =='0'){
q = q->left;
}//1== move right
else if (dir =='1'){
q = q->right;
}
//if leaf node print the char and return to root
if (q->val 128){
char ascii =(char)q->val;
cout ascii;
q = root;
}
}
//cout endl;
return 0;
}
1 9 . 1 3 Lab 1 0 Huffman Tree Puzzler Overview

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!