{"record":{"id":"edc1b5e11ba955d9","repo":"TheAlgorithms/C-Sharp","slug":"heap-malformed","errorCode":null,"errorMessage":"Heap malformed","messagePattern":"Heap malformed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs","lineNumber":263,"sourceCode":"            CascadingCut(y);\n        }\n\n        if (x.Key.CompareTo(MinItem.Key) < 0)\n        {\n            MinItem = x;\n        }\n    }\n\n    /// <summary>\n    ///     Remove x from the child list of y.\n    /// </summary>\n    /// <param name=\"x\">A child of y we just decreased the value of.</param>\n    /// <param name=\"y\">The now former parent of x.</param>\n    protected void Cut(FHeapNode<T> x, FHeapNode<T> y)\n    {\n        if (MinItem == null)\n        {\n            throw new InvalidOperationException(\"Heap malformed\");\n        }\n\n        if (y.Degree == 1)\n        {\n            y.Child = null;\n            MinItem.AddRight(x);\n        }\n        else if (y.Degree > 1)\n        {\n            x.Remove();\n        }\n        else\n        {\n            throw new InvalidOperationException(\"Heap malformed\");\n        }\n\n        y.Degree--;\n        x.Mark = false;","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs#L245-L281","documentation":"FibonacciHeap.Cut(x, y) throws InvalidOperationException \"Heap malformed\" when MinItem is null. Cutting a node out of the root/child lists requires the heap to still have a minimum node; a null MinItem means internal structure is inconsistent.","triggerScenarios":"Cut invoked (via DecreaseKey or CascadingCut) while the heap is empty — only reachable through inconsistent node/heap state or misuse of the protected API.","commonSituations":"Subclassing and calling Cut directly, or reusing nodes from a heap that was fully popped into a fresh heap.","solutions":["Ensure DecreaseKey is only called on nodes in a non-empty live heap.","Do not call the protected Cut/CascadingCut from subclass code on an empty heap.","Rebuild the node-heap association: create a fresh node and Push it."],"exampleFix":"// before\nheap.DecreaseKey(detachedNode, k); // triggers Cut on empty heap\n// after\nvar fresh = new FHeapNode<T>(k);\nheap.Push(fresh);","handlingStrategy":"validation","validationCode":"if (heap.Count > 0) { heap.DecreaseKey(node, k); } // Cut is only reached via DecreaseKey/CascadingCut","typeGuard":"bool InLiveHeap<T>(FibonacciHeap<T> heap, FHeapNode<T> node) => heap.Count > 0;","tryCatchPattern":"try { heap.DecreaseKey(node, k); } catch (InvalidOperationException ex) when (ex.Message == \"Heap malformed\") { /* rebuild heap */ }","preventionTips":["Never call protected Cut/CascadingCut directly","Only operate on nodes of a non-empty live heap","If you hit this, rebuild the heap from surviving values"],"tags":["data-structures","fibonacci-heap","invariant"],"backgroundTag":"internal-invariant-violation","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"}