Implementing Huffman encoding in C++
This medium-difficulty coding problem asks you to implement the full Huffman compression pipeline in C++: frequency counting, greedy binary-tree construction, code assignment, and string encoding. It tests both algorithmic understanding and careful implementation of a classical algorithm.
The core challenge is building the Huffman tree correctly under a specific tie-breaking rule. When two nodes have equal frequency, you must track the minimum character in each subtree and use alphabetical order to decide left/right placement. This requires maintaining both frequency and subtree metadata as you greedily merge nodes. A priority queue or similar data structure is essential for efficiently selecting the two minimum-frequency nodes at each step.
- Greedy algorithm design and correctness
- Binary tree construction and traversal
- Priority queues and custom comparators
- String encoding and bit manipulation
- Handling edge cases (single character, ties in frequency)