Huffman Tree Generator

Enter a sentence. Build its tree.

CSE 4295
13 characters
Try:

Case, spaces and punctuation count. Classroom limit: 2,000 characters and 64 different symbols.

1 · Count2 · Merge3 · Read codes

Available nodes · smallest first

The tree grows here

Each leaf shows a symbol and its frequency.

Chosen nodesNew parent0 = left edge1 = right edgeWide trees can scroll.
Methodology
  1. Count each symbol’s frequency.
  2. Sort the available nodes from smallest to largest.
  3. Take the two smallest nodes. Their sum becomes a new parent.
  4. Put that parent back into the sorted list. Repeat until one root remains.
  5. Assign 0 to left edges and 1 to right edges. Read each symbol’s code from the root.

Equal frequencies: this app chooses the node created earlier. Original symbols are created in the order they first appear; new parents are created later. The first selected node goes left (0), the second goes right (1). Other consistent tie choices can give different valid codes with the same minimum total bit cost.

One different symbol: there is nothing to merge. This lesson assigns code 0 to each occurrence.

Text is kept exactly: uppercase and lowercase differ; spaces, tabs and line breaks count. A symbol here means one Unicode code point, so some accented characters and joined emoji use more than one symbol.

Lossless: decoding the generated bits with the same tree reproduces the original text. Huffman coding minimizes the weighted code length among binary prefix codes for the supplied symbol frequencies.