Program to change the root of a binary tree using Python

Suppose we are given a binary tree and need to change its root to a specific leaf node. This transformation involves restructuring the tree by following specific rules to maintain the binary tree structure while making the leaf node the new root.

Transformation Rules

To change the root of a binary tree, we follow these rules ?

  • If a node has a left child, it becomes the right child.

  • A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child.

Node Structure

The node structure of the tree includes parent pointers ?

TreeNode:
    data: <integer>
    left: <pointer of TreeNode>
    right: <pointer of TreeNode>
    parent: <pointer of TreeNode>

Example Visualization

Original Tree: 5 3 7 2 4 6 8 New root: 8 (highlighted) After transformation: Inorder: 2, 3, 4, 5, 7, 6, 8 New root: 8

Algorithm Steps

The algorithm uses a recursive helper function that ?

  • If the current node is the original root, update its parent and remove connections to the new parent

  • Move the left child to the right position

  • Recursively process the parent node

  • Update parent-child relationships

Complete Implementation

import collections

class TreeNode:
    def __init__(self, data, left=None, right=None, parent=None):
        self.data = data
        self.left = left
        self.right = right
        self.parent = parent

def insert(temp, data):
    que = []
    que.append(temp)
    while (len(que)):
        temp = que[0]
        que.pop(0)
        if (not temp.left):
            if data is not None:
                temp.left = TreeNode(data, parent=temp)
            else:
                temp.left = TreeNode(0, parent=temp)
            break
        else:
            que.append(temp.left)
        if (not temp.right):
            if data is not None:
                temp.right = TreeNode(data, parent=temp)
            else:
                temp.right = TreeNode(0, parent=temp)
            break
        else:
            que.append(temp.right)

def make_tree(elements):
    tree = TreeNode(elements[0])
    for element in elements[1:]:
        insert(tree, element)
    return tree

def search_node(root, element):
    if (root == None):
        return None
    if (root.data == element):
        return root
    res1 = search_node(root.left, element)
    if res1:
        return res1
    res2 = search_node(root.right, element)
    return res2

def print_tree(root):
    if root is not None:
        print_tree(root.left)
        print(root.data, end=', ')
        print_tree(root.right)

def solve(root, leaf):
    def helper(node, new_par):
        if node == root:
            node.parent = new_par
            if node.left == new_par:
                node.left = None
            if node.right == new_par:
                node.right = None
            return root
        if node.left:
            node.right = node.left
        if node.parent.left == node:
            node.parent.left = None
        node.left = helper(node.parent, node)
        node.parent = new_par
        return node
    return helper(leaf, None)

# Create tree and change root
root = make_tree([5, 3, 7, 2, 4, 6, 8])
root = solve(root, search_node(root, 8))
print_tree(root)
2, 3, 4, 5, 7, 6, 8, 

How It Works

The algorithm transforms the tree by traversing from the new root (leaf node 8) back to the original root, restructuring relationships at each step. The leaf node 8 becomes the new root, and the inorder traversal shows the transformed tree structure.

Conclusion

This algorithm efficiently changes the root of a binary tree by restructuring parent-child relationships. The key insight is using recursion to traverse from the new root back to the original root while maintaining the binary tree structure.

Updated on: 2026-03-25T21:07:24+05:30

980 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements