Question: 1 9 . 1 3 Lab 1 0 Huffman Tree Puzzler Overview You will write a function that given an in - order and a
Lab Huffman Tree Puzzler
Overview
You will write a function that given an inorder and a levelorder 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 s and s
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 coutDONE FOR YOU
The format of the inorder and levelorder traversals will be integer values separated by whitespace. The leaves of the tree
will be values representing the ASCII value of the letter. The internal nodes of the tree will be values and greater.
Stated another way: the smaller values are characters in the leaf nodes and the larger values are nonleaf nodes.
Examples
An example run:
decode inordertxt levelordertxt encodedtxt
inordertxt:
::
Interpreted as: BLMA
levelordertxt:
::
Interpreted as: A B LM
encodedtxt:
Output:
ALABAMA
The Huffman tree:
This project given
#include
#include
#include
using namespace std;
class HuffNode
public:
int val;
HuffNode left;
HuffNode right;
HuffNodeint v 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 createTreeFromInAndLevelconst vector &inorder, const vector &levelorder
build the tree
level order gives you the root
inorder gives you leftright of root
construct vectors for left right and recursively build the tree
The rest is complete.
void decodeHuffNode q HuffNode root string encoded
if qval
if qleft nullptr && qright nullptr
char ascii charqval;
cout ascii;
q root;
if encodedlength return;
char dir encoded;
if dir
decodeqleft, root, encoded.substr;
else if dir
decodeqright, root, encoded.substr;
int mainint argc, char argv
ifstream inorderFileargv;
ifstream levelorderFileargv;
ifstream encodedFileargv;
if inorderFilefail levelorderFile.fail encodedFile.fail
cout "Could not open input files endl;
return ;
vector inorder;
vector levelorder;
string encoded;
int val;
char ch;
read the inorder file
while inorderFile val
inorder.pushbackval;
read the level order file
while levelorderFile val
levelorder.pushbackval;
read the encoded file
while encodedFile ch
encoded ch;
close the files
inorderFile.close;
levelorderFile.close;
encodedFile.close;
build the decoding tree
HuffNode root createTreeFromInAndLevelinorder levelorder;
decode the message
HuffNode q root;
for int i ; i intencodedlength; i
read the encoded bit
char dir encodedi;
zero move left
if dir
q qleft;
move right
else if dir
q qright;
if leaf node print the char and return to root
if qval
char ascii charqval;
cout ascii;
q root;
cout endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
