{"record":{"id":"3cc48965816ee645","repo":"TheAlgorithms/C-Sharp","slug":"key-key-already-in-tree","errorCode":null,"errorMessage":"Key \"{key}\" already in tree!","messagePattern":"Key \"(.+?)\" already in tree!","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/AATree/AATree.cs","lineNumber":211,"sourceCode":"    /// <exception cref=\"ArgumentException\">Thrown if key is already in the tree.</exception>\n    private AaTreeNode<TKey> Add(TKey key, AaTreeNode<TKey>? node)\n    {\n        if (node is null)\n        {\n            return new AaTreeNode<TKey>(key, 1);\n        }\n\n        if (comparer.Compare(key, node.Key) < 0)\n        {\n            node.Left = Add(key, node.Left);\n        }\n        else if (comparer.Compare(key, node.Key) > 0)\n        {\n            node.Right = Add(key, node.Right);\n        }\n        else\n        {\n            throw new ArgumentException($\"\"\"Key \"{key}\" already in tree!\"\"\", nameof(key));\n        }\n\n        return Split(Skew(node))!;\n    }\n\n    /// <summary>\n    ///     Recursive function to remove an element from the tree.\n    /// </summary>\n    /// <param name=\"key\">The element to remove.</param>\n    /// <param name=\"node\">The node to search from.</param>\n    /// <returns>The node with the specified element removed.</returns>\n    private AaTreeNode<TKey>? Remove(TKey key, AaTreeNode<TKey>? node)\n    {\n        if (node is null)\n        {\n            return null;\n        }\n","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/AATree/AATree.cs#L193-L229","documentation":"AATree.Add(key) throws ArgumentException when the key already exists in the tree. AA trees are keyed structures that do not allow duplicate keys, and the recursive Add detects a 0 comparison result and rejects the insert, naming the offending key in the message.","triggerScenarios":"Calling Add(key) (or AddRange containing duplicates) with a key already present per the tree's comparer; re-inserting the same key without checking Contains first.","commonSituations":"Importing data with duplicate identifiers; merging datasets where keys overlap; relying on the default comparer when a custom comparer treats distinct values as equal; double-insert from retry logic.","solutions":["Check tree.Contains(key) before calling Add","Use try-catch on ArgumentException and treat it as a duplicate-key signal","Deduplicate the input batch before AddRange","If duplicates should be allowed, wrap the key or move values into a per-key collection"],"exampleFix":"// before\ntree.Add(key);\n// after\nif (!tree.Contains(key)) tree.Add(key);","handlingStrategy":"validation","validationCode":"if (tree.Contains(key)) throw new ArgumentException($\"Duplicate key {key}\"); tree.Add(key);","typeGuard":"bool isInsertable(AATree<TKey,TValue> t, TKey key) => !t.Contains(key);","tryCatchPattern":"try { tree.Add(key); } catch (ArgumentException ex) when (ex.Message.Contains(\"already in tree\")) { /* handle duplicate */ }","preventionTips":["Use Contains before Add for potentially duplicate keys","Deduplicate batches before AddRange","Verify custom comparer semantics (equal vs distinct keys)","Avoid blind retry of insert operations"],"tags":["data-structures","duplicate-key","argument-exception"],"backgroundTag":"file-already-exists","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"}