- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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 −
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()
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
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.
- Related Articles
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Binary Tree Postorder Traversal in Python
- Binary Tree Inorder Traversal in Python
- Binary Tree Postorder Traversal in Python Programming
- Program to perform an Inorder Traversal of a binary tree in Python
- Construct a Binary Tree from Postorder and Inorder in Python
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- C++ Program to Perform Postorder Recursive Traversal of a Given Binary Tree
- C++ Program to Perform Inorder Recursive Traversal of a Given Binary Tree
- Golang program to perform inorder tree traversal
- C++ Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
- Golang Program to Perform the postorder tree traversal
- C++ Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
- Java Program to Perform the inorder tree traversal