TheAlgorithms/Java · error · IllegalStateException
Huffman tree is empty.
Error message
Huffman tree is empty.
What it means
HuffmanCoding.encode(String) checks that the internal tree (root) has been built before traversing it. The tree is built in the constructor from the input text; if the constructor received null or empty text, root stays null and the dictionary is empty, so encoding is impossible. The library throws IllegalStateException to signal the object is in a usable-but-uninitialized-for-encoding state.
Source
Thrown at src/main/java/com/thealgorithms/compression/HuffmanCoding.java:180
generateCodes(node.right, code + "1", map);
}
/**
* Encodes the given plaintext string into a binary string using the generated Huffman dictionary.
*
* @param text The plaintext string to compress.
* @return A string of '0's and '1's representing the compressed data.
* Returns an empty string if the input is null or empty.
* @throws IllegalStateException If attempting to encode when the Huffman tree is empty.
* @throws IllegalArgumentException If the input text contains a character not present
* in the original text used to build the tree.
*/
public String encode(String text) {
if (text == null || text.isEmpty()) {
return "";
}
if (root == null) {
throw new IllegalStateException("Huffman tree is empty.");
}
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (!huffmanCodes.containsKey(c)) {
throw new IllegalArgumentException(String.format("Character '%c' (U+%04X) not found in Huffman dictionary.", c, (int) c));
}
sb.append(huffmanCodes.get(c));
}
return sb.toString();
}
/**
* Decodes the given binary string back into the original plaintext using the Huffman Tree.
* Validates the integrity of the binary payload during traversal.
*
* @param encodedText The binary string of '0's and '1's to decompress.
* @return The reconstructed plaintext string. Returns an empty string if the input is null or empty.View on GitHub (pinned to fdfb9a395b)
Solutions
- Construct the HuffmanCoding object with a non-empty training text so the tree and code dictionary are populated.
- If the corpus can legitimately be empty, guard encode with a check on getHuffmanCodes().isEmpty().
- Ensure the source feeding the constructor actually yields characters.
Example fix
// before
HuffmanCoding hc = new HuffmanCoding(corpus); // corpus was empty
String enc = hc.encode(message);
// after
if (corpus == null || corpus.isEmpty()) {
throw new IllegalStateException("Cannot build Huffman tree from an empty corpus");
}
HuffmanCoding hc = new HuffmanCoding(corpus);
String enc = hc.encode(message); Defensive patterns
Strategy: validation
Validate before calling
HuffmanCoding hc = new HuffmanCoding(corpus);
if (hc.getHuffmanCodes().isEmpty()) {
throw new IllegalStateException("Huffman tree is empty; corpus was null or empty");
}
String enc = hc.encode(text); Prevention
- Construct with a non-empty training corpus so the tree is built.
- Check getHuffmanCodes().isEmpty() before encoding.
- Ensure the corpus data source yields real text.
When it happens
Trigger: Constructing with HuffmanCoding hc = new HuffmanCoding("") (or null) and then calling hc.encode(text); calling encode before constructing with a real corpus.
Common situations: The training corpus used to build the tree was empty/missing; the HuffmanCoding object was constructed with a placeholder and never rebuilt; data loading returned an empty string.
Related errors
- Character '%c' (U+%04X) not found in Huffman dictionary.
- Invalid binary sequence for single-character tree.
- Encoded text contains invalid characters: {}
- Malformed encoded string: incomplete sequence ending.
- Frequencies must be non-negative
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/7e83112fbd8d6a32.
Report an issue: GitHub.