Photo AI

Last Updated Sep 27, 2025

Binary Search Trees Simplified Revision Notes

Revision notes with simplified explanations to understand Binary Search Trees quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

417+ students studying

Binary Search Trees

Overview

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:

  • The left subtree contains values less than the node's value.
  • The right subtree contains values greater than the node's value. This property makes BSTs highly efficient for searching, insertion, and deletion operations, with average time complexities of O(log n) for balanced trees.

Structure of a BST

  • Node: Contains data, a reference to the left child, and a reference to the right child.
  • Root: The topmost node of the tree.
  • Leaf: A node with no children.
  • Subtree: A tree consisting of a node and its descendants.

Creating a Binary Search Tree

Using Object-Oriented Programming (OOP)

A typical BST implementation uses classes to define the structure and behaviour.

lightbulbExample

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)

Traversing a BST

Traversal involves visiting all nodes in a specific order.

Common traversal methods include:

Inorder Traversal (Left, Root, Right):

Visits nodes in ascending order for a BST.

def inorder(node):
    if node:
        inorder(node.left)
        print(node.value, end=" ")
        inorder(node.right)

Preorder Traversal (Root, Left, 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)

Postorder Traversal (Left, Right, Root):

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=" ")

Adding Data to a BST

When inserting data:

  1. Start at the root.
  2. Compare the value to be inserted with the current node:
  • If smaller, move to the left.
  • If larger, move to the right.
  1. Repeat until an empty position is found.
lightbulbExample

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 Data from a BST

Removing a node requires handling three cases:

  1. Node with no children (Leaf Node):
  • Simply remove the node.
  1. Node with one child:
  • Replace the node with its child.
  1. Node with two children:
  • Replace the node's value with its in-order successor (the smallest value in the right subtree).
  • Remove the successor node.
lightbulbExample

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

Using Arrays (Procedural Approach)

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.

lightbulbExample

Example:

  • Left child index: 2 * i + 1
  • Right child index: 2 * i + 2

This method is more suitable for Complete Binary Trees.

Examples of Usage

Building a BST and Traversing:

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

Deleting a Node:

bst.root = delete_node(bst.root, 10)
print("Inorder after deletion:")
inorder(bst.root)  # Output: 5 7 15

Note Summary

infoNote

Common Mistakes

  1. Forgetting Edge Cases:
  • Not handling empty trees or deletion of nodes with no or one child properly.
  1. Confusing Traversal Orders:
  • Ensure the correct traversal method is applied based on the problem context.
  1. Misplacing Nodes During Insertion:
  • Incorrectly inserting a node can break the BST properties, leading to inefficient operations.
  1. Ignoring Tree Balance:
  • Unbalanced trees can degrade performance. Consider balanced trees (e.g., AVL trees) for large datasets.
infoNote

Key Takeaways

  • A Binary Search Tree (BST) is a hierarchical structure with efficient searching, insertion, and deletion.
  • Traversal methods like inorder, preorder, and postorder are crucial for working with BSTs.
  • Focus on understanding the principles of BST operations rather than memorising specific implementations.
  • Practice implementing and tracing BSTs in various contexts to deepen your understanding.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Binary Search Trees

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 Flashcards

9 quizzes

Quizzes on Binary Search Trees

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Binary Search Trees

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Binary Search Trees

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Binary Search Trees

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Binary Search Trees you should explore

Discover More Revision Notes Related to Binary Search Trees to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Data Structures

Arrays

user avatar
user avatar
user avatar
user avatar
user avatar

209+ studying

184KViews

96%

114 rated

Data Structures

Records, Lists & Tuples

user avatar
user avatar
user avatar
user avatar
user avatar

473+ studying

181KViews

96%

114 rated

Data Structures

Linked Lists

user avatar
user avatar
user avatar
user avatar
user avatar

441+ studying

193KViews

96%

114 rated

Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

244+ studying

193KViews
Load more notes

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!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered