DevTools Logo

Huffman & Shannon-Fano Tree Visualizer

Huffman & Shannon-Fano Tree

Compare optimal prefix codes, entropy, and compression entirely in your browser.

Runs in your browser
Symbols are Unicode code points. Huffman merges the two lowest-frequency nodes, ties broken by the smallest character (by code point order); the first node taken gets bit 0. Shannon-Fano sorts by frequency descending then character, and splits where the two halves differ least (first such split on ties); the upper half gets bit 0.
Try an example

Source text

1 line · 43 chars

Code table

43 characters · 27 symbols
Savings compare the code length with the UTF-8 size of the text. They do not include the cost of storing the code table, so very short inputs would grow in practice.
Entropy H
4.385 bits/symbol
Avg. Huffman
4.465 bits/symbol
Avg. Shannon-Fano
4.465 bits/symbol
Savings (Huffman)
44.2%
Huffman bits
192
Shannon-Fano bits
192
UTF-8 baseline
344 bits
Kraft sum
1.000 / 1.000
SymbolFreq.HuffmanShannon-Fano
␠8110000
a110110010010
b1101101100110
c1101110100111
d110111110100
e310100100
f1111000101010
g1111001101011
h2111010101
i111110010110
j1111101101110
k1111110101111
l111111111000
m10000011001
n10000111010
o4001001
p100010110110
q100011110111
r201000110
s10101011100
t201100111
u201111000
v101011111010
w110000111011
x11000111110
y110010111110
z110011111111

Huffman tree

● 43
0─ ● 16
0─ ● 8
0─ ● 4
0─ ● 2
0─ m · 1
1─ n · 1
1─ ● 2
0─ p · 1
1─ q · 1
1─ o · 4
1─ ● 8
0─ ● 4
0─ r · 2
1─ ● 2
0─ s · 1
1─ v · 1
1─ ● 4
0─ t · 2
1─ u · 2
1─ ● 27
0─ ● 11
0─ ● 4
0─ ● 2
0─ w · 1
1─ x · 1
1─ ● 2
0─ y · 1
1─ z · 1
1─ ● 7
0─ e · 3
1─ ● 4
0─ ● 2
0─ a · 1
1─ b · 1
1─ ● 2
0─ c · 1
1─ d · 1
1─ ● 16
0─ ␠ · 8
1─ ● 8
0─ ● 4
0─ ● 2
0─ f · 1
1─ g · 1
1─ h · 2
1─ ● 4
0─ ● 2
0─ i · 1
1─ j · 1
1─ ● 2
0─ k · 1
1─ l · 1

Shannon-Fano tree

● 43
0─ ● 21
0─ ● 12
0─ ␠ · 8
1─ o · 4
1─ ● 9
0─ ● 5
0─ e · 3
1─ h · 2
1─ ● 4
0─ r · 2
1─ t · 2
1─ ● 22
0─ ● 11
0─ ● 5
0─ u · 2
1─ ● 3
0─ a · 1
1─ ● 2
0─ b · 1
1─ c · 1
1─ ● 6
0─ ● 3
0─ d · 1
1─ ● 2
0─ f · 1
1─ g · 1
1─ ● 3
0─ i · 1
1─ ● 2
0─ j · 1
1─ k · 1
1─ ● 11
0─ ● 5
0─ ● 2
0─ l · 1
1─ m · 1
1─ ● 3
0─ n · 1
1─ ● 2
0─ p · 1
1─ q · 1
1─ ● 6
0─ ● 3
0─ s · 1
1─ ● 2
0─ v · 1
1─ w · 1
1─ ● 3
0─ x · 1
1─ ● 2
0─ y · 1
1─ z · 1

    Examples

    Uniform four-symbol input

    Input
    abcd
    Output
    codes: a=00 b=01 c=10 d=11, entropy 2.0, encodedBits 8, originalBits 32, average length 2

    Equal frequencies produce fixed-length 2-bit codes; entropy equals the achieved rate, so no further prefix-code gain is possible.

    Single repeated symbol

    Input
    aaa
    Output
    entries: [{ char: "a", frequency: 3, probability: 1, huffmanCode: "0", shannonFanoCode: "0" }], encodedBits 3, entropy 0

    One distinct symbol needs no branching, so both builders assign the single-bit code 0 and entropy collapses to zero.

    About this tool

    Huffman Shannon Tree runs analyzeText over the input, splitting it into Unicode code points with [...text] so emoji count as single symbols, then counting frequencies per symbol. It builds both a Huffman tree (repeatedly merging the two lowest-frequency nodes from a queue ordered by frequency then minChar) and a Shannon-Fano tree (sorting symbols by descending frequency then character, then recursively splitting at the point minimizing |total - 2 * leftTotal|). Both trees are walked to prefix-free binary codes, and entries are returned sorted by character with frequency, probability, huffmanCode, and shannonFanoCode per symbol.

    Aggregates are derived from the Huffman codes: encodedBits is the frequency-weighted Huffman length, averageCodeLength is encodedBits divided by symbol count, entropy is the Shannon -sum(p * log2(p)) bound in bits per symbol, originalBits assumes 8 bits per symbol, and compressionRatio and savingsPercent compare encodedBits against that 8-bit baseline. A single repeated symbol receives code 0, so aaa encodes in 3 bits with zero entropy; uniform inputs such as abcd receive equal-length codes (2 bits each, entropy exactly 2). Empty input yields zeroed aggregates, an empty entry list, and undefined trees.

    Tie-breaking is fully deterministic: Huffman orders nodes by frequency then lexicographic minChar, and Shannon-Fano orders by descending frequency then character, so anagrams like abcd and dcba produce identical codes. Non-printing symbols render with formatSymbol (space as ␠, newline as ↵, tab as ⇥). Every emitted code is a non-empty binary string, and both code sets are prefix-free, matching the prefix property DEFLATE-style Huffman coding relies on.

    How to use this tool

    1. Enter text to analyze

      Paste or type any string. Frequencies are counted per code point, so multi-byte emoji are single symbols, and the entry table plus both code sets update from the counts.

    2. Compare the two code tables

      Read huffmanCode and shannonFanoCode side by side per symbol. Huffman minimizes the weighted length; on skewed inputs such as AAAAAAABBBCCDE the Shannon-Fano total meets or exceeds it.

    3. Read entropy versus average length

      Use entropy as the theoretical floor and averageCodeLength as the achieved Huffman rate. The gap between them is the redundancy left in the code.

    4. Check the size win

      Compare encodedBits against originalBits (length × 8) via compressionRatio and savingsPercent to see the headline saving under the tool's fixed 8-bit baseline.

    Use cases

    Teaching entropy coding

    Contrast Huffman optimality against Shannon-Fano splits, entropy floors, and prefix-free code tables on small hand-checkable inputs.

    Estimating compressibility

    Use the frequency table, entropy, and savings percent under the 8-bit baseline to judge whether a payload is worth entropy-coding.

    Debugging symbol distributions

    Expose skewed versus uniform frequency tables, deterministic tie-breaks, and code-point handling of emoji before wiring data into a real codec.

    Common mistakes

    Mistake:Reading savingsPercent as a real file-size guarantee.

    Fix:The baseline is always length × 8 bits with no tree-storage or byte-alignment overhead. Treat it as a comparative estimate, not a DEFLATE output size.

    Mistake:Expecting Shannon-Fano to beat Huffman.

    Fix:Huffman minimizes the weighted length; Shannon-Fano can only tie or lose (e.g. 27 vs >= 27 bits on AAAAAAABBBCCDE). Compare them to learn, then ship Huffman.

    Mistake:Counting emoji or accented characters as bytes.

    Fix:The analyzer counts code points, not UTF-8 bytes, so 😀😀🎉 is length 3 with two distinct symbols. Multiply by the real encoding width separately when sizing bytes.

    Frequently asked questions

    What does this tool do?

    It counts symbol frequencies per Unicode code point in your text, builds both a Huffman tree and a Shannon-Fano tree, and reports per-symbol binary codes plus entropy, average Huffman code length, encoded bits, and savings against an 8-bits-per-symbol baseline.

    Why is Shannon-Fano sometimes longer than Huffman?

    Huffman always minimizes the frequency-weighted length by merging the two lowest nodes, while Shannon-Fano only splits sorted symbols near the half-total — a heuristic that can tie but never beat Huffman (e.g. 27 vs >= 27 bits on AAAAAAABBBCCDE). Single symbols get code 0 in both, and uniform inputs like abcd get equal-length 2-bit codes with entropy exactly 2.

    Is my text uploaded or stored?

    No. Counting, tree building, and entropy math run entirely in your browser — the analyzed text never leaves your device.

    How should I use the entropy and savings figures?

    Treat entropy as the theoretical floor and averageCodeLength as the achieved Huffman rate; savingsPercent compares against length × 8 bits with no tree or framing overhead, so use it to compare inputs and judge whether entropy coding is worthwhile, not as a compressed file size.

    References & standards