{"record":{"id":"677f1c36cf4d7672","repo":"TheAlgorithms/Python","slug":"node-with-label-label-does-not-exist","errorCode":null,"errorMessage":"Node with label {label} does not exist","messagePattern":"Node with label (.+?) does not exist","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/binary_tree/binary_search_tree_recursive.py","lineNumber":107,"sourceCode":"        Searches a node in the tree\n\n        >>> t = BinarySearchTree()\n        >>> t.put(8)\n        >>> t.put(10)\n        >>> node = t.search(8)\n        >>> assert node.label == 8\n\n        >>> node = t.search(3)\n        Traceback (most recent call last):\n            ...\n        ValueError: Node with label 3 does not exist\n        \"\"\"\n        return self._search(self.root, label)\n\n    def _search(self, node: Node | None, label: int) -> Node:\n        if node is None:\n            msg = f\"Node with label {label} does not exist\"\n            raise ValueError(msg)\n        elif label < node.label:\n            node = self._search(node.left, label)\n        elif label > node.label:\n            node = self._search(node.right, label)\n\n        return node\n\n    def remove(self, label: int) -> None:\n        \"\"\"\n        Removes a node in the tree\n\n        >>> t = BinarySearchTree()\n        >>> t.put(8)\n        >>> t.put(10)\n        >>> t.remove(8)\n        >>> assert t.root.label == 10\n\n        >>> t.remove(3)","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/binary_tree/binary_search_tree_recursive.py#L89-L125","documentation":"Raised by BinarySearchTree.search() (via _search) when the recursion walks off the tree without finding the label. _search descends left/right by comparison; reaching a None node means the key is absent, so a ValueError (not KeyError) signals a missing node. Also fires on an empty tree, since self.root is None immediately.","triggerScenarios":"t.search(label) where label was never inserted; searching an empty tree (`BinarySearchTree().search(5)`); searching for a value removed earlier via t.remove().","commonSituations":"Looking up user-supplied IDs that may not exist; searching after deletions; forgetting to populate the tree before a lookup phase.","solutions":["Wrap search in try/except ValueError and treat it as a miss (this is the intended API contract, per the doctest)","Validate the key exists first by scanning `label in (n.label for n in t.inorder_traversal())` when performance is not critical","Ensure insertion completed before the lookup phase (check for failed earlier put calls that raised error 160)"],"exampleFix":"# before\nnode = t.search(maybe_missing)  # ValueError\n\n# after\ntry:\n    node = t.search(maybe_missing)\nexcept ValueError:\n    node = None","handlingStrategy":"try-catch","validationCode":"def contains(t, label: int) -> bool:\n    try:\n        t.search(label)\n        return True\n    except ValueError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    node = t.search(label)\nexcept ValueError:\n    node = None  # documented miss path, per the module's doctest","preventionTips":["Wrap every search() call — the API is designed to raise on miss","For bulk membership tests, iterate inorder_traversal once into a set instead of N searches","Remember search on an empty tree also raises; check t.root first when relevant"],"tags":["binary-tree","lookup","missing-key","search"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}