# Python Program to Print only Nodes in Left SubTree

PythonServer Side ProgrammingProgramming

When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.

Below is a demonstration of the same −

## Example

Live Demo

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

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

def inorder_traversal(self):
if self.left is not None:
self.left.inorder_traversal()
print(self.key, end=' ')
if self.right is not None:
self.right.inorder_traversal()

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

def insert_at_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 print_left_part(self):
if self.left is not None:
self.left.inorder_traversal()

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('left')
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')
continue
if suboperation == 'left':
ref_node.insert_at_left(new_node)
elif suboperation == 'right':
ref_node.insert_at_right(new_node)

elif operation == 'left':
print('Nodes of the left subtree are : ', end='')
if my_instance is not None:
my_instance.print_left_part()
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>
left
quit
What operation would you do ? insert 5 at root
What operation would you do ? insert 6 left of 5
What operation would you do ? insert 8 right of 5
What operation would you do ? left
Nodes of the left subtree are : 6
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 set the root value of the binary tree.

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

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

• Another method named ‘inorder_traversal’ that performs in order traversal.

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

• Another method named ‘print_left_part’ is defined, that helps display only the left part of the binary tree on the console.

• 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.

Published on 16-Apr-2021 12:41:09