TheAlgorithms/C-Sharp · error · ArgumentException

The value's key is greater than or equal to node's left…

Error message

The value's key is greater than or equal to node's left child's key.

What it means

Node<TKey>.Left setter enforces BST ordering: a left child's key must be strictly less than its parent's key. Assigning a left child whose key is greater than or equal to the node's key throws ArgumentException naming `value`.

Solutions

  1. Ensure the child assigned to Left has a key strictly less than the parent's key.
  2. Use ScapegoatTree.Insert to build the tree rather than manual node wiring.
  3. During rotations, verify each relink maintains left < parent <= right.

Example fix

// before
node.Left = bigChild; // bigChild.Key >= node.Key
// after
node.Right = bigChild; // >= goes on the right
Defensive patterns

Strategy: validation

Validate before calling

if (child != null && child.Key.CompareTo(node.Key) >= 0) throw new InvalidOperationException("Left child key must be < parent key");

Type guard

static bool IsValidLeftChild<TKey>(Node<TKey> parent, Node<TKey> child) where TKey : IComparable => child == null || !child.IsGreaterThanOrSameAs(parent.Key);

Try / catch

try { node.Left = child; } catch (ArgumentException ex) { /* log BST violation; rebuild tree */ }

Prevention

When it happens

Trigger: Assigning node.Left = child where child.IsGreaterThanOrSameAs(node.Key) is true and child is not null.

Common situations: Hand-building trees in unit tests, custom rotation code that swaps children incorrectly, or reusing a node from a different part of the tree where duplicates or wrong-order keys exist.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at DataStructures/ScapegoatTree/Node.cs:35

        set
        {
            if (value != null && !value.IsGreaterThanOrSameAs(Key))
            {
                throw new ArgumentException("The value's key is smaller than or equal to node's right child's key.", nameof(value));
            }

            right = value;
        }
    }

    public Node<TKey>? Left
    {
        get => left;
        set
        {
            if (value != null && value.IsGreaterThanOrSameAs(Key))
            {
                throw new ArgumentException("The value's key is greater than or equal to node's left child's key.", nameof(value));
            }

            left = value;
        }
    }

    public Node(TKey key, Node<TKey>? right, Node<TKey>? left)
        : this(key)
    {
        Right = right;
        Left = left;
    }

    /// <summary>
    /// Returns number of elements in the tree.
    /// </summary>
    /// <returns>Number of elements in the tree.</returns>
    public int GetSize() => (Left?.GetSize() ?? 0) + 1 + (Right?.GetSize() ?? 0);

View on GitHub (pinned to 96e2905cab)