Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input


When it is required to build a binary tree by taking input using inorder or postorder traversal, a class is defined, that has methods to set the root element, perform inorder traversal, perform post order traversal. It can be used by creating an instance of the class.

Below is a 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 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 post_order_traversal(self):
      if self.left is not None:
         self.left.post_order_traversal()
      if self.right is not None:
         self.right.post_order_traversal()
      print(self.key, end=' ')

def construct_btree(post_ord, in_ord):
   if post_ord == [] or in_ord == []:
      return None
   key = post_ord[-1]
   node = BinaryTree_struct(key)
   index = in_ord.index(key)
   node.left = construct_btree(post_ord[:index], in_ord[:index])
   node.right = construct_btree(post_ord[index:-1], in_ord[index + 1:])
   return node

post_ord = input('The input for post-order traversal is : ').split()
post_ord = [int(x) for x in post_ord]
in_ord = input('The input for in-order traversal is : ').split()
in_ord = [int(x) for x in in_ord]

my_instance = construct_btree(post_ord, in_ord)
print('Binary tree has been constructed...')
print('Verification in process..')
print('Post-order traversal is... ', end='')
my_instance.post_order_traversal()
print()
print('In-order traversal is... ', end='')
my_instance.inorder_traversal()
print()

Output

The input for post-order traversal is : 1 2 3 4 5
The input for in-order traversal is : 5 4 3 2 1
Binary tree has been constructed...
Verification in process..
Post-order traversal is... 1 2 3 4 5
In-order traversal is... 5 4 3 2 1

Explanation

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

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

  • It has a ‘set_root’ method that helps set the root of the binary tree.

  • Another method named ‘inorder_traversal’ that performs in-order traversal, i.e Left→Node→Right.

  • Another method named ‘post_order_traversal’ is defined that helps traverse through the tree in post order, i.e Left→Right→Node.

  • A method named ‘construct_btree’ is defined, that helps construct a binary tree using the elements that have been previously specified.

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

  • An object of the ‘BinaryTree_struct’ class is created.

  • The ‘construct_btree’ method is used to construct a binary tree by taking the elements that were previously specified.

  • The post order traversal and in order traversal are performed on this tree.

  • Relevant output is displayed on the console.

Updated on: 15-Apr-2021

283 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements