TheAlgorithms/C-Sharp · error · ArgumentException
x is not from the heap
Error message
x is not from the heap
What it means
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.
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.
Example fix
// before
heap.DecreaseKey(node, newVal);
// after
if (heap.Count > 0)
{
heap.DecreaseKey(node, newVal);
} Defensive patterns
Strategy: validation
Validate before calling
if (heap.Count > 0) { heap.DecreaseKey(node, k); } Try / catch
try { heap.DecreaseKey(node, k); } catch (ArgumentException) { /* node not in heap */ } Prevention
- 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
When it happens
Trigger: Calling DecreaseKey on an empty heap (MinItem == null), or with a node from another/already-emptied heap.
Common situations: Decreasing a key after the heap was fully popped, or passing a node captured from a heap instance that was since unioned or cleared.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/04a939d0a4e24e19.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Heap/FibonacciHeap/FibonacciHeap.cs:227
throw new InvalidOperationException("The heap is empty");
}
return MinItem.Key;
}
/// <summary>
/// Reduce the key of x to be k.
/// </summary>
/// <remarks>
/// k must be less than x.Key, increasing the key of an item is not supported.
/// </remarks>
/// <param name="x">The item you want to reduce in value.</param>
/// <param name="k">The new value for the item.</param>
public void DecreaseKey(FHeapNode<T> x, T k)
{
if (MinItem == null)
{
throw new ArgumentException($"{nameof(x)} is not from the heap");
}
if (x.Key == null)
{
throw new ArgumentException("x has no value");
}
if (k.CompareTo(x.Key) > 0)
{
throw new InvalidOperationException("Value cannot be increased");
}
x.Key = k;
var y = x.Parent;
if (y != null && x.Key.CompareTo(y.Key) < 0)
{
Cut(x, y);
CascadingCut(y);View on GitHub (pinned to 96e2905cab)