What is a Binary Search Tree in Data Structures?
By Rohan Vats
Updated on Apr 01, 2025 | 8 min read | 6.2k views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Apr 01, 2025 | 8 min read | 6.2k views
Share:
Table of Contents
If you’re diving into the world of data structures and algorithms, you’ve probably come across something called the Binary Search Tree (BST). It's one of those foundational concepts that’s crucial for both understanding more advanced algorithms and optimizing performance in real-world applications.
I’m going to walk you through what a Binary Search Tree is, why it’s useful, and how it’s implemented. So, let’s dive into the world of Binary Search Tree, as I share a professional’s experience working with them.
Learn data science courses online from the World’s top Universities and fast-track your career.
In simple terms, a Binary Search Tree (BST) is a type of binary tree that keeps its elements in a sorted order. That means it allows for efficient searching, insertion, and deletion of data. At a high level, a Binary Search Tree is made up of nodes that hold data and have two child nodes—a left child and a right child.
The key property that makes a Binary Search Tree so useful is the way data is structured:
1. Left Subtree: All the nodes in the left subtree of a node contain values less than the node's value.
2. Right Subtree: All the nodes in the right subtree of a node contain values greater than the node's value.
This property allows you to perform operations like search, insertion, and deletion in a very efficient manner, specifically in O(log n) time on average, where n is the number of nodes.
Must Explore: Recursion in Data Structures: Types, Algorithms, and Applications
Okay, now that we’ve got the basics covered, you’re probably wondering—why should I care about a Binary Search Tree? Why not just use an array or a linked list?
Here’s the thing: in terms of searching for an element, arrays and linked lists are less efficient than Binary Search Tree.
A Binary Search Tree, however, lets you quickly search, insert, and delete nodes because of its structure. Let’s see how this works in more detail.
Also Read: What is Decision Tree in Data Mining? Types, Real World Examples & Applications
There are four fundamental operations that you will commonly perform on a BST:
Searching in a binary search tree is very efficient. Starting from the root, if the target value is less than the current node, we go to the left child; if it's greater, we go to the right child. This process continues until we find the target node or reach a null pointer (which means the value isn’t in the tree).
Insertion into a Binary Search Tree is similar to searching, but instead of stopping when you find the node, you create a new node at the appropriate position. You start at the root and follow the left or right child pointers based on comparisons until you find a null child, and then place the new node there.
Deleting a node is a bit trickier than searching or inserting because there are three cases to consider:
Traversal refers to visiting all the nodes in the tree in a specific order. The three common types of traversal are:
Must Explore: 4 Types of Trees in Data Structures Explained[2025]
Now that we’ve covered the basic operations, let’s talk about some variations of the BST:
A balanced binary search tree is one where the height of the left and right subtrees of every node differ by at most 1. This ensures that the tree is as shallow as possible, which helps maintain the O(log n) time complexity for search, insert, and delete operations.
An unbalanced binary search doesn’t enforce the height constraint, so it can potentially degrade into a linked list in the worst case. For example, if the values are inserted in sorted order, the tree becomes skewed, and all the nodes are placed on one side. In this case, the time complexity for operations can degrade to O(n).
To prevent the degradation mentioned above, some trees implement self-balancing mechanisms, like AVL trees or Red-Black trees, which automatically balance themselves after each insertion and deletion.
Binary Search Tree are widely used in various real-world applications. Here are a few examples:
Here’s a simple implementation of a Binary Search Tree in Java to help you get a hands-on understanding.
class BinarySearchTree {
// Node class to represent each node of the tree
static class TreeNode {
int value;
TreeNode leftChild, rightChild;
// Constructor to create a new node with a given value
TreeNode(int value) {
this.value = value;
leftChild = rightChild = null;
}
}
// Root of the binary search tree
TreeNode rootNode;
// Constructor
BinarySearchTree() {
rootNode = null;
}
// Insert a new node with the given value into the tree
void insert(int value) {
rootNode = insertRecursively(rootNode, value);
}
TreeNode insertRecursively(TreeNode node, int value) {
// If the tree is empty, create a new node
if (node == null) {
node = new TreeNode(value);
return node;
}
// Otherwise, recur down the tree
if (value < node.value)
node.leftChild = insertRecursively(node.leftChild, value);
else if (value > node.value)
node.rightChild = insertRecursively(node.rightChild, value);
return node;
}
// Inorder traversal of the tree to print nodes in ascending order
void inorderTraversal() {
inorderRecursiveTraversal(rootNode);
}
void inorderRecursiveTraversal(TreeNode node) {
if (node != null) {
inorderRecursiveTraversal(node.leftChild);
System.out.print(node.value + " ");
inorderRecursiveTraversal(node.rightChild);
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
// Inserting values into the binary search tree
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// Performing inorder traversal and printing the result
System.out.println("Inorder traversal of the binary search tree:");
tree.inorderTraversal();
}
}
Output:
Inorder traversal of the binary search tree:
20 30 40 50 60 70 80
Must Read: Top 30+ DSA projects with source code in 2025
Explanation of the Code:
Must Explore: Trees in Data Structure: 8 Types of Trees Every Data Scientist Should Know About
The Binary Search Tree (BST) is an incredibly powerful and versatile data structure that allows you to store and search data efficiently. Its structure makes operations like searching, insertion, and deletion fast and effective. Whether you’re working on database indexing, implementing autocomplete functionality, or learning about algorithms, understanding the BST is a vital step in becoming a proficient programmer.
I hope this guide has helped clarify the concept of Binary Search Tree and how they work. If you have any questions or thoughts, feel free to leave them in the comments!
Expand your expertise with the best resources available. Browse the programs below to find your ideal fit in Best Machine Learning and AI Courses Online.
Discover in-demand Machine Learning skills to expand your expertise. Explore the programs below to find the perfect fit for your goals.
Discover popular AI and ML blogs and free courses to deepen your expertise. Explore the programs below to find your perfect fit.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Top Resources