{"record":{"id":"752726fde9ebc1c1","repo":"kunal-kushwaha/DSA-Bootcamp-Java","slug":"removing-from-an-empty-heap","errorCode":null,"errorMessage":"Removing from an empty heap!","messagePattern":"Removing from an empty heap!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"lectures/24-heaps/code/heaps-1/Heap.java","lineNumber":46,"sourceCode":"\n  public void insert(T value) {\n    list.add(value);\n    upheap(list.size() - 1);\n  }\n  private void upheap(int index) {\n    if(index == 0) {\n      return;\n    }\n    int p = parent(index);\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 an empty heap!\");\n    }\n\n    T temp = list.get(0);\n\n    T last = list.remove(list.size() - 1);\n    if (!list.isEmpty()) {\n      list.set(0, last);\n      downheap(0);\n    }\n    \n    return temp;\n  }\n  private void downheap(int index) {\n    int min = index;\n    int left = left(index);\n    int right = right(index);\n\n    if(left < list.size() && list.get(min).compareTo(list.get(left)) > 0) {","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/kunal-kushwaha/DSA-Bootcamp-Java/blob/6bc4d8bf8ac5e434ac9083e1c01210e42f2a762c/lectures/24-heaps/code/heaps-1/Heap.java#L28-L64","documentation":"Heap.remove() throws a checked Exception when the underlying list is empty, since there is no root element to extract. It is the heap's extract-min/max operation used by heapSort and other consumers, so underflow propagates into sorting/queue logic.","triggerScenarios":"Calling remove() on a Heap whose list is empty: removing more elements than were inserted, running heapSort on an empty or over-drained heap, or a second remove after the heap was exhausted.","commonSituations":"Priority-queue consumers polling until 'done' without checking size; heapSort loops with an off-by-one iteration; K-way merge code removing once per input when fewer inputs exist; reusing a heap after it was fully drained.","solutions":["Check heap size (isEmpty()) before each remove().","Catch Exception around remove() and stop consuming when empty.","Bound loops by heap size rather than a fixed count."],"exampleFix":"// before\nT min = heap.remove();\n// after\nif (!heap.isEmpty()) {\n    T min = heap.remove();\n}","handlingStrategy":"validation","validationCode":"if (!heap.isEmpty()) {\n    T min = heap.remove();\n}","typeGuard":null,"tryCatchPattern":"try {\n    T min = heap.remove();\n} catch (Exception e) {\n    // heap exhausted — stop consuming\n}","preventionTips":["Check isEmpty() before every heap remove().","Bound heapSort loops by heap size.","Check size before each removal in K-way merge loops.","Track how many elements remain after insertions and removals."],"tags":["java","heap","priority-queue","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"}