TheAlgorithms/C-Sharp · error · ArgumentException
The parameter's value is invalid.
Error message
The parameter's value is invalid.
What it means
RebuildFromList rebuilds a balanced subtree of scapegoat-tree nodes from a list slice delimited by [start, end]. It throws ArgumentException when start is greater than end, meaning the requested window is inverted and cannot define a subtree. The parameter named in the exception is `start`.
Solutions
- Ensure start <= end before calling RebuildFromList; swap or normalize the indexes if needed.
- Decide explicitly how an empty range should behave (return null or skip the rebuild) and guard before calling.
- Validate computed indexes against the list bounds as well (0 <= start, end < list.Count).
Example fix
// before var node = Node<TKey>.RebuildFromList(nodes, end, start); // after if (start > end) (start, end) = (end, start); var node = Node<TKey>.RebuildFromList(nodes, start, end);
Defensive patterns
Strategy: validation
Validate before calling
if (list is null || start < 0 || end >= list.Count || start > end) throw new ArgumentOutOfRangeException(nameof(start), "Require 0 <= start <= end < list.Count");
Type guard
static bool IsValidRange<T>(int start, int end) => start <= end;
Prevention
- Always normalize (min,max) before slicing node lists.
- Add an assert start <= end near range computation.
- Unit-test empty-range edge cases in tree rebuild helpers.
When it happens
Trigger: Calling RebuildFromList with start > end, e.g. passing indexes in the wrong order or computing an empty/negative-length range.
Common situations: Off-by-one errors when slicing node lists after collecting an in-order path; passing (end, start) swapped; callers that computed an empty range (start == end+1) instead of handling it separately.
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
- Graph capacity should always be a non-negative integer.
- The path collection should not be empty.
- The alpha parameter's value should be in 0.5..1.0 range.
- Invalid parameter settings for Ascon Hash
- Cash flows list cannot be empty
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/9a646d15d47ad0c6.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/ScapegoatTree/Extensions.cs:41
}
}
/// <summary>
/// Rebuilds a scapegoat tree from list of nodes.
/// Use with <see cref="FlattenTree{TKey}"/> method.
/// </summary>
/// <param name="list">Flattened tree.</param>
/// <param name="start">Start index.</param>
/// <param name="end">End index.</param>
/// <typeparam name="TKey">Scapegoat tree node key type.</typeparam>
/// <returns>Scapegoat tree root node.</returns>
/// <exception cref="ArgumentException">Thrown if start index is invalid.</exception>
public static Node<TKey> RebuildFromList<TKey>(IList<Node<TKey>> list, int start, int end)
where TKey : IComparable
{
if (start > end)
{
throw new ArgumentException("The parameter's value is invalid.", nameof(start));
}
var pivot = Convert.ToInt32(Math.Ceiling(start + (end - start) / 2.0));
return new Node<TKey>(list[pivot].Key)
{
Left = start > (pivot - 1) ? null : RebuildFromList(list, start, pivot - 1),
Right = (pivot + 1) > end ? null : RebuildFromList(list, pivot + 1, end),
};
}
}
View on GitHub (pinned to 96e2905cab)