CT033

Huffman Encoding Tree

HardAcceptance: 0.0%

Huffman coding encodes characters based on their frequency. Each letter is assigned a variable-length binary string, where shorter lengths correspond to more common letters. A binary tree is built such that the path from root to any leaf maps to a character: descending left = 0, descending right = 1. Given character frequencies, build a Huffman tree and determine the encoding mapping for each character. Output each character and its binary encoding, sorted alphabetically by character.

Example 1:

Input: 4 a 3 c 6 e 8 f 2
Output: a 001 c 01 e 1 f 000

Example 2:

Input: 3 a 1 b 1 c 1
Output: a 0 b 10 c 11

Constraints:

1 <= number of distinct characters <= 256 1 <= frequency <= 10^6

Tags:

heap tree greedy
Loading...
Test Cases:No test cases
No test cases available.