{"record":{"id":"04a939d0a4e24e19","repo":"TheAlgorithms/C-Sharp","slug":"x-is-not-from-the-heap","errorCode":null,"errorMessage":"x is not from the heap","messagePattern":"x is not from the heap","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs","lineNumber":227,"sourceCode":"            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\");\n        }\n\n        if (x.Key == null)\n        {\n            throw new ArgumentException(\"x has no value\");\n        }\n\n        if (k.CompareTo(x.Key) > 0)\n        {\n            throw new InvalidOperationException(\"Value cannot be increased\");\n        }\n\n        x.Key = k;\n        var y = x.Parent;\n        if (y != null && x.Key.CompareTo(y.Key) < 0)\n        {\n            Cut(x, y);\n            CascadingCut(y);","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs#L209-L245","documentation":"FibonacciHeap.DecreaseKey(x, k) throws ArgumentException with \"x is not from the heap\" when MinItem is null, i.e. the heap is empty so node x cannot belong to it. The check names the node argument but really detects an empty heap.","triggerScenarios":"Calling DecreaseKey on an empty heap (MinItem == null), or with a node from another/already-emptied heap.","commonSituations":"Decreasing a key after the heap was fully popped, or passing a node captured from a heap instance that was since unioned or cleared.","solutions":["Check heap.Count > 0 / MinItem != null before DecreaseKey().","Keep the node reference tied to the same live heap instance.","Catch ArgumentException if node membership is uncertain."],"exampleFix":"// before\nheap.DecreaseKey(node, newVal);\n// after\nif (heap.Count > 0)\n{\n    heap.DecreaseKey(node, newVal);\n}","handlingStrategy":"validation","validationCode":"if (heap.Count > 0) { heap.DecreaseKey(node, k); }","typeGuard":null,"tryCatchPattern":"try { heap.DecreaseKey(node, k); } catch (ArgumentException) { /* node not in heap */ }","preventionTips":["Only decrease keys of nodes obtained from the same live heap","Check MinItem/Count before decrease-key","Discard node references after the heap is emptied"],"tags":["data-structures","fibonacci-heap","argument"],"backgroundTag":"invalid-argument-value","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"}