# Python Program to Create a Mirror Copy of a Tree and Display using BFS Traversal

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

When it is required to create a mirror copy of a tree, and display it using breadth first search, a binary tree class is created with methods set the root element, insert element to left, insert element to right, search for a specific element, and perform post order traversal and so on. An instance of the class is created, and it can be used to access the methods.

Below is the demonstration of the same −

## Example

Live Demo

class BinaryTree_struct:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None

def set_root(self, key):
self.key = key

def insert_to_left(self, new_node):
self.left = new_node

def insert_to_right(self, new_node):
self.right = new_node

def search_elem(self, key):
if self.key == key:
return self
if self.left is not None:
temp = self.left.search_elem(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search_elem(key)
return temp
return None

def copy_mirror(self):
mirror = BinaryTree_struct(self.key)
if self.right is not None:
mirror.left = self.right.copy_mirror()
if self.left is not None:
mirror.right = self.left.copy_mirror()
return mirror

def bfs(self):
queue = [self]
while queue != []:
popped = queue.pop(0)
if popped.left is not None:
queue.append(popped.left)
if popped.right is not None:
queue.append(popped.right)
print(popped.key, end=' ')

my_instance = None

print('Menu (this assumes no duplicate keys)')
print('insert <data> at root')
print('insert <data> left of <data>')
print('insert <data> right of <data>')
print('mirror')
print('quit')

while True:
my_input = input('What operation would you do ? ').split()

operation = my_input[0].strip().lower()
if operation == 'insert':
data = int(my_input[1])
new_node = BinaryTree_struct(data)
suboperation = my_input[2].strip().lower()
if suboperation == 'at':
my_instance = new_node
else:
position = my_input[4].strip().lower()
key = int(position)
ref_node = None
if my_instance is not None:
ref_node = my_instance.search_elem(key)
if ref_node is None:
print('No such key exists..')
continue
if suboperation == 'left':
ref_node.insert_to_left(new_node)
elif suboperation == 'right':
ref_node.insert_to_right(new_node)

elif operation == 'mirror':
if my_instance is not None:
print('Creating a mirror copy...')
mirror = my_instance.copy_mirror()
print('The breadth first search traversal of original tree is : ')
my_instance.bfs()
print()
print('The breadth first traversal of mirror is : ')
mirror.bfs()
print()
elif operation == 'quit':
break

## Output

Menu (this assumes no duplicate keys)
insert <data> at root
insert <data> left of <data>
insert <data> right of <data>
mirror
quit
What operation would you do ? insert 6 at root
What operation would you do ? insert 9 left of 6
What operation would you do ? insert 4 right of 6
What operation would you do ? mirror
Creating a mirror copy...
The breadth first search traversal of original tree is :
6 9 4
The breadth first traversal of mirror is :
6 4 9
What operation would you do ?quit
Use quit() or Ctrl-D (i.e. EOF) to exit

## Explanation

• The ‘BinaryTree_struct’ class with required attributes is created.

• It has an ‘init’ function that is used to assign the left and right nodes to ‘None’.

• A ‘set_root’ method is defined that helps assign a root node to a value.

• It has an ‘insert_to_left’ method that helps add elements to the left node of the tree.

• It has an ‘insert_to_right’ method that helps add elements to the right nodes of the tree.

• It has a ‘bfs’ method that helps perform breadth first search traversal on the tree.

• A method named ‘search_elem’ is defined, that helps search for a specific element.

• It has a ‘copy_mirror’ method that helps create a copy of the binary tree.

• An instance is created and assigned to ‘None’.

• The user input is taken for the operation that needs to be performed.

• Depending on the user’ choice, the operation is performed.

• Relevant output is displayed on the console.

Updated on 16-Apr-2021 12:34:24