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.

If you work in a group:

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.

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