Huffman Coding
Huffman coding builds an optimal prefix-free binary code from a text's symbol frequencies: frequent symbols get short codes, rare ones long. This tool computes the frequency table, builds the Huffman tree, shows each symbol's code, encodes the text to a bit string, and decodes it back. Everything runs locally in your browser.
Input text
Code table
Encoded output
Statistics
Huffman's algorithm repeatedly merges the two lowest-frequency nodes into a parent whose frequency is their sum, until one tree remains; walking it with left = 0, right = 1 yields the codes. By construction no code is a prefix of another (so decoding is unambiguous), and the weighted code length Σ freqᵢ · |codeᵢ| is minimal among prefix codes. The Shannon entropy H = −Σ pᵢ log₂ pᵢ is the information-theoretic lower bound; Huffman's average length satisfies H ≤ L < H + 1 bits/symbol. A single distinct symbol is given the code 0. Pairs with the Run-Length Encoding and Burrows-Wheeler Transform tools. Everything runs locally — nothing leaves your browser.