{"record":{"id":"31f056b7617a63b7","repo":"TheAlgorithms/C-Sharp","slug":"tree-is-empty","errorCode":null,"errorMessage":"Tree is empty!","messagePattern":"Tree is empty!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/AATree/AATree.cs","lineNumber":97,"sourceCode":"    }\n\n    /// <summary>\n    ///     Checks if the specified element is in the tree.\n    /// </summary>\n    /// <param name=\"key\">The element to look for.</param>\n    /// <returns>true if the element is in the tree, false otherwise.</returns>\n    public bool Contains(TKey key) => Contains(key, Root);\n\n    /// <summary>\n    ///     Gets the largest element in the tree. (ie. the element in the right most node).\n    /// </summary>\n    /// <returns>The largest element in the tree according to the stored comparer.</returns>\n    /// <exception cref=\"InvalidOperationException\">Thrown if the tree is empty.</exception>\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    }\n\n    /// <summary>\n    ///     Gets the smallest element in the tree. (ie. the element in the left most node).\n    /// </summary>\n    /// <returns>The smallest element in the tree according to the stored comparer.</returns>\n    /// <throws>InvalidOperationException if the tree is empty.</throws>\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;","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/AATree/AATree.cs#L79-L115","documentation":"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.","triggerScenarios":"Calling GetMax() on a newly constructed AATree; after removing all elements; after Clear() without reinsertion — e.g. GetMax_EmptyTree_ThrowsCorrectException exercises exactly this path.","commonSituations":"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.","solutions":["Check tree.Count > 0 (or Root non-null) before calling GetMax.","Use TryGet-style logic: wrap in try-catch on InvalidOperationException and provide a default.","Restructure code so GetMax is only called when at least one element has been inserted.","Add an empty-collection code path (return default/optional) in a wrapper method."],"exampleFix":"// before\nvar max = tree.GetMax(); // throws when empty\n// after\nif (tree.Count > 0)\n{\n    var max = tree.GetMax();\n}","handlingStrategy":"validation","validationCode":"if (tree.Count == 0)\n{\n    return default; // or skip the max query entirely\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    max = tree.GetMax();\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Tree is empty!\")\n{\n    max = default; // or throw a domain-specific error\n}","preventionTips":["Check Count before any min/max query on a collection that can be empty.","After bulk removals, re-verify non-emptiness before querying.","Encapsulate GetMax in a TryGetMax helper returning bool.","Write tests for empty-tree queries to catch ordering bugs early."],"tags":["empty-collection","invalid-state","aatree","csharp","data-structures"],"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"}