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 Sort using a Binary Search Tree
A Binary Search Tree (BST) is a tree data structure where the left subtree contains values less than the root, and the right subtree contains values greater than or equal to the root. The inorder traversal of a BST gives elements in sorted order, making it useful for sorting.
How BST Sorting Works
BST sorting involves two main steps:
Insertion: Add elements to the BST maintaining the BST property
Inorder Traversal: Visit nodes in left-root-right order to get sorted sequence
Implementation
We'll create two classes: BinSearchTreeNode for individual nodes and BinSearchTree for the tree structure ?
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)
# Create BST and add elements
my_bst = BinSearchTree()
numbers = [67, 54, 89, 0, 11, 34, 99]
print("Original numbers:", numbers)
for num in numbers:
my_bst.add_val(num)
print("Sorted numbers: ", end="")
my_bst.inorder_traversal()
Original numbers: [67, 54, 89, 0, 11, 34, 99] Sorted numbers: 0 11 34 54 67 89 99
How It Works
The BinSearchTreeNode class represents individual tree nodes:
__init__: Initializes a node with a key value and sets left, right, and parent to Noneinsert_elem: Inserts a new node maintaining BST property (smaller values go left, larger go right)inorder_traversal: Visits nodes in left-root-right order
The BinSearchTree class manages the entire tree:
add_val: Creates a new node and inserts it into the treeinorder_traversal: Starts inorder traversal from the root
Time Complexity
| Operation | Best Case | Average Case | Worst Case |
|---|---|---|---|
| Insertion | O(log n) | O(log n) | O(n) |
| Traversal | O(n) | O(n) | O(n) |
| Overall Sorting | O(n log n) | O(n log n) | O(n²) |
The worst case occurs when the input is already sorted, creating a skewed tree that resembles a linked list.
Conclusion
BST sorting provides an average time complexity of O(n log n) and demonstrates the elegant property where inorder traversal of a BST yields sorted data. However, it can degrade to O(n²) with skewed inputs, making it less reliable than algorithms like merge sort or heap sort.
