TheAlgorithms/C-Sharp · error · ArgumentException

Current value is not present in this heap.

Error message

Current value is not present in this heap.

What it means

PairingHeap.UpdateKey(currentValue, newValue) throws ArgumentException when currentValue is not a key in the heap's internal mapping dictionary. UpdateKey only re-keys values already present in the heap; inserting is done via Insert().

Solutions

  1. Ensure the value is inserted with Insert() before calling UpdateKey()
  2. Check membership first by tracking inserted values in your own set, or catch ArgumentException
  3. Verify the value type implements Equals/GetHashCode consistently with the heap's comparer

Example fix

// before
heap.UpdateKey(node, newDist); // node may have been extracted
// after
if (inHeap.Contains(node)) heap.UpdateKey(node, newDist);
Defensive patterns

Strategy: validation

Validate before calling

if (!heapContainsValue(currentValue)) { /* insert it first or skip */ } else { heap.UpdateKey(currentValue, newValue); }

Try / catch

try { heap.UpdateKey(currentValue, newValue); } catch (ArgumentException) { /* value not in heap */ }

Prevention

When it happens

Trigger: Calling UpdateKey with a value that was never Insert()ed, a value already extracted from the heap, or a value that is equal but not the same instance/type expected by the mapping dictionary (e.g. boxed struct or different comparer semantics).

Common situations: Decrease-key loops in Dijkstra where a node was already popped from the heap; passing the wrong comparable record (value equality vs reference equality mismatch); typos between stored and looked-up values.

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/c1b38c2cd7342c0c. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/Heap/PairingHeap/PairingHeap.cs:53

    public T Extract()
    {
        var minMax = root;

        RemoveMapping(minMax.Value, minMax);
        RebuildHeap(root.ChildrenHead);

        Count--;
        return minMax.Value;
    }

    /// <summary>
    /// Update heap key [O(log(n))].
    /// </summary>
    public void UpdateKey(T currentValue, T newValue)
    {
        if (!mapping.ContainsKey(currentValue))
        {
            throw new ArgumentException("Current value is not present in this heap.");
        }

        var node = mapping[currentValue]?.Where(x => x.Value.Equals(currentValue)).FirstOrDefault();

        if (comparer.Compare(newValue, node!.Value) > 0)
        {
            throw new ArgumentException($"New value is not {(sorting != Sorting.Descending ? "less" : "greater")} than old value.");
        }

        UpdateNodeValue(currentValue, newValue, node);

        if (node == root)
        {
            return;
        }

        DeleteChild(node);

View on GitHub (pinned to 96e2905cab)