{"record":{"id":"fff7a84f3b0c1310","repo":"TheAlgorithms/C-Sharp","slug":"key-key-is-not-in-the-tree","errorCode":null,"errorMessage":"Key {key} is not in the tree!","messagePattern":"Key (.+?) is not in the tree!","errorType":"exception","errorClass":"KeyNotFoundException","httpStatus":null,"severity":"error","filePath":"DataStructures/RedBlackTree/RedBlackTree.cs","lineNumber":453,"sourceCode":"            return 2;\n        }\n    }\n\n    /// <summary>\n    ///     Search for the node to be deleted.\n    /// </summary>\n    /// <param name=\"node\">Node to start search from.</param>\n    /// <param name=\"key\">Key to search for.</param>\n    /// <returns>Node to be deleted.</returns>\n    private RedBlackTreeNode<TKey> Remove(RedBlackTreeNode<TKey>? node, TKey key)\n    {\n        if (node is null)\n        {\n            throw new InvalidOperationException(\"Tree is empty!\");\n        }\n        else if (!Contains(key))\n        {\n            throw new KeyNotFoundException($\"Key {key} is not in the tree!\");\n        }\n        else\n        {\n            // Find node\n            int dir;\n            while (true)\n            {\n                dir = comparer.Compare(key, node!.Key);\n                if (dir < 0)\n                {\n                    node = node.Left;\n                }\n                else if (dir > 0)\n                {\n                    node = node.Right;\n                }\n                else\n                {","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/RedBlackTree/RedBlackTree.cs#L435-L471","documentation":"RedBlackTree Remove(node, key) throws KeyNotFoundException when Contains(key) is false — the key is not present in a non-empty tree. The tree requires exact key existence before attempting rebalanced deletion.","triggerScenarios":"Calling public Remove(key) with a key never added, or one already removed earlier, on a non-empty tree.","commonSituations":"Double-deletion in eviction logic, removing keys from a different tree instance than they were added to, comparer mismatches making keys non-equal, or stale key references after data reload.","solutions":["Check tree.Contains(key) before calling Remove.","Catch KeyNotFoundException when absence is an acceptable outcome.","Fix key construction/comparer so lookups match insertion (same normalization).","Track removed keys to avoid duplicate Remove calls."],"exampleFix":"// before\ntree.Remove(key); // KeyNotFoundException if absent\n// after\nif (tree.Contains(key))\n{\n    tree.Remove(key);\n}","handlingStrategy":"validation","validationCode":"if (tree.Contains(key))\n{\n    tree.Remove(key);\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    tree.Remove(key);\n}\ncatch (KeyNotFoundException)\n{\n    // key absent — safe to ignore or log\n}","preventionTips":["Contains-check before Remove.","Guard against double removal in eviction code.","Ensure the same tree instance and comparer are used for add and remove.","Catch KeyNotFoundException when absence is acceptable."],"tags":["data-structures","red-black-tree","key-not-found","csharp"],"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"}