{"record":{"id":"5d88b61a3c192a09","repo":"TheAlgorithms/C-Sharp","slug":"the-heap-is-empty","errorCode":null,"errorMessage":"The heap is empty","messagePattern":"The heap is empty","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs","lineNumber":209,"sourceCode":"        Consolidate();\n\n        Count -= 1;\n\n        return z.Key;\n    }\n\n    /// <summary>\n    ///     A method to see what's on top of the heap without changing its structure.\n    /// </summary>\n    /// <returns>\n    ///     Returns the top element without popping it from the structure of\n    ///     the heap.\n    /// </returns>\n    public T Peek()\n    {\n        if (MinItem == null)\n        {\n            throw new InvalidOperationException(\"The heap is empty\");\n        }\n\n        return MinItem.Key;\n    }\n\n    /// <summary>\n    ///     Reduce the key of x to be k.\n    /// </summary>\n    /// <remarks>\n    ///     k must be less than x.Key, increasing the key of an item is not supported.\n    /// </remarks>\n    /// <param name=\"x\">The item you want to reduce in value.</param>\n    /// <param name=\"k\">The new value for the item.</param>\n    public void DecreaseKey(FHeapNode<T> x, T k)\n    {\n        if (MinItem == null)\n        {\n            throw new ArgumentException($\"{nameof(x)} is not from the heap\");","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs#L191-L227","documentation":"This InvalidOperationException is the empty-heap guard in FibonacciHeap.Peek (FibonacciHeap.cs:209). Peek returns MinItem.Key without modifying the heap; when MinItem is null the heap has no elements, so no minimum key exists to return. It fires whenever Peek is called on an empty (Count == 0) FibonacciHeap.","triggerScenarios":"Calling Peek() on an empty FibonacciHeap<T> (no Push yet, or fully drained).","commonSituations":"Reading the minimum before inserting elements, or peeking after a drain loop in graph algorithms.","solutions":["Check heap.Count > 0 before Peek().","Catch InvalidOperationException when emptiness is expected.","Insert at least one element before querying the minimum."],"exampleFix":"// before\nvar min = heap.Peek();\n// after\nvar min = heap.Count > 0 ? heap.Peek() : throw new EmptyHeapException();","handlingStrategy":"validation","validationCode":"if (heap.Count == 0) return; // or default\nvar min = heap.Peek();","typeGuard":null,"tryCatchPattern":"try { var min = heap.Peek(); } catch (InvalidOperationException) { /* empty heap path */ }","preventionTips":["Check Count before Peek","Ensure at least one Push precedes any Peek","Centralize heap access in helper methods with emptiness checks"],"tags":["data-structures","fibonacci-heap","invalid-operation"],"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"}