TheAlgorithms/C-Sharp · error · InvalidOperationException

B-Tree is empty.

Error message

B-Tree is empty.

What it means

This InvalidOperationException is thrown by BTree.GetMin() when the tree is empty (root is null), since there is no minimum key to return. Unlike the Remove path, this is a state error — the operation itself is valid, but the object is not in a state that can satisfy it — hence InvalidOperationException. Callers must ensure the tree has at least one key before querying the minimum.

Solutions

  1. Check the tree is non-empty first (Count > 0 / root != null) before calling GetMin.
  2. Catch InvalidOperationException around GetMin where emptiness is a normal, expected state.
  3. Restructure code to only call GetMin after at least one successful Insert.
  4. Expose or use a TryGetMin-style wrapper in your own code that returns false on empty instead of throwing.

Example fix

// before
var min = btree.GetMin(); // throws if empty

// after
if (btree.Count > 0)
{
    var min = btree.GetMin();
}
else
{
    // handle empty tree: return default, skip, etc.
}
Defensive patterns

Strategy: try-catch

Validate before calling

// C#
if (btree.Count == 0)
{
    // empty — no minimum exists
}
else
{
    var min = btree.GetMin();
}

Type guard

// C#
static bool HasMin<TK>(BTree<TK>? tree) => tree is not null && tree.Count > 0;

Try / catch

// C#
try
{
    var min = btree.GetMin();
}
catch (InvalidOperationException)
{
    // empty tree — use a sentinel/default or skip
}

Prevention

When it happens

Trigger: Calling btree.GetMin() on a newly constructed tree, on a tree whose keys were all removed, or in test helpers (e.g. Constructor_UseCustomComparer_FormsCorrectTree-style flows) that probe the minimum before any Insert.

Common situations: Peeking the minimum during initialization before data is loaded; calling GetMin after a clear/drain operation; async or deferred population where GetMin runs before inserts complete; assuming a default/empty return instead of an exception.

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/3ef5f5b725cff669. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/BTree/BTree.cs:184

    /// <param name="key">Key value to search for.</param>
    /// <returns>Whether or not the key is in the tree.</returns>
    public bool Contains(TKey key)
    {
        return Search(root, key) is not null;
    }

    /// <summary>
    ///     Get the minimum key in the tree.
    /// </summary>
    /// <returns>Minimum key in tree.</returns>
    /// <exception cref="InvalidOperationException">
    ///     Thrown when the tree is empty.
    /// </exception>
    public TKey GetMin()
    {
        if (root is null)
        {
            throw new InvalidOperationException("B-Tree is empty.");
        }

        return GetMin(root);
    }

    /// <summary>
    ///     Get the maximum key in the tree.
    /// </summary>
    /// <returns>Maximum key in tree.</returns>
    /// <exception cref="InvalidOperationException">
    ///     Thrown when the tree is empty.
    /// </exception>
    public TKey GetMax()
    {
        if (root is null)
        {
            throw new InvalidOperationException("B-Tree is empty.");
        }

View on GitHub (pinned to 96e2905cab)