
- 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 Program to Sort using a Binary Search Tree
When it is required to sort a binary search tree, a class is created, and methods are defined inside it that perform operations like inserting an element, and performing inorder traversal.
Below is a demonstration of the same −
Example
class BinSearchTreeNode: 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 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() class BinSearchTree: def __init__(self): self.root = None def inorder_traversal(self): if self.root is not None: self.root.inorder_traversal() def add_val(self, key): new_node = BinSearchTreeNode(key) if self.root is None: self.root = new_node else: self.root.insert_elem(new_node) my_instance = BinSearchTree() my_list = input('Enter the list of numbers... ').split() my_list = [int(x) for x in my_list] for x in my_list: my_instance.add_val(x) print('Sorted list: ') print(my_instance.inorder_traversal())
Output
Enter the list of numbers... 67 54 89 0 11 34 99 Sorted list: 0 11 34 54 67 89 99
Explanation
The ‘BinSearchTreeNode’ class with required attributes is created.
It has an ‘init’ function that is used to assign ‘left’, ‘right’ and ‘parent’ nodes to ‘None’.
Another method named ‘insert_elem’ is defined that helps add nodes to the tree.
Another method named ‘inorder_traversal’ is defined, that helps perform inorder traversal on the tree.
Another class named ‘BinSearchTree’ is defined.
It sets the root to ‘None.
It has a method named ‘inorder_traversal’ that helps perform inorder traversal on the tree.
Another method named ‘add_val’ is defined that helps add nodes to the tree.
An instance of the ‘BinSearchTree’ is created.
The list of numbers is taken by user.
A tree is created using this.
The list is sorted, and its inorder traversal is performed.
Relevant output is displayed on the console.
- Related Articles
- Python Program for Depth First Binary Tree Search using Recursion
- C++ Program to Implement a Binary Search Tree using Linked Lists
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- Program to create linked list to binary search tree in Python
- Program to fix a erroneous binary tree using Python
- C++ Program to Search for an Element in a Binary Search Tree
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Validate Binary Search Tree in Python
- Program to find largest binary search subtree from a given tree in Python
- C++ Program to Implement Randomized Binary Search Tree
- Binary Tree to Binary Search Tree Conversion in C++
- Program to find the kth smallest element in a Binary Search Tree in Python
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- C++ Program to Implement self Balancing Binary Search Tree
- C++ Program to Perform Dictionary Operations in a Binary Search Tree
