Python Program To Find the Smallest and Largest Elements in the Binary Search Tree

When it is required to find the smallest and the largest elements in a binary search tree, a binary tree class is created, and methods to add elements to the tree, search for a specific node are defined. An instance of the class is created, and is used with these methods.
Below is a demonstration of the same −
 Live Demo
class BST_Node:
   def __init__(self, key):
      self.key = key
      self.left = None
      self.right = None
      self.parent = None

   def insert_elem(self, node):
      if self.key > node.key:
         if self.left is None:
            self.left = node
            node.parent = self
         else:
            self.left.insert_elem(node)
      elif self.key < node.key:
         if self.right is None:
            self.right = node
            node.parent = self
         else:
            self.right.insert_elem(node)

   def search_node(self, key):
      if self.key > key:
         if self.left is not None:
            return self.left.search_node(key)
         else:
            return None
      elif self.key < key:
         if self.right is not None:
            return self.right.search_node(key)
         else:
            return None
      return self

class BSTree:
   def __init__(self):
      self.root = None

   def add_elem(self, key):
      new_node = BST_Node(key)
      if self.root is None:
         self.root = new_node
      else:
         self.root.insert_elem(new_node)

   def search_node(self, key):
      if self.root is not None:
         return self.root.search_node(key)

   def get_smallest_elem(self):
      if self.root is not None:
         current = self.root
         while current.left is not None:
            current = current.left
         return current.key

   def get_largest_elem(self):
      if self.root is not None:
         current = self.root
         while current.right is not None:
            current = current.right
         return current.key

my_instance = BSTree()

print('Menu (Assume no duplicate keys)')
print('add ')
print('smallest')
print('largest')
print('quit')

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

   operation = my_input[0].strip().lower()
   if operation == 'add':
      key = int(my_input[1])
      my_instance.add_elem(key)
   if operation == 'smallest':
      smallest = my_instance.get_smallest_elem()
      print('The smallest element is : {}'.format(smallest))
   if operation == 'largest':
      largest = my_instance.get_largest_elem()
      print('The largest element is : {}'.format(largest))
   elif operation == 'quit':
      break
Menu (Assume no duplicate keys)
add <key>
smallest
largest
quit
What operation would you perform ? add 5
What operation would you perform ? add 8
What operation would you perform ? add 11
What operation would you perform ? add 0
What operation would you perform ? add 3
What operation would you perform ? smallest
The smallest element is : 0
What operation would you perform ? largest
The largest element is : 11
What operation would you perform ? quit’
The ‘BST_Node’ class with required attributes is created.
The ‘BST_Node’ class with required attributes is created.
It has an ‘init’ function that is used to set the left, right and parent nodes to ‘None’.
It has an ‘init’ function that is used to set the left, right and parent nodes to ‘None’.
It has an ‘insert_element’ method that helps insert an element into the binary tree.
It has an ‘insert_element’ method that helps insert an element into the binary tree.
Another method named ‘search_node’ that searches for a specific node in the tree.
Another method named ‘search_node’ that searches for a specific node in the tree.
Another class named ‘BSTree’ is defined, where the root is set to ‘None’.
Another class named ‘BSTree’ is defined, where the root is set to ‘None’.
A method named ‘add_elem’ is defined that adds elements to the tree.
A method named ‘add_elem’ is defined that adds elements to the tree.
There is another method named ‘search_node’ that helps search for a specific node in the tree.
There is another method named ‘search_node’ that helps search for a specific node in the tree.
Another method named ‘get_smallest_node’ is defined that helps fetch the smallest node in the tree.
Another method named ‘get_smallest_node’ is defined that helps fetch the smallest node in the tree.
Another method named ‘get_largest_node’ is defined that helps fetch the largest node in the tree.
Another method named ‘get_largest_node’ is defined that helps fetch the largest node in the tree.
An object of the ‘BSTree’ class is created.
An object of the ‘BSTree’ class is created.
Based on the operation chosen by the user, the operation is performed.
Based on the operation chosen by the user, the operation is performed.