{"record":{"id":"584056837dbeb1db","repo":"TheAlgorithms/C-Sharp","slug":"heap-is-empty-minmaxheap","errorCode":null,"errorMessage":"Heap is empty","messagePattern":"Heap is empty","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/MinMaxHeap.cs","lineNumber":61,"sourceCode":"    ///     Adds an element to the heap.\n    /// </summary>\n    /// <param name=\"item\">The element to add to the heap.</param>\n    public void Add(T item)\n    {\n        heap.Add(item);\n        PushUp(Count - 1);\n    }\n\n    /// <summary>\n    ///     Removes the maximum node from the heap and returns its value.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\">Thrown if heap is empty.</exception>\n    /// <returns>Value of the removed maximum node.</returns>\n    public T ExtractMax()\n    {\n        if (Count == 0)\n        {\n            throw new InvalidOperationException(\"Heap is empty\");\n        }\n\n        var max = GetMax();\n        RemoveNode(GetMaxNodeIndex());\n        return max;\n    }\n\n    /// <summary>\n    ///     Removes the minimum node from the heap and returns its value.\n    /// </summary>\n    /// <exception cref=\"InvalidOperationException\">Thrown if heap is empty.</exception>\n    /// <returns>Value of the removed minimum node.</returns>\n    public T ExtractMin()\n    {\n        if (Count == 0)\n        {\n            throw new InvalidOperationException(\"Heap is empty\");\n        }","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/MinMaxHeap.cs#L43-L79","documentation":"MinMaxHeap.ExtractMax() throws InvalidOperationException(\"Heap is empty\") when Count == 0. The library refuses to remove the maximum from an empty heap because there is no element to return. It is a guard against operating on uninitialized/emptied heap state.","triggerScenarios":"Calling ExtractMax() on a MinMaxHeap with zero elements — e.g. calling it before any Insert, or calling it more times than items were inserted (including after prior ExtractMax/ExtractMin calls drained the heap).","commonSituations":"Looping 'while' over a heap to drain it without checking Count first; merging/priority-queue code that pops a max after a failed insert path; heap-sort implementations that call ExtractMax an off-by-one number of times.","solutions":["Check heap.Count > 0 before calling ExtractMax()","Wrap the call in try/catch for InvalidOperationException if draining until empty","Restructure drain loops to use while (heap.Count > 0) { heap.ExtractMax(); }"],"exampleFix":"// before\nvar max = heap.ExtractMax();\n// after\nif (heap.Count == 0) return default; // or handle empty case\nvar max = heap.ExtractMax();","handlingStrategy":"validation","validationCode":"if (heap.Count == 0) { /* handle empty: return default / skip */ } else { var max = heap.ExtractMax(); }","typeGuard":null,"tryCatchPattern":"try { var max = heap.ExtractMax(); } catch (InvalidOperationException) { /* heap drained */ }","preventionTips":["Always check Count before extract/peek operations on MinMaxHeap","Use while (heap.Count > 0) loops to drain heaps","Treat extract/peek as unsafe on empty collections by default"],"tags":["data-structures","heap","empty-collection"],"backgroundTag":"empty-result-set","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}