Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Develop a Huffman Tree that uses a template class. Implement the template class in such a way as to store any given sentence of text
Develop a Huffman Tree that uses a template class. Implement the template class in such a way as to store any given sentence of text and then traverse the tree to print the sentence back to the console.
Can't get the solution below to work, this solution was copied from a Chegg answer and that was it, no .h files were in that post:
#include "book.h" #include #include // Include definition for heaps // to keep track of the forest of huffman trees during the // building operation #include "heap.h" #define MAXCODELEN 20 // Max length of a huffman code #define CODETABLELEN 100 // Maximum number of codes storable // CodeTable maps objects to their associated codes. template class CodeTable { private: E* obs; // Objects char** codes; // Associated code values int currsize; // Current number of objects in table int maxsize; // Max objects permitted in table public: CodeTable(int size) { obs = new E[size]; codes = new char*[size]; for (int i = 0; i ostream& operator << (ostream& s, HuffNode* z) { if (z->isLeaf()) return s << ((LeafNode*)z)->val(); else return s << z->weight(); } #include "hufftree.h" // Space for the heap's array HuffTree** TreeArray = NULL; // Overload for the HuffTree << operator template ostream& operator << (ostream& s, HuffTree* z) { return s << z->weight(); } // Comparator for the heap class minTreeComp { public: static bool prior(HuffTree* x, HuffTree* y) { return x->weight() < y->weight(); } }; // Read the list of frequencies, make the forest, and set the // list of entries into the code table. int read_freqs(CodeTable* ct, FILE* fp) { // Read a list of strings and frequencies from standard input, // building a list of Huffman coding tree nodes char buff[100]; char buff2[100]; char *ptr; char *ptr2; int freq; Assert(fgets(buff, 99, fp) != NULL, // Read number of chars "Couldn't read character count"); ptr = buff; Assert(isdigit(*ptr) != 0, "Must be a digit here."); int count = atoi(ptr); TreeArray = new HuffTree*[count]; for (int i=0; i"); Assert((fp = fopen(argv[1], "rt")) != NULL, "No such file"); // Now, read in the list of frequencies, and initialize the // forest of Huffman trees. cout << "Read frequencies "; int count = read_freqs(theTable, fp); // forest->print(); // Now, build the tree. cout << "Build the tree "; theTree = buildHuff(TreeArray, count); // Now, output the tree, which also creates the code table. cout << "Output the tree "; buildcode(theTree->root(), theTable, prefix, 0, total); cout << "Average code length is " << total/(double)theTree->weight() << " "; // Finally, do the encode/decode commands to test the system. do_commands(theTree, theTable, fp); return 0; }
Cannot open source file for the following: #include "book.h" #include "heap.h" #include "huffnode.h" #include "hufftree.h" is one of the many error messages.
Any idea, using Visual Studio Express 2015?
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