Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Binary Search Trees quickly and effectively.
417+ students studying
A Binary Search Tree (BST) is a hierarchical data structure where each node has at most two children, referred to as the left and right child. The defining characteristic of a BST is that for any node:
A typical BST implementation uses classes to define the structure and behaviour.
Example in Python:
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, value):
if not self.root:
self.root = TreeNode(value)
else:
self._insert(self.root, value)
def _insert(self, current, value):
if value < current.value:
if current.left:
self._insert(current.left, value)
else:
current.left = TreeNode(value)
else:
if current.right:
self._insert(current.right, value)
else:
current.right = TreeNode(value)
Traversal involves visiting all nodes in a specific order.
Common traversal methods include:
Visits nodes in ascending order for a BST.
def inorder(node):
if node:
inorder(node.left)
print(node.value, end=" ")
inorder(node.right)
Visits nodes in the order they would be created in a depth-first manner.
def preorder(node):
if node:
print(node.value, end=" ")
preorder(node.left)
preorder(node.right)
Useful for operations like deleting the tree from leaf to root.
def postorder(node):
if node:
postorder(node.left)
postorder(node.right)
print(node.value, end=" ")
When inserting data:
Example:
def insert(node, value):
if not node:
return TreeNode(value)
if value < node.value:
node.left = insert(node.left, value)
else:
node.right = insert(node.right, value)
return node
Removing a node requires handling three cases:
Example:
def delete_node(node, value):
if not node:
return node
if value < node.value:
node.left = delete_node(node.left, value)
elif value > node.value:
node.right = delete_node(node.right, value)
else:
# Node with only one child or no child
if not node.left:
return node.right
elif not node.right:
return node.left
# Node with two children
temp = min_value_node(node.right)
node.value = temp.value
node.right = delete_node(node.right, temp.value)
return node
def min_value_node(node):
current = node
while current.left:
current = current.left
return current
A BST can also be represented indirectly using arrays, but this is less common than using linked structures. Typically, array indices are used to simulate parent-child relationships.
Example:
This method is more suitable for Complete Binary Trees.
bst = BinarySearchTree()
bst.insert(10)
bst.insert(5)
bst.insert(15)
bst.insert(7)
print("Inorder Traversal:")
inorder(bst.root) # Output: 5 7 10 15
bst.root = delete_node(bst.root, 10)
print("Inorder after deletion:")
inorder(bst.root) # Output: 5 7 15
Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!
90 flashcards
Flashcards on Binary Search Trees
Revise key concepts with interactive flashcards.
Try Computer Science Flashcards9 quizzes
Quizzes on Binary Search Trees
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Binary Search Trees
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Binary Search Trees
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Binary Search Trees
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Binary Search Trees to Deepen Your Understanding and Improve Your Mastery
Join 500,000+ A-Level students using SimpleStudy...
Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!
Report Improved Results
Recommend to friends
Students Supported
Questions answered