{"record":{"id":"c0dee5d4443f16a4","repo":"TheAlgorithms/Python","slug":"binary-search-tree-is-empty","errorCode":null,"errorMessage":"Binary search tree is empty","messagePattern":"Binary search tree is empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/binary_tree/binary_search_tree_recursive.py","lineNumber":202,"sourceCode":"            return False\n\n    def get_max_label(self) -> int:\n        \"\"\"\n        Gets the max label inserted in the tree\n\n        >>> t = BinarySearchTree()\n        >>> t.get_max_label()\n        Traceback (most recent call last):\n            ...\n        ValueError: Binary search tree is empty\n\n        >>> t.put(8)\n        >>> t.put(10)\n        >>> t.get_max_label()\n        10\n        \"\"\"\n        if self.root is None:\n            raise ValueError(\"Binary search tree is empty\")\n\n        node = self.root\n        while node.right is not None:\n            node = node.right\n\n        return node.label\n\n    def get_min_label(self) -> int:\n        \"\"\"\n        Gets the min label inserted in the tree\n\n        >>> t = BinarySearchTree()\n        >>> t.get_min_label()\n        Traceback (most recent call last):\n            ...\n        ValueError: Binary search tree is empty\n\n        >>> t.put(8)","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/binary_tree/binary_search_tree_recursive.py#L184-L220","documentation":"Raised by BinarySearchTree.get_max_label() when self.root is None. The max label lives at the rightmost node; an empty tree has no such node, so the method raises ValueError('Binary search tree is empty') before starting the while-walk. It is a straightforward empty-state guard.","triggerScenarios":"Calling t.get_max_label() on a freshly constructed tree, or after removing all nodes; calling it before any put() in a script.","commonSituations":"Querying max of a tree populated from an empty/filtered dataset; calling statistics helpers before the data-loading step; reusing a tree object after clearing it.","solutions":["Guard the call: `if t.root is not None: max_label = t.get_max_label()`","Use len(t) or the size accessor to check emptiness before querying","Ensure put() calls succeeded earlier — a put that raised error 160 can leave you thinking the tree is populated when it is not"],"exampleFix":"# before\nm = t.get_max_label()  # ValueError if empty\n\n# after\nm = t.get_max_label() if t.root is not None else None","handlingStrategy":"validation","validationCode":"max_label = t.get_max_label() if t.root is not None else None","typeGuard":null,"tryCatchPattern":"try:\n    m = t.get_max_label()\nexcept ValueError:\n    m = None  # empty tree","preventionTips":["Check t.root is not None before min/max queries","Keep a parallel count of successful put() calls to know the tree is populated","Handle the empty case where the data is loaded, not at the query site"],"tags":["binary-tree","empty-state","max","query"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}