{"record":{"id":"fb333f5084daa6b9","repo":"TheAlgorithms/Java","slug":"invalid-binary-sequence-for-single-character-tree","errorCode":null,"errorMessage":"Invalid binary sequence for single-character tree.","messagePattern":"Invalid binary sequence for single-character tree\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/compression/HuffmanCoding.java","lineNumber":216,"sourceCode":"     * @return The reconstructed plaintext string. Returns an empty string if the input is null or empty.\n     * @throws IllegalStateException    If attempting to decode when the Huffman tree is empty.\n     * @throws IllegalArgumentException If the binary string contains characters other than '0' or '1',\n     * or if the sequence ends abruptly without reaching a leaf node.\n     */\n    public String decode(String encodedText) {\n        if (encodedText == null || encodedText.isEmpty()) {\n            return \"\";\n        }\n        if (root == null) {\n            throw new IllegalStateException(\"Huffman tree is empty.\");\n        }\n\n        StringBuilder sb = new StringBuilder();\n\n        if (root.isLeaf()) {\n            for (char bit : encodedText.toCharArray()) {\n                if (bit != '0') {\n                    throw new IllegalArgumentException(\"Invalid binary sequence for single-character tree.\");\n                }\n                sb.append(root.ch);\n            }\n            return sb.toString();\n        }\n\n        Node current = root;\n        for (char bit : encodedText.toCharArray()) {\n            if (bit != '0' && bit != '1') {\n                throw new IllegalArgumentException(\"Encoded text contains invalid characters: \" + bit);\n            }\n\n            current = (bit == '0') ? current.left : current.right;\n\n            if (current.isLeaf()) {\n                sb.append(current.ch);\n                current = root;\n            }","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/compression/HuffmanCoding.java#L198-L234","documentation":"When the Huffman tree is a single leaf (the corpus contained one distinct character), the code for that character is \"0\" and decode takes a fast path that asserts every bit is '0'. Any '1' (or other character) in that path is invalid because a single-node tree has no branch to follow, so the library rejects it.","triggerScenarios":"Building the tree from a single-character corpus like \"aaaa\", then decoding a payload containing a '1', e.g. decode(\"010\"); decoding corrupted/mismatched bits against a single-character tree.","commonSituations":"Encoded payload was tampered with or truncated; the decode corpus differs from the encode corpus (different single character); bits were concatenated incorrectly.","solutions":["Ensure every bit in the payload for a single-character tree is '0'.","Round-trip test: encode then decode with the same HuffmanCoding instance to catch mismatches.","Validate the payload against the tree shape before decoding."],"exampleFix":"// before\nHuffmanCoding hc = new HuffmanCoding(\"aaaa\");\nString dec = hc.decode(\"010\"); // contains a 1\n\n// after\nHuffmanCoding hc = new HuffmanCoding(\"aaaa\");\nString dec = hc.decode(\"000\"); // valid single-char-tree payload","handlingStrategy":"validation","validationCode":"// For a single-character tree, every bit must be '0'\nif (hc.getHuffmanCodes().size() == 1 && encodedText.chars().anyMatch(b -> b != '0')) {\n    throw new IllegalArgumentException(\"Single-character tree requires all-zero payload\");\n}\nString dec = hc.decode(encodedText);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Round-trip encode then decode with the same instance to catch mismatches.","Ensure single-character-tree payloads contain only '0' bits.","Validate the payload against the tree shape before decoding."],"tags":["compression","huffman","single-character","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}