{"record":{"id":"f98ef909ae146771","repo":"TheAlgorithms/C-Sharp","slug":"tree-is-empty-redblacktree","errorCode":null,"errorMessage":"Tree is empty!","messagePattern":"Tree is empty!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/RedBlackTree/RedBlackTree.cs","lineNumber":209,"sourceCode":"            }\n            else\n            {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    /// <summary>\n    ///     Get the minimum value in the tree.\n    /// </summary>\n    /// <returns>Minimum value in tree.</returns>\n    public TKey GetMin()\n    {\n        if (root is null)\n        {\n            throw new InvalidOperationException(\"Tree is empty!\");\n        }\n\n        return GetMin(root).Key;\n    }\n\n    /// <summary>\n    ///     Get the maximum value in the tree.\n    /// </summary>\n    /// <returns>Maximum value in tree.</returns>\n    public TKey GetMax()\n    {\n        if (root is null)\n        {\n            throw new InvalidOperationException(\"Tree is empty!\");\n        }\n\n        return GetMax(root).Key;\n    }","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/RedBlackTree/RedBlackTree.cs#L191-L227","documentation":"RedBlackTree<TKey>.GetMin throws InvalidOperationException when the tree's root is null, i.e. no keys have been inserted. The minimum is defined only over existing nodes, so an empty tree is an error rather than a default value.","triggerScenarios":"Calling GetMin on a RedBlackTree constructed but never populated via Add, or after all keys were removed.","commonSituations":"Querying min before initialization, trees cleared between phases, or generic code that assumes the tree is non-empty (e.g. in constructor tests with custom comparers).","solutions":["Check the tree's Count/IsEmpty (or track insertion count) before calling GetMin.","Ensure Add is called at least once before querying min.","Catch InvalidOperationException and return a sentinel/default for empty trees.","If trees are frequently empty, wrap access in a helper returning TKey?."],"exampleFix":"// before\nvar min = tree.GetMin(); // throws if empty\n// after\nif (tree.Count > 0) { var min = tree.GetMin(); }","handlingStrategy":"validation","validationCode":"var min = tree.Count > 0 ? tree.GetMin() : default;","typeGuard":null,"tryCatchPattern":"try\n{\n    var min = tree.GetMin();\n}\ncatch (InvalidOperationException)\n{\n    // tree empty: no minimum\n}","preventionTips":["Check Count > 0 before GetMin.","Ensure at least one Add precedes min queries.","Return TKey? via a helper for possibly-empty trees.","Test min/max on empty trees explicitly."],"tags":["data-structures","red-black-tree","empty-collection","csharp"],"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"}