Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Print only Nodes in Left SubTree
When it is required to print the nodes in the left subtree, a binary tree class can be created with methods to perform operations like setting the root node, inserting elements, and traversing the tree. This program demonstrates how to build a binary tree and display only the nodes in the left subtree.
A binary tree is a hierarchical data structure where each node has at most two children: a left child and a right child. The left subtree contains all nodes that are descendants of the left child of the root node.
Binary Tree Implementation
class BinaryTree:
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_subtree(self):
if self.left is not None:
self.left.inorder_traversal()
# Create a binary tree
root = BinaryTree(10)
root.insert_at_left(BinaryTree(5))
root.insert_at_right(BinaryTree(15))
root.left.insert_at_left(BinaryTree(3))
root.left.insert_at_right(BinaryTree(7))
print("Left subtree nodes: ", end="")
root.print_left_subtree()
print()
The output of the above code is ?
Left subtree nodes: 3 5 7
Interactive Binary Tree Program
Here's a more comprehensive program that allows interactive tree building ?
class BinaryTree:
def __init__(self, data=None):
self.key = data
self.left = None
self.right = None
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_subtree(self):
if self.left is not None:
self.left.inorder_traversal()
my_tree = None
print('Menu (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:
user_input = input('What operation would you like to perform? ').split()
operation = user_input[0].strip().lower()
if operation == 'insert':
data = int(user_input[1])
new_node = BinaryTree(data)
suboperation = user_input[2].strip().lower()
if suboperation == 'at':
my_tree = new_node
else:
position = user_input[4].strip().lower()
key = int(position)
ref_node = None
if my_tree is not None:
ref_node = my_tree.search_elem(key)
if ref_node is None:
print('No such key found')
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 in the left subtree: ', end='')
if my_tree is not None:
my_tree.print_left_subtree()
print()
else:
print('Tree is empty')
elif operation == 'quit':
break
How It Works
The print_left_subtree() method works by checking if the root node has a left child. If it does, it performs an inorder traversal of the entire left subtree, which visits all nodes in the left subtree in the order: left ? root ? right.
In this tree structure, the left subtree (highlighted in orange) contains nodes 5, 3, and 7. When print_left_subtree() is called on the root, it traverses only these nodes.
Key Features
- BinaryTree Class: Contains the tree structure with left and right pointers
- Inorder Traversal: Visits nodes in left-root-right order
- Search Function: Finds a specific node by its key value
- Left Subtree Display: Shows only nodes in the left subtree
Conclusion
This program demonstrates how to build a binary tree and extract nodes from the left subtree only. The print_left_subtree() method performs inorder traversal on the left child, displaying all nodes in that subtree in sorted order.
