TheAlgorithms/Java · error · IllegalArgumentException

Frequencies must be non-negative

Error message

Frequencies must be non-negative

What it means

Thrown by Huffman.buildHuffmanTree inside the leaf-creation loop when charFreq[i] < 0. Huffman frequencies represent occurrence counts/weights used to build a min-priority queue; a negative weight breaks the priority ordering and would produce an invalid tree (potentially a non-optimal or cyclic structure).

Source

Thrown at src/main/java/com/thealgorithms/others/Huffman.java:97

     */
    public static HuffmanNode buildHuffmanTree(char[] charArray, int[] charFreq) {
        if (charArray == null || charFreq == null) {
            throw new IllegalArgumentException("Character array and frequency array cannot be null");
        }
        if (charArray.length == 0 || charFreq.length == 0) {
            throw new IllegalArgumentException("Character array and frequency array cannot be empty");
        }
        if (charArray.length != charFreq.length) {
            throw new IllegalArgumentException("Character array and frequency array must have the same length");
        }

        int n = charArray.length;
        PriorityQueue<HuffmanNode> priorityQueue = new PriorityQueue<>(n, new HuffmanComparator());

        // Create leaf nodes and add to priority queue
        for (int i = 0; i < n; i++) {
            if (charFreq[i] < 0) {
                throw new IllegalArgumentException("Frequencies must be non-negative");
            }
            HuffmanNode node = new HuffmanNode(charArray[i], charFreq[i]);
            priorityQueue.add(node);
        }

        // Build the Huffman tree
        while (priorityQueue.size() > 1) {
            HuffmanNode left = priorityQueue.poll();
            HuffmanNode right = priorityQueue.poll();

            HuffmanNode parent = new HuffmanNode();
            parent.data = left.data + right.data;
            parent.c = '-';
            parent.left = left;
            parent.right = right;

            priorityQueue.add(parent);
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate frequencies are non-negative at the point they are computed; clamp or reject negatives.
  2. Sanitize the frequency table before building arrays (replace negatives with 0 or drop the symbol).
  3. Audit the upstream counter logic for underflow.

Example fix

// before
int[] frq = computeWeights(input); // may contain negatives
Huffman.buildHuffmanTree(syms, frq);

// after
int[] frq = computeWeights(input);
for (int i=0;i<frq.length;i++) if (frq[i] < 0) frq[i] = 0; // or throw
Huffman.buildHuffmanTree(syms, frq);
Defensive patterns

Strategy: validation

Validate before calling

for (int f : charFreq) {
    if (f < 0) throw new IllegalStateException("Negative frequency " + f);
}

Prevention

When it happens

Trigger: Passing a frequency array with a negative value — e.g., weights computed as a difference that went negative, a corrupted/deserialized table, or a signed-integer underflow in a counter.

Common situations: Frequency tables derived from deltas or signed adjustments, data corruption, or a counter that decremented below zero.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/2eb6a3c3361c0091. Report an issue: GitHub.