{"record":{"id":"c8c8d4b25f11e58b","repo":"TheAlgorithms/Python","slug":"root-root-is-not-present-in-the-binary-tree","errorCode":null,"errorMessage":"root {root} is not present in the binary_tree","messagePattern":"root (.+?) is not present in the binary_tree","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/binary_tree/binary_tree_mirror.py","lineNumber":35,"sourceCode":"    \"\"\"\n    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1)\n    {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}\n    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1)\n    {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]}\n    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5)\n    Traceback (most recent call last):\n        ...\n    ValueError: root 5 is not present in the binary_tree\n    >>> binary_tree_mirror({}, 5)\n    Traceback (most recent call last):\n        ...\n    ValueError: binary tree cannot be empty\n    \"\"\"\n    if not binary_tree:\n        raise ValueError(\"binary tree cannot be empty\")\n    if root not in binary_tree:\n        msg = f\"root {root} is not present in the binary_tree\"\n        raise ValueError(msg)\n    binary_tree_mirror_dictionary = dict(binary_tree)\n    binary_tree_mirror_dict(binary_tree_mirror_dictionary, root)\n    return binary_tree_mirror_dictionary\n\n\nif __name__ == \"__main__\":\n    binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}\n    print(f\"Binary tree: {binary_tree}\")\n    binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5)\n    print(f\"Binary tree mirror: {binary_tree_mirror_dictionary}\")\n","sourceCodeStart":17,"sourceCodeEnd":46,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/binary_tree/binary_tree_mirror.py#L17-L46","documentation":"Raised by binary_tree_mirror(binary_tree, root) when `root not in binary_tree` — the caller-supplied root key is not a node in the adjacency dict. The recursive mirror must start at a node that exists in the dict; a missing key would make the traversal a no-op or KeyError later, so the function validates it explicitly with ValueError.","triggerScenarios":"binary_tree_mirror({1: [2,3], ...}, 5) where 5 is not a key; typos in the root argument; using a value (child id) instead of a key (parent id) as root; passing 1-based vs 0-based ids inconsistently.","commonSituations":"Root id comes from config/user input and refers to a pruned or renamed node; tree parsed with different id normalization than the root constant; stale hardcoded root after data changes.","solutions":["Validate up front: `if root not in tree: raise/return` with your own clearer message","Derive the root programmatically instead of hardcoding (e.g. the key that never appears as a child)","Print/log tree.keys() when the error fires to spot id mismatches (1- vs 0-based, string vs int)"],"exampleFix":"# before\nbinary_tree_mirror({1: [2, 3], 2: [4, 5]}, 5)\n\n# after\nroot = root if root in tree else next(iter(tree))\nbinary_tree_mirror(tree, root)","handlingStrategy":"validation","validationCode":"if root not in binary_tree:\n    raise KeyError(f\"root {root!r} not in tree keys {list(binary_tree)[:10]!r}\")\nbinary_tree_mirror(binary_tree, root)","typeGuard":null,"tryCatchPattern":"try:\n    mirrored = binary_tree_mirror(tree, root)\nexcept ValueError as e:\n    if \"not present\" not in str(e):\n        raise\n    root = next(iter(tree))\n    mirrored = binary_tree_mirror(tree, root)","preventionTips":["Derive the root from the data (a key that never appears as a child) instead of hardcoding","Normalize id types/offsets consistently between tree building and root selection","Print tree.keys() on failure to catch 0- vs 1-based mismatches"],"tags":["binary-tree","invalid-argument","root","mirror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}