{"record":{"id":"ac8347cb849023a0","repo":"TheAlgorithms/Java","slug":"character-array-and-frequency-array-cannot-be-empt","errorCode":null,"errorMessage":"Character array and frequency array cannot be empty","messagePattern":"Character array and frequency array cannot be empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/Huffman.java","lineNumber":85,"sourceCode":"public final class Huffman {\n    private Huffman() {\n    }\n\n    /**\n     * Builds a Huffman tree from the given character array and their frequencies.\n     *\n     * @param charArray array of characters\n     * @param charFreq  array of frequencies corresponding to the characters\n     * @return root node of the Huffman tree\n     * @throws IllegalArgumentException if arrays are null, empty, or have different\n     *                                  lengths\n     */\n    public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {\n        if (charArray == null || charFreq == null) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be null\");\n        }\n        if (charArray.length == 0 || charFreq.length == 0) {\n            throw new IllegalArgumentException(\"Character array and frequency array cannot be empty\");\n        }\n        if (charArray.length != charFreq.length) {\n            throw new IllegalArgumentException(\"Character array and frequency array must have the same length\");\n        }\n\n        int n = charArray.length;\n        PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());\n\n        // Create leaf nodes and add to priority queue\n        for (int i = 0; i < n; i++) {\n            if (charFreq[i] < 0) {\n                throw new IllegalArgumentException(\"Frequencies must be non-negative\");\n            }\n            HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);\n            priorityQueue.add(node);\n        }\n\n        // Build the Huffman tree","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/Huffman.java#L67-L103","documentation":"Thrown by Huffman.buildHuffmanTree when charArray.length == 0 or charFreq.length == 0. A Huffman tree needs at least one symbol to build; an empty alphabet has no leaves. The check runs after the null check and before the length-match check.","triggerScenarios":"Calling buildHuffmanTree with empty arrays — e.g., building a frequency table from an empty input string, or converting an empty map to arrays.","commonSituations":"Empty input text producing no symbols, a filter that removed all symbols, or a default-empty frequency table.","solutions":["Verify the symbol/frequency source yields at least one entry before calling.","Branch around the call if an empty alphabet is legitimate in your domain.","Log the array lengths to confirm emptiness at the call site."],"exampleFix":"// before\nchar[] syms = symbols.toArray(new char[0]);\nint[] frq = freqs.stream().mapToInt(Integer::intValue).toArray();\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq); // throws if input empty\n\n// after\nif (symbols.isEmpty()) throw new IllegalStateException(\"No symbols to encode\");\nchar[] syms = ...; int[] frq = ...;\nHuffmanNode root = Huffman.buildHuffmanTree(syms, frq);","handlingStrategy":"validation","validationCode":"if (charArray.length == 0 || charFreq.length == 0) {\n    throw new IllegalStateException(\"Cannot build Huffman tree from empty alphabet\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Branch around buildHuffmanTree when the alphabet may be empty.","Verify the symbol source yields at least one entry."],"tags":["huffman","precondition","empty-input","compression","tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}