{"record":{"id":"c1b38c2cd7342c0c","repo":"TheAlgorithms/C-Sharp","slug":"current-value-is-not-present-in-this-heap","errorCode":null,"errorMessage":"Current value is not present in this heap.","messagePattern":"Current value is not present in this heap\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/Heap/PairingHeap/PairingHeap.cs","lineNumber":53,"sourceCode":"    public T Extract()\n    {\n        var minMax = root;\n\n        RemoveMapping(minMax.Value, minMax);\n        RebuildHeap(root.ChildrenHead);\n\n        Count--;\n        return minMax.Value;\n    }\n\n    /// <summary>\n    /// Update heap key [O(log(n))].\n    /// </summary>\n    public void UpdateKey(T currentValue, T newValue)\n    {\n        if (!mapping.ContainsKey(currentValue))\n        {\n            throw new ArgumentException(\"Current value is not present in this heap.\");\n        }\n\n        var node = mapping[currentValue]?.Where(x => x.Value.Equals(currentValue)).FirstOrDefault();\n\n        if (comparer.Compare(newValue, node!.Value) > 0)\n        {\n            throw new ArgumentException($\"New value is not {(sorting != Sorting.Descending ? \"less\" : \"greater\")} than old value.\");\n        }\n\n        UpdateNodeValue(currentValue, newValue, node);\n\n        if (node == root)\n        {\n            return;\n        }\n\n        DeleteChild(node);\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/Heap/PairingHeap/PairingHeap.cs#L35-L71","documentation":"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().","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure the value is inserted with Insert() before calling UpdateKey()","Check membership first by tracking inserted values in your own set, or catch ArgumentException","Verify the value type implements Equals/GetHashCode consistently with the heap's comparer"],"exampleFix":"// before\nheap.UpdateKey(node, newDist); // node may have been extracted\n// after\nif (inHeap.Contains(node)) heap.UpdateKey(node, newDist);","handlingStrategy":"validation","validationCode":"if (!heapContainsValue(currentValue)) { /* insert it first or skip */ } else { heap.UpdateKey(currentValue, newValue); }","typeGuard":null,"tryCatchPattern":"try { heap.UpdateKey(currentValue, newValue); } catch (ArgumentException) { /* value not in heap */ }","preventionTips":["Only UpdateKey values currently in the heap (track with a HashSet)","Never UpdateKey after the value was extracted","Ensure T has consistent Equals/GetHashCode with the heap's comparer"],"tags":["data-structures","heap","argument-exception","key-not-found"],"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"}