{"record":{"id":"95f7df3f33d2235b","repo":"TheAlgorithms/C-Sharp","slug":"avl-tree-is-empty","errorCode":null,"errorMessage":"AVL tree is empty.","messagePattern":"AVL tree is empty\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"DataStructures/AVLTree/AVLTree.cs","lineNumber":133,"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(\"AVL 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(\"AVL tree is empty.\");\n        }\n\n        return GetMax(root).Key;\n    }","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/AVLTree/AVLTree.cs#L115-L151","documentation":"AVLTree.GetMin() throws InvalidOperationException with \"AVL tree is empty.\" when the tree's root is null. Like the AA tree, an empty tree has no minimum, and the library deliberately throws instead of returning a default. Callers are expected to verify the tree is non-empty first.","triggerScenarios":"Calling GetMin() on a new AVLTree, after removing the last key, or on a tree never populated.","commonSituations":"Peeking the smallest key in a priority-like usage before any insert; clearing the tree then querying; constructing from an empty collection then immediately reading a statistic.","solutions":["Check the tree's emptiness (root null / Count == 0) before GetMin","Catch InvalidOperationException when emptiness is a legitimate state","Restructure code so min queries only run after at least one Add","Return a nullable or default value via your own wrapper method"],"exampleFix":"// before\nvar min = avlTree.GetMin();\n// after\nvar min = avlTree.Count == 0 ? default : avlTree.GetMin();","handlingStrategy":"validation","validationCode":"if (avlTree.Count == 0) throw new InvalidOperationException(\"AVL tree empty; GetMin unavailable\");","typeGuard":"bool hasMin(AVLTree<TKey,TValue> t) => t.Count > 0;","tryCatchPattern":"try { var min = avlTree.GetMin(); } catch (InvalidOperationException ex) when (ex.Message == \"AVL tree is empty.\") { min = default; }","preventionTips":["Check Count before GetMin/GetMax","Handle empty inputs when building trees from collections","Wrap min/max accessors in null-or-default helpers","Test empty-tree behavior explicitly"],"tags":["data-structures","avl-tree","empty-collection"],"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"}