{"record":{"id":"7e83112fbd8d6a32","repo":"TheAlgorithms/Java","slug":"huffman-tree-is-empty","errorCode":null,"errorMessage":"Huffman tree is empty.","messagePattern":"Huffman tree is empty\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/HuffmanCoding.java","lineNumber":180,"sourceCode":"        generateCodes(node.right, code + \"1\", map);\n    }\n\n    /**\n     * Encodes the given plaintext string into a binary string using the generated Huffman dictionary.\n     *\n     * @param text The plaintext string to compress.\n     * @return A string of '0's and '1's representing the compressed data.\n     * Returns an empty string if the input is null or empty.\n     * @throws IllegalStateException    If attempting to encode when the Huffman tree is empty.\n     * @throws IllegalArgumentException If the input text contains a character not present\n     * in the original text used to build the tree.\n     */\n    public String encode(String text) {\n        if (text == null || text.isEmpty()) {\n            return \"\";\n        }\n        if (root == null) {\n            throw new IllegalStateException(\"Huffman tree is empty.\");\n        }\n\n        StringBuilder sb = new StringBuilder();\n        for (char c : text.toCharArray()) {\n            if (!huffmanCodes.containsKey(c)) {\n                throw new IllegalArgumentException(String.format(\"Character '%c' (U+%04X) not found in Huffman dictionary.\", c, (int) c));\n            }\n            sb.append(huffmanCodes.get(c));\n        }\n        return sb.toString();\n    }\n\n    /**\n     * Decodes the given binary string back into the original plaintext using the Huffman Tree.\n     * Validates the integrity of the binary payload during traversal.\n     *\n     * @param encodedText The binary string of '0's and '1's to decompress.\n     * @return The reconstructed plaintext string. Returns an empty string if the input is null or empty.","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/HuffmanCoding.java#L162-L198","documentation":"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.","triggerScenarios":"Constructing with HuffmanCoding hc = new HuffmanCoding(\"\") (or null) and then calling hc.encode(text); calling encode before constructing with a real corpus.","commonSituations":"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.","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."],"exampleFix":"// before\nHuffmanCoding hc = new HuffmanCoding(corpus); // corpus was empty\nString enc = hc.encode(message);\n\n// after\nif (corpus == null || corpus.isEmpty()) {\n    throw new IllegalStateException(\"Cannot build Huffman tree from an empty corpus\");\n}\nHuffmanCoding hc = new HuffmanCoding(corpus);\nString enc = hc.encode(message);","handlingStrategy":"validation","validationCode":"HuffmanCoding hc = new HuffmanCoding(corpus);\nif (hc.getHuffmanCodes().isEmpty()) {\n    throw new IllegalStateException(\"Huffman tree is empty; corpus was null or empty\");\n}\nString enc = hc.encode(text);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["compression","huffman","validation","illegal-state"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}