In this tutorial, we will learn how to implement a Binary Search Tree (BST) data structure in Python. A Binary Search Tree is a hierarchical data structure that allows efficient insertion, deletion, and search operations.
A BST consists of nodes, where each node contains a value and references to its left and right subtrees. The left subtree contains values less than the node value, while the right subtree contains values greater than the node value.
Here is an example representation of a BST:
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
First, let’s start with implementing the BST node class in Python. Each node will have a value, a left child, and a right child.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
To insert a value into the BST, we need to find the appropriate position based on the BST property and create a new node with the given value.
def insert(root, value):
if root is None:
return Node(value)
if value < root.value:
root.left = insert(root.left, value)
else:
root.right = insert(root.right, value)
return root
To search for a value in the BST, we compare the value with the current node’s value. If it matches, we have found the value. If it is less, we search in the left subtree. If it is greater, we search in the right subtree. We repeat this process recursively until we find the value or reach a leaf node.
def search(root, value):
if root is None or root.value == value:
return root
if value < root.value:
return search(root.left, value)
return search(root.right, value)
There are three commonly used methods to traverse a BST: inorder, preorder, and postorder. In this tutorial, we will focus on the inorder traversal, which visits the nodes in ascending order.
def inorder(root):
if root is not None:
inorder(root.left)
print(root.value)
inorder(root.right)
Let’s test our BST implementation with some sample code:
# Creating the BST
root = None
root = insert(root, 8)
root = insert(root, 3)
root = insert(root, 10)
root = insert(root, 1)
root = insert(root, 6)
root = insert(root, 14)
root = insert(root, 4)
root = insert(root, 7)
root = insert(root, 13)
# Searching for a value
found_node = search(root, 6)
if found_node:
print("Value found:", found_node.value)
else:
print("Value not found")
# Inorder traversal
print("Inorder traversal:")
inorder(root)
Value found: 6
Inorder traversal:
1
3
4
6
7
8
10
13
14
Congratulations! You have successfully implemented a Binary Search Tree in Python. This data structure is widely used in many applications, including efficient searching and sorting algorithms. Understanding and implementing data structures like BSTs are essential for becoming proficient in software development.
You can further explore BST operations such as deletion and more advanced concepts like balancing the tree. Happy coding!
I hope you found this tutorial helpful. If you have any questions or feedback, please leave a comment below.
References: