|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Huffman encoding/decoding demonstration with tree visualization. More...
#include <cstring>#include <iostream>#include <fstream>#include <tclap/CmdLine.h>#include <Huffman.H>#include <huffman_btreepic.H>Go to the source code of this file.
Functions | |
| size_t | read_and_encode (char *str, Huffman_Encoder_Engine &huffman_engine, BitArray &bit_stream) |
| Encode string and return encoded bit stream length. | |
| void | write_verse (const char *text, const string &filename) |
| Write poem in LaTeX verse format. | |
| int | main (int argc, char *argv[]) |
Variables | |
| char | poem [] |
Huffman encoding/decoding demonstration with tree visualization.
This example demonstrates Huffman compression, one of the most important lossless data compression algorithms. It encodes text using variable-length codes based on character frequencies, achieving optimal compression for the given frequency distribution. Huffman coding is the foundation of many modern compression algorithms.
Goal: Compress text by assigning shorter codes to frequent characters
Challenge:
Huffman coding is a greedy algorithm that assigns shorter codes to more frequent characters and longer codes to less frequent ones. It builds a binary tree where:
Count character frequencies in input:
Time: O(n) where n = input length
Create min-heap of character nodes:
Time: O(k log k) where k = number of unique characters
Repeatedly merge two lowest-frequency nodes:
Time: O(k log k) - k extractions from heap Result: Single tree with all characters as leaves
Traverse tree to assign binary codes:
Time: O(k) - visit each leaf once
Replace characters with their codes:
Time: O(n) - process each character
The example shows:
Huffman coding is optimal for the given frequency distribution:
By default, encodes a poem by Federico García Lorca: "Muerte de Antoñito el Camborio"
This demonstrates:
Decoding uses the same tree:
Time: O(n) where n = encoded length
✅ Optimal: Best compression for given frequencies ✅ Fast: O(n + k log k) encoding time ✅ Simple: Easy to understand and implement ✅ Adaptive: Can adapt to frequency changes
❌ Two passes: Need to analyze frequencies first ❌ Tree overhead: Must store tree for decoding ❌ Fixed frequencies: Doesn't adapt during encoding
Definition in file write_huffman.C.
| int main | ( | int | argc, |
| char * | argv[] | ||
| ) |
Definition at line 397 of file write_huffman.C.
References Aleph::code(), Aleph::destroyRec(), Aleph::DynList< T >::empty(), huffman_to_btreepic(), Aleph::maps(), output, output_ptr, poem, read_and_encode(), and write_verse().
| size_t read_and_encode | ( | char * | str, |
| Huffman_Encoder_Engine & | huffman_engine, | ||
| BitArray & | bit_stream | ||
| ) |
Encode string and return encoded bit stream length.
Definition at line 354 of file write_huffman.C.
References Aleph::maps().
Referenced by main().
| void write_verse | ( | const char * | text, |
| const string & | filename | ||
| ) |
Write poem in LaTeX verse format.
Definition at line 365 of file write_huffman.C.
References Aleph::maps().
Referenced by main().
| char poem[] |
Definition at line 292 of file write_huffman.C.
Referenced by main().