Skip to content

About

Huffman Encoding Algorithm — C++ honors project (Hash Table → Sorted Linked List → Binary Tree) with an interactive web demo

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

Repository files navigation

Huffman Encoding Algorithm

🌳 Huffman Encoding Algorithm

CIS 22C — Data Abstractions and Structures · Honors Project

A full-pipeline C++ implementation of the Huffman Encoding Algorithm, built on three fundamental data structures: a direct-address Hash Table, a Sorted Singly Linked List, and a Binary Tree.

Language Course Status Live Demo

Type any text and watch the frequency table, Huffman tree, codes, and live compression build in real time — right in your browser.


📖 Overview

This project implements a variation of the Huffman Encoding Algorithm in C++ as part of the CIS 22C Honors curriculum at De Anza College. It reads any input text, counts character frequencies, builds an optimal Huffman Tree, and exposes a full menu-driven interface for encoding and decoding text and files using variable-length, prefix-free binary codes.

The program is built on three independently designed data structures, each owning a distinct stage of the pipeline:

Stage Data Structure Responsibility
Frequency Analysis HashTable Count occurrences of each character in O(1)
Priority Ordering SortedLinkedList Maintain leaf nodes sorted by ascending frequency
Code Generation TreeNode (Binary Tree) Build the Huffman tree via greedy pair-merging

Design principle: The architecture uses composition and pointer-based relationships throughout — no inheritance. Each class has a single, well-defined responsibility, and data flows explicitly between them.

📐 For the full algorithm walkthrough — class diagrams, the step-by-step "Mississippi" example, and complexity notes — see Huffman.md.


▶ Try It Two Ways

1. In your browser (no install)

The interactive demo runs the exact same algorithm, ported to JavaScript. Type text and watch every phase build live — frequencies, the animated tree, codes, encoding, and decoding.

2. Build and run the C++ program

# Compile (C++17)
g++ -std=c++17 -I Header_Files main.cpp Cpp_Files/*.cpp -o huffman

# Run the menu-driven program
./huffman

Then choose option 1 to enter text manually (try Mississippi), or option 2 to read from a file such as testdata.txt.

Run the unit tests

g++ -std=c++17 -I Header_Files test_huffman.cpp Cpp_Files/*.cpp -o huffman_test
./huffman_test

🏗 Architecture

flowchart LR
    A["📄 Input Text"] --> B["HashTable\nCount Frequencies"]
    B --> C["SortedLinkedList\nLeaf TreeNodes"]
    C --> D["Greedy Merge\nmin₁ + min₂"]
    D -->|"repeat until\n1 node remains"| D
    D --> E["Huffman Tree\nroot TreeNode*"]
    E --> F["Recursive Traversal\n0 = left · 1 = right"]
    F --> G["HashTable\nStores Codes"]
    G --> H1["✅ Encode\nText / Files"]
    G --> H2["✅ Decode\nBinary → Text"]
Loading

🖥 Program Menu

========== Huffman Encoding Program ==========
  -- Setup --
  1. Enter text manually
  2. Read text from file
  -- Menu Options --
  3. Print character weights (frequencies)
  4. Print Tree (Right-Root-Left, indented)
  5. Print Huffman codes for all characters
  6. Enter one character  - print its Huffman code
  7. Enter a word         - print ASCII binary and Huffman code
  8. Enter encoded string - decode and display
  9. Encode a text file and save to another file
 10. Decode an encoded file and save to another file
  0. Quit

📁 File Structure

CIS22C_HonorProject/
│
├── main.cpp                 # Entry point & program startup
├── test_huffman.cpp         # Unit test suite
├── testdata.txt             # Sample input text
│
├── Header_Files/            # ── Class declarations ──
│   ├── MenuApp.h            #    Menu driver & algorithm orchestration
│   ├── FileManager.h        #    File encoding & decoding I/O
│   ├── HashEntry.h          #    Single hash table cell (char + freq + code)
│   ├── HashTable.h          #    256-slot direct-index hash table
│   ├── Freq.h               #    Character + weight data object
│   ├── TreeNode.h           #    Binary tree node (left*, right*, Freq)
│   ├── ListNode.h           #    Linked list node (TreeNode* + next*)
│   └── SortedLinkedList.h   #    Sorted list of TreeNodes
│
├── Cpp_Files/               # ── Class implementations ──
│   └── *.cpp                #    (one .cpp per header above)
│
├── Outputs/                 # Sample program runs & encoded/decoded files
│
├── docs/                    # ── Interactive web demo (GitHub Pages) ──
│   ├── index.html          #    The interactive Huffman visualizer
│   ├── huffman.js           #    Algorithm engine (faithful JS port)
│   ├── app.js               #    UI rendering & tree visualization
│   └── style.css            #    Styling
│
├── assets/
│   └── banner.svg           # Animated README banner
│
├── Huffman.md               # Full algorithm walkthrough & class diagrams
└── README.md

🎯 Real-World Applications

Huffman encoding is not just an academic exercise — it is embedded in formats you use daily:

Application Where Huffman Is Used
JPEG / PNG Entropy coding stage of image compression
MP3 / AAC Audio stream entropy encoding
ZIP / DEFLATE File archiving (Huffman + LZ77 in combination)
PDF Internal stream compression
HTTP/2 Header compression via HPACK

👥 Team

Member Contribution
Oak HashEntry, HashTable, MenuApp, FileManager, main.cpp — application layer & hash table design; interactive web demo
Benn Freq, TreeNode, ListNode, SortedLinkedList — tree and list layer

CIS 22C · De Anza College · Honors Section


Built with 🌳 and a healthy respect for pointer arithmetic.

About

Huffman Encoding Algorithm — C++ honors project (Hash Table → Sorted Linked List → Binary Tree) with an interactive web demo

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages