A Binary Search Tree (BST) is a binary tree in which each node has a value, and for every node, the values in the left subtree are less than the node's value, and the values in the right subtree are greater than the node's value. BSTs are used in various applications such as searching, sorting, and maintaining a dynamic set of ordered elements.
Consider a BST with the following structure:
10
/ \
5 20
/ \ / \
3 7 15 25
In this BST, the root node is 10. The left subtree of 10 contains values less than 10, and the right subtree contains values greater than 10. This property holds for every node in the tree.
Binary Search Trees have several important properties:
class Node:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, key):
if self.root is None:
self.root = Node(key)
else:
self._insert(self.root, key)
def _insert(self, current, key):
if key < current.val:
if current.left is None:
current.left = Node(key)
else:
self._insert(current.left, key)
else:
if current.right is None:
current.right = Node(key)
else:
self._insert(current.right, key)
def search(self, key):
return self._search(self.root, key)
def _search(self, current, key):
if current is None or current.val == key:
return current
if key < current.val:
return self._search(current.left, key)
return self._search(current.right, key)
def inorder_traversal(self, root):
if root:
self.inorder_traversal(root.left)
print(root.val, end=' ')
self.inorder_traversal(root.right)
# Example usage:
bst = BinarySearchTree()
bst.insert(10)
bst.insert(5)
bst.insert(20)
bst.insert(3)
bst.insert(7)
bst.insert(15)
bst.insert(25)
bst.inorder_traversal(bst.root) # Output: 3 5 7 10 15 20 25
print(bst.search(7)) # Output:
print(bst.search(100)) # Output: None
This code defines a simple binary search tree with methods for inserting nodes, searching for nodes, and performing in-order traversal.
There are several variations of binary search trees, each with distinct characteristics:
Binary Search Trees are a fundamental data structure that provides efficient searching, insertion, and deletion operations. Understanding their components, properties, and variations is crucial for implementing various algorithms and solving complex problems.