{"record":{"id":"3ef5f5b725cff669","repo":"TheAlgorithms/C-Sharp","slug":"b-tree-is-empty","errorCode":null,"errorMessage":"B-Tree is empty.","messagePattern":"B-Tree is empty\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/BTree/BTree.cs","lineNumber":184,"sourceCode":"    /// <param name=\"key\">Key value to search for.</param>\n    /// <returns>Whether or not the key is in the tree.</returns>\n    public bool Contains(TKey key)\n    {\n        return Search(root, key) is not null;\n    }\n\n    /// <summary>\n    ///     Get the minimum key in the tree.\n    /// </summary>\n    /// <returns>Minimum key in tree.</returns>\n    /// <exception cref=\"InvalidOperationException\">\n    ///     Thrown when the tree is empty.\n    /// </exception>\n    public TKey GetMin()\n    {\n        if (root is null)\n        {\n            throw new InvalidOperationException(\"B-Tree is empty.\");\n        }\n\n        return GetMin(root);\n    }\n\n    /// <summary>\n    ///     Get the maximum key in the tree.\n    /// </summary>\n    /// <returns>Maximum key in tree.</returns>\n    /// <exception cref=\"InvalidOperationException\">\n    ///     Thrown when the tree is empty.\n    /// </exception>\n    public TKey GetMax()\n    {\n        if (root is null)\n        {\n            throw new InvalidOperationException(\"B-Tree is empty.\");\n        }","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BTree/BTree.cs#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the tree is non-empty first (Count > 0 / root != null) before calling GetMin.","Catch InvalidOperationException around GetMin where emptiness is a normal, expected state.","Restructure code to only call GetMin after at least one successful Insert.","Expose or use a TryGetMin-style wrapper in your own code that returns false on empty instead of throwing."],"exampleFix":"// before\nvar min = btree.GetMin(); // throws if empty\n\n// after\nif (btree.Count > 0)\n{\n    var min = btree.GetMin();\n}\nelse\n{\n    // handle empty tree: return default, skip, etc.\n}","handlingStrategy":"try-catch","validationCode":"// C#\nif (btree.Count == 0)\n{\n    // empty — no minimum exists\n}\nelse\n{\n    var min = btree.GetMin();\n}","typeGuard":"// C#\nstatic bool HasMin<TK>(BTree<TK>? tree) => tree is not null && tree.Count > 0;","tryCatchPattern":"// C#\ntry\n{\n    var min = btree.GetMin();\n}\ncatch (InvalidOperationException)\n{\n    // empty tree — use a sentinel/default or skip\n}","preventionTips":["Only call GetMin after at least one successful Insert.","After clearing or draining the tree, guard subsequent queries with a Count check.","Wrap GetMin in a TryGetMin helper for call sites where emptiness is normal.","In async/deferred loading, sequence GetMin behind population completion."],"tags":["invalidoperationexception","b-tree","getmin","empty-tree"],"backgroundTag":"empty-result-set","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"}