{"record":{"id":"23a880019e31916e","repo":"TheAlgorithms/C-Sharp","slug":"heap-is-empty-fibonacciheap","errorCode":null,"errorMessage":"Heap is empty!","messagePattern":"Heap is empty!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs","lineNumber":159,"sourceCode":"    ///             or another of the items in the root list is a candidate to become the new\n    ///             MinItem.\n    ///         </item>\n    ///         <item>\n    ///             Remove the MinItem from the root list and appoint a new MinItem temporarily.\n    ///         </item>\n    ///         <item>\n    ///             <see cref=\"Consolidate\" /> what's left\n    ///             of the heap.\n    ///         </item>\n    ///     </list>\n    /// </remarks>\n    /// <returns>The minimum item from the heap.</returns>\n    public T Pop()\n    {\n        FHeapNode<T>? z = null;\n        if (MinItem == null)\n        {\n            throw new InvalidOperationException(\"Heap is empty!\");\n        }\n\n        z = MinItem;\n\n        // Since z is leaving the heap, add its children to the root list\n        if (z.Child != null)\n        {\n            foreach (var x in SiblingIterator(z.Child))\n            {\n                x.Parent = null;\n            }\n\n            // This effectively adds each child x to the root list\n            z.ConcatenateRight(z.Child);\n        }\n\n        if (Count == 1)\n        {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs#L141-L177","documentation":"This InvalidOperationException is the empty-heap guard at the start of FibonacciHeap.Pop (FibonacciHeap.cs:159). Pop removes the minimum node, which requires MinItem to reference a real root-list node; when MinItem is null the heap holds no elements and there is nothing to remove. It fires when Pop is called on a heap with Count == 0, or after all elements have already been popped.","triggerScenarios":"Calling Pop() on a new FibonacciHeap<T>, after popping all nodes, or on a heap made empty by Union with an empty other heap.","commonSituations":"Dijkstra/Prim loops that pop without checking Count or MinItem, or draining the heap fully and popping once more.","solutions":["Check heap.Count > 0 or MinItem != null before Pop().","Catch InvalidOperationException when empty is a valid state.","Guard extraction loops to stop when the heap is empty."],"exampleFix":"// before\nvar min = heap.Pop();\n// after\nif (heap.Count > 0)\n{\n    var min = heap.Pop();\n}","handlingStrategy":"validation","validationCode":"if (heap.Count > 0) { var min = heap.Pop(); }","typeGuard":null,"tryCatchPattern":"try { var min = heap.Pop(); } catch (InvalidOperationException) { /* heap empty */ }","preventionTips":["Guard extraction loops with Count > 0","Track heap size alongside graph algorithm state","Do not pop after full drain"],"tags":["data-structures","fibonacci-heap","invalid-operation"],"backgroundTag":"unsupported-operation","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"}