{"record":{"id":"1437b2a570394172","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"removing-from-empty-heap","errorCode":null,"errorMessage":"Removing from empty Heap","messagePattern":"Removing from empty Heap","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lectures/27-huffman-coding/code/Heap.java","lineNumber":34,"sourceCode":"        return list.size();\n    }\n    private void upheap(int index){\n\n        if(index == 0){\n            return;\n        }\n\n        int p = parent(index);\n\n        if(list.get(index).compareTo(list.get(p)) < 0){\n            swap(index, p);\n            upheap(p);\n        }\n    }\n\n    public T remove() throws Exception{\n        if(list.isEmpty()){\n            throw new Exception(\"Removing from empty Heap\");\n        }\n        T temp = list.get(0);\n\n        T last = list.remove(list.size() - 1);\n\n        if(!list.isEmpty()){\n            list.set(0, last);\n\n            downheap(0);\n        }\n        return temp;\n    }\n\n    private void downheap(int index) {\n\n        int min = index;\n        int left = left(index);\n        int right = right(index);","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/27-huffman-coding/code/Heap.java#L16-L52","documentation":"Heap.remove() in the Huffman-coding lecture throws a checked Exception when the list backing the heap is empty, since extract-min needs a root node. In Huffman workflows it returns the next lowest-frequency node, so underflow breaks tree building.","triggerScenarios":"Calling remove() on an empty heap: extracting more nodes than inserted, calling remove() twice for merging when fewer than two nodes remain, or building a Huffman tree from an empty symbol set.","commonSituations":"Huffman tree construction loops calling remove() twice per merge but running one extra iteration; encoding with an empty frequency map; heaps built from empty input text so no nodes were ever added.","solutions":["Check isEmpty() (or size() >= 2 for the merge loop) before removing.","Catch Exception around remove() and terminate tree building when empty.","Handle the single-node/empty-input edge case before the merge loop."],"exampleFix":"// before\nNode left = heap.remove();\nNode right = heap.remove();\n// after\nif (heap.size() >= 2) {\n    Node left = heap.remove();\n    Node right = heap.remove();\n}","handlingStrategy":"validation","validationCode":"if (heap.size() >= 2) {\n    Node left = heap.remove();\n    Node right = heap.remove();\n}","typeGuard":null,"tryCatchPattern":"try {\n    Node left = heap.remove();\n    Node right = heap.remove();\n} catch (Exception e) {\n    // fewer than two nodes left — finish tree building\n}","preventionTips":["Check size() >= 2 before each two-node Huffman merge step.","Handle empty-input and single-symbol edge cases before the merge loop.","Verify the frequency map is non-empty before building the heap.","Count insertions and stop merging when fewer than two nodes remain."],"tags":["java","heap","huffman-coding","underflow","checked-exception"],"backgroundTag":"heap-empty-underflow","analyzedSha":"6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c","analyzedAt":"2026-08-31T22:04:22.314Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}