Huffman codes Data structures

Huffman codes Data structures

Please accept if you know C++

You will be generating Huffman codes to compress a given string. A Huffman code uses a set of prefix code to compress the string with no loss of data (lossless). David Huffman developed this algorithm in the paper “A Method for the Construction of Minimum-Redundancy Codes”


Huffman codes Data structures

For Programming Project 3, you will be generating Huffman codes to compress a given string. A Huffman code uses a set of prefix code to compress the string with no loss of data (lossless). David Huffman developed this algorithm in the paper “A Method for the Construction of Minimum-Redundancy Codes” (…)

A program can generate Huffman codes from a string using the following steps:

Generate a list of the frequency in which characters appear in the string using a map
Inserting the characters and their frequencies into a priority queue (sorted first by the lowest frequency and then lexicographically)
Until there is one element left in the priority queue
Remove two characters/frequencies pairs from the priority queue
Turn them into leaf nodes on a binary tree
Create an intermediate node to be their parent using the sum of the frequencies for those children
Put that intermediate node back in the priority queue

Huffman codes Data structures

The last pair in the priority queue is the root node of the tree
Using this new tree, encode the characters in the string using a map with their prefix code by traversing the tree to find where the character’s leaf is. When traversal goes left, add a 0 to the code, when it goes right, add a 1 to the code
With this encoding, replace the characters in the string with their new variable-length prefix codes
if it the node is a leaf (external node) then you add a L plus the character to the serialize tree string
if it is a branch (internal node) then you add a B to the serialize tree string
Huffman.cpp (Your written HuffmanTree class)
Huffman.hpp (Your written HuffmanTree class)
HeapQueue.cpp (The given Heap Priority Queue using an array/vector class)
HeapQueue.hpp (The given Heap Priority Queue using an array/vector class)
HuffmanBase.cpp (The provided base class and helper class)
HuffmanBase.hpp (The provided base class and helper class)
PP3Test.cpp (Test file)
TestStrings.hpp (Test file)
catch.hpp (Catch2 Header)

Huffman codes Data structures

In addition to the compress string, you will need to be able to serialize the tree. Without the serialized version of the Huffman tree, you will not be able to decompress the Huffman codes. Tree serialization will organize the characters associated with the nodes using post order. During the post order when you visit a node,

For decompression, two input arguments will be needed. The Huffman Code that was generated by your compress method and the serialized tree string from your serializeTree method. Your Huffman tree will have to be built by deserializing the tree string by using the leaves and branches indicators. After you have your tree back, you can decompress the Huffman Code by tracing the tree to figure out what variable length codes represent actual characters from the original string.

So, for example, if we are compressing the string “if a machine is expected to be infallible it cannot also be intelligent”:

Our compress algorithm would generate the following codes for the characters:



Prefix Code























































Our code would be:


And our serialize tree would look like:


You will need to create one class for this project: HuffmanTree for the compression, decompression, and serialization that uses a linked binary tree. You are given a Heap-based Priority Queue for the sorting. You are allowed to use the STL map, vector, and stack, but not the STL priority queue.

Abstract Class Methods
std::string compress(const std::string inputStr)

Compress the input string using the method explained above. Note: Typically we would be returning a number of bits to represent the code, but for this project we are returning a string

std::string serializeTree() const

Serialize the tree using the above method. We do not need the frequency values to rebuild the tree, just the characters on the leaves and where the branches are in the post order.

std::string decompress(const std::string inputCode, const std::string serializedTree)

Given a string created with the compress method and a serialized version of the tree, return the decompressed original string

Other things in Huffman hpp/cpp
To simplify the process, I have given the full interface and implementation for a class called HuffmanNode. This class has all the basics for a tree node (leaf, branch, root, data members for linking, accessor) and also includes a comparator class for use with the heap. You should not need to alter any of the code for this node.


Below are some examples of how your code will run

HuffmanTree t;

string test = “It is time to unmask the computing community as a Secret Society for the Creation and Preservation of Artificial Complexity”;

/* 1000101011110000101001100110000010111011001110111101111100011001001011010010000111100111001001110110111101011001010101011111001100000111000010110111101011001000101111100011000011111111110010110100110010111010011111101111001001110011110100111101111110000111001111111111010101110110100110011100100111011010011001001110010101100010110011110010100111000001110100000001001110101001110010010001100101011000101100111101011101011110100010001000110001010110001111000001011001011101001101011001010101010010111101000111000011111111 */

string code = t.compress(test);


string tree = t.serializeTree();

/* It is time to unmask the computing community as a Secret Society for the Creation and Preservation of Artificial Complexity */

string orig = t.decompress(code, tree);


Please submit complete projects as zipped folders. The zipped folder should contain:

And any additional source and header files needed for your project.


For the encoding step where you translate characters using your Huffman Tree, this is essentially a preordering of the tree and can be done recursively.

Remember, when you are deserializing, you are going from post ordering back to the full tree. This is very similar to the postfix to infix conversion you did in PP2, but now building a tree instead of an expression.

For decoding the characters, you just follow the tree down the branches until you hit the leaf with the character, adding a zero for a left move and adding a 1 for a right move.

I suggest implementing a recursive method to destroy nodes for your destructor.

For the branching nodes, I suggest using the null character just to hold a spot since this should not be popping up in text you are compressing.

The test cases are based on using the standard library header

, not the unordered map.

Additional resources:


Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
The price is based on these factors:
Academic level
Number of pages
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
Open chat
You can contact our live agent via WhatsApp! Via + 1 3234125597

Feel free to ask questions, clarifications, or discounts available when placing an order.