TheAlgorithms/C-Sharp · error · ArgumentException

The value's key is smaller than or equal to node's right…

Error message

The value's key is smaller than or equal to node's right child's key.

What it means

Node<TKey>.Right setter validates binary-search-tree ordering: a right child's key must be greater than or equal to its parent's key. Setting a right child whose key is smaller than (or, per IsGreaterThanOrSameAs semantics as checked here, violating) the node's key throws ArgumentException naming `value`.

Solutions

  1. Ensure the child assigned to Right has a key >= the parent's key.
  2. Build the tree via ScapegoatTree.Insert instead of manual node linking.
  3. If relinking during a rotation, assign children so the BST invariant is preserved at each step.

Example fix

// before
node.Right = smallerChild; // smallerChild.Key < node.Key
// after
node.Left = smallerChild; // correct side keeps BST invariant
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Assigning node.Right = child where child.Key is smaller than or equal to node.Key (and value is not null).

Common situations: Manually constructing a scapegoat tree for tests, rotating/relinking nodes by hand during a custom rebalance, or building a tree from incorrectly sorted data.

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/4c792a2172cda73a. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/ScapegoatTree/Node.cs:21

/// <summary>
/// Scapegoat tree node class.
/// </summary>
/// <typeparam name="TKey">Scapegoat tree node key type.</typeparam>
public class Node<TKey>(TKey key) where TKey : IComparable
{
    private Node<TKey>? right;
    private Node<TKey>? left;

    public TKey Key { get; } = key;

    public Node<TKey>? Right
    {
        get => right;
        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;
        }

View on GitHub (pinned to 96e2905cab)