What is a Binary Search Tree in Data Structures?
By Rohan Vats
Updated on Apr 01, 2025 | 8 min read | 6.76K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Apr 01, 2025 | 8 min read | 6.76K+ 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.
Popular AI Programs
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
Machine Learning Courses to upskill
Explore Machine Learning Courses for Career Progression
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:
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
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.
A Binary Search Tree (BST) is a binary tree where each node's left child holds values less than its parent, and the right child holds values greater, ensuring efficient searching, insertion, and deletion operations.
A BST offers faster search, insertion, and deletion operations compared to arrays and linked lists. It uses a sorted structure, enabling O(log n) time complexity on average for these operations, unlike O(n) for unsorted arrays or linked lists.
The main operations on a BST are search, insertion, deletion, and traversal. Each operation is efficient, with average time complexity of O(log n), though it can degrade to O(n) if the tree is unbalanced.
A balanced BST maintains a height difference of at most 1 between its left and right subtrees, ensuring efficient operations. An unbalanced BST may become skewed, resulting in slower operations, potentially O(n).
On average, searching in a Binary Search Tree takes O(log n) time, where n is the number of nodes. However, in the worst-case scenario of an unbalanced tree, it can degrade to O(n).
Insertion in a BST is done by comparing the value to be inserted with the current node. Depending on the comparison, it is placed in the left or right subtree, ensuring that the tree maintains its sorted structure.
The primary types of BSTs are balanced BSTs (like AVL trees or Red-Black trees), unbalanced BSTs, and self-balancing BSTs, which automatically adjust their structure during insertions and deletions to maintain efficiency.
Common BST traversal methods include in-order (left, root, right), pre-order (root, left, right), and post-order (left, right, root). These methods are used to visit all nodes in a specific order.
Self-balancing BSTs, like AVL trees or Red-Black trees, automatically adjust their structure after each insertion or deletion to ensure that the tree remains balanced, maintaining O(log n) time complexity for operations.
BSTs are used in various applications like database indexing, autocompletion for search engines, and organizing files in file systems. Their efficient search and insert operations make them ideal for these tasks.
To implement a BST in Java, define a TreeNode class with left and right child pointers. Implement methods like insert, delete, and inorderTraversal to manage the tree and perform operations like searching or printing nodes in order.
408 articles published
Rohan Vats is a Senior Engineering Manager with over a decade of experience in building scalable frontend architectures and leading high-performing engineering teams. Holding a B.Tech in Computer Scie...
Speak with AI & ML expert
By submitting, I accept the T&C and
Privacy Policy
Top Resources