CS 411 Fall 2025 > Assignment 6 (Huffman Codes)
CS 411 Fall 2025
Assignment 6 (Huffman Codes)
Assignment 6 is due at 5 pm Tuesday, November 25. It is worth 85 points.
Procedures
This is, optionally, a group assignment. You may work in a group of two, if you wish. Each group only needs to turn in a single copy of the assignment. Under normal circumstances, each group member will receive the same grade on the assignment.
Turn in your answer to the exercise below on the UA Canvas site, under Assignment 6 for this class.
- Your submission must consist of
the source code for Exercise A
(files
huffcode.hppandhuffcode.cpp). - I may not look at your homework submission immediately. If you have questions, e-mail me.
If you work in a group:
- The assignment files must contain the names of both group members.
- One student submits the assignment as usual, on Canvas.
- The other student submits an empty assignment (no attached source or header files) on Canvas, with a note indicating whom the assignment was done with.
Exercises (A only, 85 pts total)
Exercise A — Huffman Codes
Purpose
In this exercise, you will implement Huffman coding. Given character weights, your package will be able to construct a Huffman code, which it can then use to encode and decode strings.
Instructions
Write a class HuffCode,
defined in files huffcode.hpp
and huffcode.cpp.
Class HuffCode
should include the following
public member functions.
- Default constructor,
copy constructor,
copy assignment,
destructor.
All of these should do the usual things.
- So you can use the compiled-generated versions, as long as you do not do anything silly like declaring a data member that is a pointer.
- Member function
setWeights, prototyped as follows (remember, this and later functions are all class members):[C++]
void setWeights(const std::unordered_map<char, int> & theweights);
Function
setWeightssets the character weights. It is given anunordered_map, as above; the value corresponding to a character is its weight. Characters whose weight is not specified will not occur in any texts to be encoded. - Member function
encode, prototyped as follows:[C++]
std::string encode(const std::string & text) const;
Given a string of characters, each of which has already had its weight defined (using
setWeights), this returns a string of zero ('0') and one ('1') characters, representing the given text, encoded using an appropritate Huffman code. See Examples, below, for an example. - Member function
decode, prototyped as follows:std::string decode(const std::string & codestr) const;
Given a string of zero (
'0') and one ('1') characters, encoded using the Huffman code generated by the class, this returns the corresponding text. In particular, if the return value ofencodeis passed to this function, then it will return the argument that was passed toencode. See Examples, below, for an example.
Your code may generate and use any Huffman code that is correct for the given weights.
Examples
[C++]
HuffCode h; std::unordered_map<char, int> w; w['a'] = 1; w['b'] = 2; w['c'] = 3; h.setWeights(w); // I will assume the code is // a: 00 // b: 01 // c: 1 // There are other possible Huffman codes for the above weights; // these would result in different behavior below. std::cout << h.encode("aca") << std::endl; // Above prints "00100" std::cout << h.decode("00100") << std::endl; // Above prints "aca" std::cout << h.decode("01000111") << std::endl; // Above prints "babcc"
Skeleton Files
I have provided unfinished “skeleton” files
huffcode.hpp
and
huffcode.cpp,
in the ZIP file
huffcode.zip.
You may use these as the basis for your own work, if you wish.
Test Program
A test program
is available:
huffcode_test.cpp.
If you compile and run this program (unmodified!) with your code,
then it will test
whether your code works properly.
Do not turn in the test program.
Notes
- Coding standards are as for Assignment 2.
- Your code may be tested with additional input beyond that given in the posted test programs.