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 Find the Largest value in a Tree using Inorder Traversal
Finding the largest value in a binary tree using inorder traversal is a common tree operation. This approach visits nodes in left-root-right order while tracking the maximum value encountered during traversal.
Below is a complete implementation using a binary tree class with methods for tree construction and largest value finding ?
Complete Implementation
class BinaryTree:
def __init__(self, key=None):
self.key = key
self.left = None
self.right = None
def set_root(self, key):
self.key = key
def inorder_traversal_largest(self):
largest = []
self.inorder_largest_helper(largest)
return largest[0] if largest else None
def inorder_largest_helper(self, largest):
# Traverse left subtree
if self.left is not None:
self.left.inorder_largest_helper(largest)
# Process current node
if largest == []:
largest.append(self.key)
elif largest[0] < self.key:
largest[0] = self.key
# Traverse right subtree
if self.right is not None:
self.right.inorder_largest_helper(largest)
def insert_to_left(self, new_node):
self.left = new_node
def insert_to_right(self, new_node):
self.right = new_node
def search_node(self, key):
if self.key == key:
return self
if self.left is not None:
temp = self.left.search_node(key)
if temp is not None:
return temp
if self.right is not None:
temp = self.right.search_node(key)
return temp
return None
# Create and build a sample tree
root = BinaryTree(8)
root.insert_to_left(BinaryTree(3))
root.insert_to_right(BinaryTree(10))
root.left.insert_to_left(BinaryTree(1))
root.left.insert_to_right(BinaryTree(6))
root.right.insert_to_right(BinaryTree(14))
print("Tree structure:")
print(" 8")
print(" / ")
print(" 3 10")
print(" / \ ")
print(" 1 6 14")
print()
# Find the largest value
largest_value = root.inorder_traversal_largest()
print(f"The largest element in the tree is: {largest_value}")
Tree structure:
8
/ \
3 10
/ \ \
1 6 14
The largest element in the tree is: 14
How Inorder Traversal Works
The inorder traversal visits nodes in this sequence ?
class BinaryTree:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def inorder_display(self):
if self.left:
self.left.inorder_display()
print(self.key, end=" ")
if self.right:
self.right.inorder_display()
# Create the same tree
root = BinaryTree(8)
root.left = BinaryTree(3)
root.right = BinaryTree(10)
root.left.left = BinaryTree(1)
root.left.right = BinaryTree(6)
root.right.right = BinaryTree(14)
print("Inorder traversal sequence:")
root.inorder_display()
print("\nLargest value found: 14")
Inorder traversal sequence: 1 3 6 8 10 14 Largest value found: 14
Alternative Approach Using Simple Recursion
A more direct approach without using a list to track the maximum ?
class BinaryTree:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def find_max_inorder(self):
max_val = float('-inf')
def inorder_helper(node):
nonlocal max_val
if node:
inorder_helper(node.left)
max_val = max(max_val, node.key)
inorder_helper(node.right)
inorder_helper(self)
return max_val
# Test with the same tree
root = BinaryTree(8)
root.left = BinaryTree(3)
root.right = BinaryTree(10)
root.left.left = BinaryTree(1)
root.left.right = BinaryTree(6)
root.right.right = BinaryTree(14)
print(f"Maximum value using alternative approach: {root.find_max_inorder()}")
Maximum value using alternative approach: 14
Key Points
- Inorder traversal visits nodes in left-root-right sequence
- The algorithm compares each node's value during traversal
- Time complexity is O(n) where n is the number of nodes
- Space complexity is O(h) where h is the height of the tree (due to recursion)
Conclusion
Finding the largest value using inorder traversal requires visiting all nodes and comparing their values. While inorder traversal provides a systematic approach, a simple recursive traversal without following inorder sequence would be more efficient for this specific task.
