codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

13 / 65 topics
13Binary Search Trees (BSTs)14Balanced Binary Search Trees15AVL Trees16Red-Black Trees17B-Trees18Graphs Basics19Graph Representations (Adjacency List, Matrix)64AVL Trees Advantages and Disadvantages65Red-Black Trees Advantages and Disadvantages
Tutorials/Data Structures & Algorithms/Binary Search Trees
🧮Data Structures & Algorithms

Binary Search Trees

Updated 2026-05-15
10 min read

Binary Search Trees

Introduction

In the world of computer science, data structures play a crucial role in organizing and managing data efficiently. One such fundamental data structure is the Binary Search Tree (BST). A binary search tree is a node-based binary tree data structure which has the following properties:

  • The left subtree of a node contains only nodes with keys lesser than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than the node’s key.
  • The left and right subtrees must also be binary search trees.

These properties ensure that operations like search, insertion, and deletion can be performed efficiently. In this tutorial, we will delve into the concept of binary search trees, their properties, and how they can be implemented in code.

Concept

A binary search tree is a type of binary tree where each node has at most two children referred to as the left child and the right child. The key property that distinguishes a binary search tree from other types of binary trees is its ability to maintain order among elements, which allows for efficient searching, insertion, and deletion operations.

Properties of Binary Search Trees

  1. Order Property: For any given node, all nodes in the left subtree have values less than the node's value, and all nodes in the right subtree have values greater than the node's value.
  2. No Duplicate Keys: Typically, binary search trees do not allow duplicate keys to maintain the order property.
  3. Balanced Trees: While not a requirement for binary search trees, balanced trees (like AVL trees or Red-Black trees) ensure that operations are performed in logarithmic time.

Operations on Binary Search Trees

Binary search trees support several fundamental operations:

  1. Search: To find an element, start at the root and compare the target value with the current node's value. If the target is less than the current node's value, move to the left child; if greater, move to the right child. Repeat until you find the target or reach a leaf node.
  2. Insertion: To insert a new element, start at the root and follow the same path as in search until you reach a leaf node where the new element can be inserted while maintaining the order property.
  3. Deletion: Deleting an element involves three cases:
    • The node to be deleted has no children (it is a leaf node).
    • The node to be deleted has one child.
    • The node to be deleted has two children.

Examples

Let's look at some practical examples of how binary search trees can be implemented and used in code.

Example 1: Basic Implementation

Here is a basic implementation of a binary search tree in Python:

Python
1class TreeNode:
2 def __init__(self, key):
3 self.left = None
4 self.right = None
5 self.val = key
6
7class BinarySearchTree:
8 def __init__(self):
9 self.root = None
10
11 def insert(self, key):
12 if self.root is None:
13 self.root = TreeNode(key)
14 else:
15 self._insert_recursive(self.root, key)
16
17 def _insert_recursive(self, node, key):
18 if key < node.val:
19 if node.left is None:
20 node.left = TreeNode(key)
21 else:
22 self._insert_recursive(node.left, key)
23 else:
24 if node.right is None:
25 node.right = TreeNode(key)
26 else:
27 self._insert_recursive(node.right, key)
28
29 def search(self, key):
30 return self._search_recursive(self.root, key)
31
32 def _search_recursive(self, node, key):
33 if node is None or node.val == key:
34 return node
35 if key < node.val:
36 return self._search_recursive(node.left, key)
37 return self._search_recursive(node.right, key)

Example 2: Insertion and Search

Let's insert some values into the binary search tree and perform a search operation:

Python
1bst = BinarySearchTree()
2values = [50, 30, 70, 20, 40, 60, 80]
3for value in values:
4 bst.insert(value)
5
6# Search for a value
7result = bst.search(40)
8if result:
9 print("Found:", result.val)
10else:
11 print("Not found")

Example 3: Deletion

Here is an example of how to delete a node from the binary search tree:

Python
1class BinarySearchTree:
2 # ... (previous methods)
3
4 def delete(self, key):
5 self.root = self._delete_recursive(self.root, key)
6
7 def _delete_recursive(self, node, key):
8 if node is None:
9 return node
10 if key < node.val:
11 node.left = self._delete_recursive(node.left, key)
12 elif key > node.val:
13 node.right = self._delete_recursive(node.right, key)
14 else:
15 # Node with only one child or no child
16 if node.left is None:
17 return node.right
18 elif node.right is None:
19 return node.left
20
21 # Node with two children: Get the inorder successor (smallest in the right subtree)
22 temp = self._min_value_node(node.right)
23 node.val = temp.val
24 node.right = self._delete_recursive(node.right, temp.val)
25
26 return node
27
28 def _min_value_node(self, node):
29 current = node
30 while current.left is not None:
31 current = current.left
32 return current

What's Next?

In the next section, we will explore another advanced data structure called Heaps. Heaps are useful for implementing priority queues and can be used in various applications such as scheduling and graph algorithms.

Stay tuned for more tutorials on data structures and algorithms!


PreviousHash Collision HandlingNext Balanced Binary Search Trees

Recommended Gear

Hash Collision HandlingBalanced Binary Search Trees