Skip to content
Laviyx Learn

Try

Mini project

File Compressor with Huffman Coding in C++

A command-line tool in C++ that compresses and decompresses any file using Huffman coding, with a priority queue, bit packing and a check that the restored file is identical.

Advanced 2–3 weeks 1–2 studentsUpdated

C++17STL priority_queueBinary file I/OCMake (optional)

The problem

Compression is everywhere: ZIP files, images, web pages sent over the network. Huffman coding is one of the classic ideas behind it: common bytes get short codes and rare bytes get long ones. Building a real compressor teaches trees, greedy algorithms, priority queues and working with raw bits and binary files.

Features to build

  • Compress any file: compressor -c input.txt output.huf
  • Decompress back: compressor -d output.huf restored.txt
  • Count byte frequencies and build the Huffman tree with a min-heap
  • Store the frequency table in the file header so the tree can be rebuilt
  • Pack the codes into bits, and record how many padding bits the last byte has
  • Print the original size, compressed size and compression ratio
  • Handle edge cases: empty files and files with only one distinct byte

Build plan

  1. 1

    Count frequencies

    Read the file in binary mode into a buffer and count how often each of the 256 byte values appears.

  2. 2

    Build the tree

    Push a leaf node for each byte that appears into a priority_queue ordered by frequency. Repeatedly pop the two smallest, join them under a new node, and push it back until one node remains.

  3. 3

    Make the codes

    Walk the tree, adding 0 for left and 1 for right, and store each byte's code in a table.

  4. 4

    Write the compressed file

    Write a header with the frequency table and the original size, then pack the codes into bytes with bit operations.

  5. 5

    Decompress and verify

    Rebuild the same tree from the header, read bits and walk the tree until a leaf, and stop after the original number of bytes. Compare the restored file with the original byte by byte.

Learn these first

Each part of the project is taught in one of these free lessons.

Make it stand out

  • Compare results on text, source code, images and ZIP files, and explain the differences
  • Use canonical Huffman codes to store a smaller header
  • Compress a whole folder into one archive
  • Add a CRC32 checksum to detect corrupted files

Viva and interview questions

Try to answer each one aloud before you open it.

  • Why can Huffman codes be decoded without separators?

    They are prefix codes: no code is the start of another code, because every byte sits at a leaf of the tree. While reading bits, the decoder reaches exactly one leaf for each code, so there is never any doubt where a code ends.

  • Why is Huffman coding a greedy algorithm, and is it optimal?

    At each step it joins the two least frequent nodes, the locally best choice. It is proven to give the shortest possible average code length for coding one byte at a time with these frequencies.

  • What is the time complexity?

    Counting is O(n) for n bytes. Building the tree is O(k log k) for k distinct bytes, at most 256. Encoding and decoding are O(n) plus the total number of bits. So overall it is linear in the file size.

  • Why does a JPEG or ZIP file barely shrink, or even grow?

    Those files are already compressed, so their bytes are spread out almost evenly and there are no common bytes to give short codes. The header then adds a little extra size.

  • Why must the header store the original size?

    The last byte is padded with extra bits. Without knowing how many bytes to restore, the decoder could read the padding as extra characters.

What to put in your project report

  1. Abstract: the problem, your solution and the result in one paragraph.
  2. Introduction and problem statement: who has the problem and why it matters.
  3. Requirements: the features (functional) and qualities such as speed and security (non-functional).
  4. Design: architecture diagram, database tables or data flow, and screen layouts.
  5. Implementation: the main modules and the most interesting code, explained.
  6. Testing: the test cases you ran, with inputs, expected and actual results.
  7. Results: screenshots or numbers that show the project working.
  8. Conclusion and future scope: what you achieved and what you would add next.

Your college may give its own format. Follow that first, and use this list to check nothing is missing.

All project ideas →