TheAlgorithms/C-Sharp · error · ArgumentException

Key " " already in tree!

Error message

Key "{key}" already in tree!

What it means

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.

Solutions

  1. Check tree.Contains(key) before calling Add
  2. Use try-catch on ArgumentException and treat it as a duplicate-key signal
  3. Deduplicate the input batch before AddRange
  4. If duplicates should be allowed, wrap the key or move values into a per-key collection

Example fix

// before
tree.Add(key);
// after
if (!tree.Contains(key)) tree.Add(key);
Defensive patterns

Strategy: validation

Validate before calling

if (tree.Contains(key)) throw new ArgumentException($"Duplicate key {key}"); tree.Add(key);

Type guard

bool isInsertable(AATree<TKey,TValue> t, TKey key) => !t.Contains(key);

Try / catch

try { tree.Add(key); } catch (ArgumentException ex) when (ex.Message.Contains("already in tree")) { /* handle duplicate */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/3cc48965816ee645. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/AATree/AATree.cs:211

    /// <exception cref="ArgumentException">Thrown if key is already in the tree.</exception>
    private AaTreeNode<TKey> Add(TKey key, AaTreeNode<TKey>? node)
    {
        if (node is null)
        {
            return new AaTreeNode<TKey>(key, 1);
        }

        if (comparer.Compare(key, node.Key) < 0)
        {
            node.Left = Add(key, node.Left);
        }
        else if (comparer.Compare(key, node.Key) > 0)
        {
            node.Right = Add(key, node.Right);
        }
        else
        {
            throw new ArgumentException($"""Key "{key}" already in tree!""", nameof(key));
        }

        return Split(Skew(node))!;
    }

    /// <summary>
    ///     Recursive function to remove an element from the tree.
    /// </summary>
    /// <param name="key">The element to remove.</param>
    /// <param name="node">The node to search from.</param>
    /// <returns>The node with the specified element removed.</returns>
    private AaTreeNode<TKey>? Remove(TKey key, AaTreeNode<TKey>? node)
    {
        if (node is null)
        {
            return null;
        }

View on GitHub (pinned to 96e2905cab)