{"record":{"id":"dce1b3f86e4153e6","repo":"TheAlgorithms/C-Sharp","slug":"key-is-not-in-the-tree","errorCode":null,"errorMessage":"key is not in the tree","messagePattern":"key is not in the tree","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/AATree/AATree.cs","lineNumber":74,"sourceCode":"    /// <param name=\"keys\">The elements to add to the tree.</param>\n    public void AddRange(IEnumerable<TKey> keys)\n    {\n        foreach (var key in keys)\n        {\n            Root = Add(key, Root);\n            Count++;\n        }\n    }\n\n    /// <summary>\n    ///     Remove a single element from the tree.\n    /// </summary>\n    /// <param name=\"key\">Element to remove.</param>\n    public void Remove(TKey key)\n    {\n        if (!Contains(key, Root))\n        {\n            throw new InvalidOperationException($\"{nameof(key)} is not in the tree\");\n        }\n\n        Root = Remove(key, Root);\n        Count--;\n    }\n\n    /// <summary>\n    ///     Checks if the specified element is in the tree.\n    /// </summary>\n    /// <param name=\"key\">The element to look for.</param>\n    /// <returns>true if the element is in the tree, false otherwise.</returns>\n    public bool Contains(TKey key) => Contains(key, Root);\n\n    /// <summary>\n    ///     Gets the largest element in the tree. (ie. the element in the right most node).\n    /// </summary>\n    /// <returns>The largest element in the tree according to the stored comparer.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the tree is empty.</exception>","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/AATree/AATree.cs#L56-L92","documentation":"AATree.Remove(TKey key) first checks Contains(key, Root) and throws InvalidOperationException with message \"key is not in the tree\" if the key is absent. Removal of a non-existent key is treated as an invalid state for the caller rather than a silent no-op, so verify membership or catch the exception.","triggerScenarios":"Calling tree.Remove(k) where k was never inserted or was already removed; removing with a key that is only equal under a different comparer than the tree's; calling Remove after Clear.","commonSituations":"Double-delete in test teardown (as in Remove_MultipleKeys_TreeStillValid / act test paths); keys sourced from a different collection that no longer exists in the tree; custom comparer mismatch making keys compare unequal.","solutions":["Check tree.Contains(key) before calling Remove, or wrap Remove in try-catch on InvalidOperationException.","Ensure the same key values (and comparer semantics) used for insertion are used for removal.","Track removals to avoid deleting the same key twice (e.g. remove from a HashSet of pending keys).","If a no-op delete is desired, extend/wrap Remove to skip when the key is absent."],"exampleFix":"// before\ntree.Remove(key); // throws if absent\n// after\nif (tree.Contains(key))\n{\n    tree.Remove(key);\n}","handlingStrategy":"try-catch","validationCode":"if (!tree.Contains(key))\n{\n    return; // nothing to remove\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    tree.Remove(key);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not in the tree\"))\n{\n    // treat as no-op or log the unexpected removal\n}","preventionTips":["Always guard removals with Contains or track membership in a companion set.","Use the same key type and comparer for insert and remove operations.","Avoid double-deletes by removing keys from pending-work collections first.","Consider a TryRemove-style wrapper for idempotent deletions."],"tags":["key-not-found","invalid-state","aatree","csharp","data-structures"],"backgroundTag":"record-not-found","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"}