{"record":{"id":"4c792a2172cda73a","repo":"TheAlgorithms/C-Sharp","slug":"the-value-s-key-is-smaller-than-or-equal-to-node-s-right","errorCode":null,"errorMessage":"The value's key is smaller than or equal to node's right child's key.","messagePattern":"The value's key is smaller than or equal to node's right child's key\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/ScapegoatTree/Node.cs","lineNumber":21,"sourceCode":"/// <summary>\n/// Scapegoat tree node class.\n/// </summary>\n/// <typeparam name=\"TKey\">Scapegoat tree node key type.</typeparam>\npublic class Node<TKey>(TKey key) where TKey : IComparable\n{\n    private Node<TKey>? right;\n    private Node<TKey>? left;\n\n    public TKey Key { get; } = key;\n\n    public Node<TKey>? Right\n    {\n        get => right;\n        set\n        {\n            if (value != null && !value.IsGreaterThanOrSameAs(Key))\n            {\n                throw new ArgumentException(\"The value's key is smaller than or equal to node's right child's key.\", nameof(value));\n            }\n\n            right = value;\n        }\n    }\n\n    public Node<TKey>? Left\n    {\n        get => left;\n        set\n        {\n            if (value != null && value.IsGreaterThanOrSameAs(Key))\n            {\n                throw new ArgumentException(\"The value's key is greater than or equal to node's left child's key.\", nameof(value));\n            }\n\n            left = value;\n        }","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/ScapegoatTree/Node.cs#L3-L39","documentation":"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`.","triggerScenarios":"Assigning node.Right = child where child.Key is smaller than or equal to node.Key (and value is not null).","commonSituations":"Manually constructing a scapegoat tree for tests, rotating/relinking nodes by hand during a custom rebalance, or building a tree from incorrectly sorted data.","solutions":["Ensure the child assigned to Right has a key >= the parent's key.","Build the tree via ScapegoatTree.Insert instead of manual node linking.","If relinking during a rotation, assign children so the BST invariant is preserved at each step."],"exampleFix":"// before\nnode.Right = smallerChild; // smallerChild.Key < node.Key\n// after\nnode.Left = smallerChild; // correct side keeps BST invariant","handlingStrategy":"validation","validationCode":"if (child != null && child.Key.CompareTo(node.Key) < 0) throw new InvalidOperationException(\"Right child key must be >= parent key\");","typeGuard":"static bool IsValidRightChild<TKey>(Node<TKey> parent, Node<TKey> child) where TKey : IComparable => child == null || child.IsGreaterThanOrSameAs(parent.Key);","tryCatchPattern":"try { node.Right = child; } catch (ArgumentException ex) { /* log BST violation; rebuild tree */ }","preventionTips":["Build trees via Insert, not manual node assignment.","Recheck ordering after any manual rotation code.","Assert BST invariants in tests after tree mutations."],"tags":["binary-search-tree","invariant-violation","argument-validation"],"backgroundTag":"invalid-argument-value","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"}