TheAlgorithms/C-Sharp · error · InvalidOperationException

Tree is empty!

Error message

Tree is empty!

What it means

AATree.GetMax() returns the largest key in the tree according to the stored comparer, but only when the tree is non-empty. When Root is null it throws InvalidOperationException("Tree is empty!"). This is a precondition failure: querying the maximum of an empty collection is undefined, so the library signals it explicitly rather than returning a default.

Solutions

  1. Check tree.Count > 0 (or Root non-null) before calling GetMax.
  2. Use TryGet-style logic: wrap in try-catch on InvalidOperationException and provide a default.
  3. Restructure code so GetMax is only called when at least one element has been inserted.
  4. Add an empty-collection code path (return default/optional) in a wrapper method.

Example fix

// before
var max = tree.GetMax(); // throws when empty
// after
if (tree.Count > 0)
{
    var max = tree.GetMax();
}
Defensive patterns

Strategy: validation

Validate before calling

if (tree.Count == 0)
{
    return default; // or skip the max query entirely
}

Try / catch

try
{
    max = tree.GetMax();
}
catch (InvalidOperationException ex) when (ex.Message == "Tree is empty!")
{
    max = default; // or throw a domain-specific error
}

Prevention

When it happens

Trigger: Calling GetMax() on a newly constructed AATree; after removing all elements; after Clear() without reinsertion — e.g. GetMax_EmptyTree_ThrowsCorrectException exercises exactly this path.

Common situations: Looping over data that can be empty before the first query; logic that removes items and then reads the max without rechecking Count; tests or production code that assume at least one element exists.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


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

Appendix: source

Thrown at DataStructures/AATree/AATree.cs:97

    }

    /// <summary>
    ///     Checks if the specified element is in the tree.
    /// </summary>
    /// <param name="key">The element to look for.</param>
    /// <returns>true if the element is in the tree, false otherwise.</returns>
    public bool Contains(TKey key) => Contains(key, Root);

    /// <summary>
    ///     Gets the largest element in the tree. (ie. the element in the right most node).
    /// </summary>
    /// <returns>The largest element in the tree according to the stored comparer.</returns>
    /// <exception cref="InvalidOperationException">Thrown if the tree is empty.</exception>
    public TKey GetMax()
    {
        if (Root is null)
        {
            throw new InvalidOperationException("Tree is empty!");
        }

        return GetMax(Root).Key;
    }

    /// <summary>
    ///     Gets the smallest element in the tree. (ie. the element in the left most node).
    /// </summary>
    /// <returns>The smallest element in the tree according to the stored comparer.</returns>
    /// <throws>InvalidOperationException if the tree is empty.</throws>
    public TKey GetMin()
    {
        if (Root is null)
        {
            throw new InvalidOperationException("Tree is empty!");
        }

        return GetMin(Root).Key;

View on GitHub (pinned to 96e2905cab)