Huffman Tutorial Part 1

05 Jul 2016

Tags: c++ Huffman encoder compression io


Let’s start off with a basic setup, which includes a nice directory structure and a neat Makefile. What are you looking at me for? Alright, here is my setup:

huffman/                        # Root directory
├── dec/                        # Source for decoder
├── enc/                        # Source for encoder
├── obj/                        # Object files
└── texts/                      # Test files for encoding
    └── t8.shakespeare.txt      # Shakespeare :)
├── Makefile

Pretty straight-forward. Just make it easier for yourself.


Here are some user stories for the project:

File Reading

You know that that’s one of the prerequisites, right? Here is the gist of it.

#include <fstream>

using namespace std;

int main() {
    ifstream ifile("infile.txt");

    if (ifile.good()) {
        char tmp;
        while (, 1)) {
            // Do something

    return 0;

Disclaimer: The above code is just a snippet, and is not guaranteed to work.

Assignment for This Part

(:star: denotes a challenging task. :star2: denotes an even more challenging task.)