TheAlgorithms/Python · error · ValueError

root {root} is not present in the binary_tree

Error message

root {root} is not present in the binary_tree

What it means

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.

Source

Thrown at data_structures/binary_tree/binary_tree_mirror.py:35

    """
    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1)
    {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]}
    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 1)
    {1: [3, 2], 2: [5, 4], 3: [7, 6], 4: [11, 10]}
    >>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 4: [10,11]}, 5)
    Traceback (most recent call last):
        ...
    ValueError: root 5 is not present in the binary_tree
    >>> binary_tree_mirror({}, 5)
    Traceback (most recent call last):
        ...
    ValueError: binary tree cannot be empty
    """
    if not binary_tree:
        raise ValueError("binary tree cannot be empty")
    if root not in binary_tree:
        msg = f"root {root} is not present in the binary_tree"
        raise ValueError(msg)
    binary_tree_mirror_dictionary = dict(binary_tree)
    binary_tree_mirror_dict(binary_tree_mirror_dictionary, root)
    return binary_tree_mirror_dictionary


if __name__ == "__main__":
    binary_tree = {1: [2, 3], 2: [4, 5], 3: [6, 7], 7: [8, 9]}
    print(f"Binary tree: {binary_tree}")
    binary_tree_mirror_dictionary = binary_tree_mirror(binary_tree, 5)
    print(f"Binary tree mirror: {binary_tree_mirror_dictionary}")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate up front: `if root not in tree: raise/return` with your own clearer message
  2. Derive the root programmatically instead of hardcoding (e.g. the key that never appears as a child)
  3. Print/log tree.keys() when the error fires to spot id mismatches (1- vs 0-based, string vs int)

Example fix

# before
binary_tree_mirror({1: [2, 3], 2: [4, 5]}, 5)

# after
root = root if root in tree else next(iter(tree))
binary_tree_mirror(tree, root)
Defensive patterns

Strategy: validation

Validate before calling

if root not in binary_tree:
    raise KeyError(f"root {root!r} not in tree keys {list(binary_tree)[:10]!r}")
binary_tree_mirror(binary_tree, root)

Try / catch

try:
    mirrored = binary_tree_mirror(tree, root)
except ValueError as e:
    if "not present" not in str(e):
        raise
    root = next(iter(tree))
    mirrored = binary_tree_mirror(tree, root)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/c8c8d4b25f11e58b. Report an issue: GitHub.