Greedy Algorithms · Prefix Codes

Huffman Coding Visualizer

Build an optimal prefix code by repeatedly joining the two least frequent symbols. Step through each merge, inspect the resulting binary tree, and see how frequent symbols receive shorter codes.

Step merges the two least frequent available nodes. Reset restarts the chosen frequencies. Finish the tree to enable encoding.

Merge: 0 / 5
Total frequency: 100
Weighted path length: —

Huffman tree

100%
Available nodeCreated by a mergeLeft edge = 0 · Right edge = 1

Merge queue

Each step removes the two nodes with the smallest weights and replaces them with a parent whose weight is their sum.

Code table

SymbolFrequencyCodeBits

Encode a message

Press Encode to map symbols to bits.

Use only symbols in the frequency set. Codewords are prefix-free, so decoding needs no separator.

The queue starts with one leaf for each symbol. Select Step or Play to build the tree.