Instead of just reading about greedy algorithms and trees in textbooks, I built a working text compressor in C using the Huffman Coding algorithm!
The program takes a list of letters and counts how often they appear. It puts them into a custom-built, self-sorting list called a "Min-Heap" and links them together to form a binary Huffman Tree. From this tree, it creates unique, short binary codes (0s and 1s) for each letter. Letters that show up a lot get very short codes, which shrinks the overall file size!
I built this project to challenge myself with tougher data structures and see algorithms work in real life. It helped me learn:
- Advanced Trees and Heaps: How to build and handle binary trees and sorting queues from scratch in C without using pre-made libraries.
- How Compression Works: Understanding the actual programming logic behind shrinking a file without messing up or losing any data.
- Optimizing Space: Learning how file encoders look at data streams to map out characters efficiently.
- Custom Min-Heap (Priority Queue): I built a sorting queue from scratch using arrays to track the characters with the lowest frequencies automatically.
- Smart Binary Codes: It assigns short codes to common letters (like
0) and longer codes to rare letters (like1101), which drastically cuts down memory usage. - Tree Traversal: Uses a clean recursive loop to walk down the tree branches and print out the final binary shortcuts for each character.
-
Open your terminal inside this folder and compile the code: gcc huffman.c -o huffman
-
Run the program: ./huffman
- C Language
- GCC Compiler
- Core Concepts: Binary Trees, Min-Heaps, and Greedy Algorithms
Shafin Alam GitHub: shafinalam07