DSAVisualiseData Structures & Algorithms Studio
Strings & Compression Hard

Huffman Encoding Tree

Repeatedly remove the two least-frequency nodes, combine them, and derive each character's bit path.

Trace input

Bounded JSON

Edit the lesson input, then build a deterministic source-linked trace.

This lesson validates its own input and operation limits.

Source scenarios

Complexity

BestO(n log n)
AverageO(n log n)
WorstO(n log n)
SpaceO(n)

Array state

Follow the active comparisons and writes.

Live snapshot

Current operation

Trace state
Ready to explore
Source behavior & common mistakes

Construct the source's Huffman trie from an ordered list of unique character frequencies and derive 0-left, 1-right codes.

  • Swapping the first and second polled nodes changes left/right encoding paths.
  • Sorting ties by character instead of preserving Java PriorityQueue's strict comparator behavior.
  • Using unbounded arithmetic instead of Java signed 32-bit sums and comparator differences.
  • Assigning a nonempty code to a one-symbol tree when the source has no explicit encoder convention.

Algorithm code

Java
Huffman.java (pinned repository source)Step linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1