View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

What is a Binary Search Tree in Data Structures?

By Rohan Vats

Updated on Apr 01, 2025 | 8 min read | 6.2k views

Share:

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.

What is a Binary Search Tree (BST)?

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

Why Use a Binary Search Tree?

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.

  • In an array, searching for an element takes O(n) time if it’s unsorted. Even in the case of a sorted array, searching still takes O(log n) using binary search, but inserting or deleting an element takes O(n) time, which is inefficient when you have large amounts of data.
  • linked list is great for dynamic memory usage, but searching for an element also takes O(n) time because you have to traverse through each node sequentially.

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

Placement Assistance

Executive PG Program11 Months
background

Liverpool John Moores University

Master of Science in Machine Learning & AI

Dual Credentials

Master's Degree19 Months

Basic Operations on a Binary Search Tree

There are four fundamental operations that you will commonly perform on a BST:

1. Search

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).

  • Time ComplexityO(log n) on average, but in the worst case (if the tree is unbalanced), it can degrade to O(n).

2. Insertion

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.

  • Time ComplexityO(log n) on average, but like searching, it can degrade to O(n) in the worst case.

3. Deletion

Deleting a node is a bit trickier than searching or inserting because there are three cases to consider:

  • Case 1: The node has no children – Simply remove the node.
  • Case 2: The node has one child – Remove the node and replace it with its child.
  • Case 3: The node has two children – Find the node’s in-order successor (the smallest node in the right subtree), replace the node with the successor, and delete the successor.
  • Time Complexity: Like insertion and search, O(log n) on average.

4. Traversal

Traversal refers to visiting all the nodes in the tree in a specific order. The three common types of traversal are:

  • In-order traversal: Visit the left subtree, then the current node, and then the right subtree. This results in nodes being visited in ascending order.
  • Pre-order traversal: Visit the current node, then the left subtree, then the right subtree.
  • Post-order traversal: Visit the left subtree, then the right subtree, and finally the current node.
  • Time Complexity: All traversal methods have O(n) time complexity because you have to visit each node once.

Must Explore: 4 Types of Trees in Data Structures Explained[2025]

Types of Binary Search Tree

Now that we’ve covered the basic operations, let’s talk about some variations of the BST:

1. Balanced Binary Search Tree

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.

  • Examples include AVL trees and Red-Black trees.

2. Unbalanced Binary Search Tree

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).

3. Self-Balancing BST

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.

Real-World Applications of Binary Search Tree

Binary Search Tree are widely used in various real-world applications. Here are a few examples:

  • Database Indexing: Binary search tree can be used for indexing data in databases. Since searches are efficient, they help databases quickly locate and retrieve records.
  • Autocompletion: Binary search tree can be used to store a set of strings (such as dictionary words) and provide fast lookups for autocompletion suggestions.
  • File Systems: Some file systems use BSTs to organize directories and files for efficient searching and retrieval.

Implementing a Binary Search Tree in Java

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:

  • TreeNode Class: This represents each node in the tree, which contains an integer value and references to its left and right children.
  • Insert Method: The insert method recursively places a new value at the correct position in the tree, maintaining the Binary Search Tree property.
  • Inorder Traversal: The inorderTraversal method prints the values of the tree nodes in ascending order by recursively visiting the left subtree, then the node, and then the right subtree.

Must Explore: Trees in Data Structure: 8 Types of Trees Every Data Scientist Should Know About

Conclusion

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.

Frequently Asked Questions (FAQs)

1. What is a Binary Search Tree (BST)?

2. Why is a Binary Search Tree better than an array or linked list?

3. What are the main operations on a Binary Search Tree?

4. What is the difference between balanced and unbalanced Binary Search Trees?

5. What is the time complexity of searching in a Binary Search Tree?

6. What happens during the insertion operation in a Binary Search Tree?

7. What are the types of Binary Search Trees?

8. What are the common traversal techniques in Binary Search Trees?

9. How do self-balancing Binary Search Trees work?

10. What are real-world applications of Binary Search Trees?

11. How do you implement a Binary Search Tree in Java?

Rohan Vats

408 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive Program in Generative AI for Leaders

76%

seats filled

View Program

Top Resources

Recommended Programs

LJMU

Liverpool John Moores University

Master of Science in Machine Learning & AI

Dual Credentials

Master's Degree

19 Months

IIITB
bestseller

IIIT Bangalore

Executive Diploma in Machine Learning and AI

Placement Assistance

Executive PG Program

11 Months

upGrad
new course

upGrad

Advanced Certificate Program in GenerativeAI

Generative AI curriculum

Certification

4 months