Binary Search Tree (BST)

Search, insert, delete, validation and interview patterns


1. Introduction

A Binary Search Tree (BST) is a special type of Binary Tree that follows a specific ordering rule.

BST Property:

Left\ Subtree < Root < Right\ Subtree

Example:

        50
       /  \
     30    70
    / \    / \
   20 40 60 80

Notice:

Left of 50 → 30,20,40 (smaller)

Right of 50 → 70,60,80 (larger)

2. Why Binary Search Tree?

Consider an Array:

10 20 30 40 50 60 70

Searching:

O(n)

for unsorted arrays.

BST reduces searching time by eliminating half of the tree at every step.

Similar idea to Binary Search.


3. BST Properties

For every node:

All values in left subtree are smaller.

All values in right subtree are greater.

Example:

        15
       /  \
      10   20
     / \   / \
    8  12 17 25

Valid BST.


Invalid BST:

       15
      /  \
     20   10

Violates BST rule.


4. Binary Tree vs BST

FeatureBinary TreeBST
Ordering RuleNoYes
SearchO(n)O(log n) average
InsertAny positionOrdered
DeleteGeneralOrdered

Binary Tree:

       10
      /  \
     50   5

Valid Binary Tree

Not BST.


5. BST Structure

Example BST:

           50
         /    \
       30      70
      /  \    /  \
     20  40  60  80

Root:

50

Left Subtree:

30
20
40

Right Subtree:

70
60
80

6. BST Node Implementation

class Node {

    int data;

    Node left;
    Node right;

    Node(int data){
        this.data = data;
    }
}

Create Root:

Node root = new Node(50);

7. BST Terminology

Example:

          50
        /   \
      30     70
     / \     / \
   20 40   60 80

Root:

50

Leaf Nodes:

20 40 60 80

Parent of 20:

30

Sibling of 20:

40

Height:

2

8. Searching in BST

Search:

60

BST:

         50
        /  \
      30    70
           /
         60

Process:

60 > 50

Go Right

60 < 70

Go Left

Found

Recursive Search

public boolean search(Node root,
                      int key){

    if(root == null)
        return false;

    if(root.data == key)
        return true;

    if(key < root.data)
        return search(root.left,key);

    return search(root.right,key);
}

Complexity:

Balanced BST:

O(log n)

Worst Case:

O(n)

9. Insertion in BST

Insert:

65

BST:

         50
        /  \
      30    70
           /
         60

Process:

65 > 50

Right

65 < 70

Left

65 > 60

Right

Result:

         50
        /  \
      30    70
           /
         60
           \
            65

Java Implementation

public Node insert(Node root,
                   int value){

    if(root == null)
        return new Node(value);

    if(value < root.data)
        root.left =
             insert(root.left,value);

    else if(value > root.data)
        root.right =
             insert(root.right,value);

    return root;
}

10. Deletion in BST

Most important BST topic.

Three cases.


Case 1: Delete Leaf Node

BST:

      50
     /
   30

Delete:

30

Result:

50

Case 2: Delete Node with One Child

Before:

      50
     /
   30
   /
 20

Delete:

30

Result:

      50
     /
   20

Case 3: Delete Node with Two Children

Before:

         50
        /  \
      30    70
           /  \
         60    80

Delete:

70

Replace with:

Inorder Successor

Successor:

80

Result:

         50
        /  \
      30    80
           /
         60

Deletion Complexity:

O(h)

where

h = height

11. BST Traversals

Same as Binary Tree.


Preorder

Root
Left
Right

Inorder

Left
Root
Right

Postorder

Left
Right
Root

Level Order

BFS

12. Why Inorder Traversal is Important

For BST:

Inorder Traversal always produces:

Sorted Order

Example:

        50
      /    \
    30      70
   / \      / \
 20 40    60 80

Inorder:

20 30 40 50 60 70 80

Java:

public void inorder(Node root){

    if(root == null)
        return;

    inorder(root.left);

    System.out.print(root.data+" ");

    inorder(root.right);
}

13. Finding Minimum Value

Minimum value is always:

Leftmost Node

Example:

         50
        /
      30
     /
   20

Minimum:

20

Code:

while(root.left != null){
    root = root.left;
}

14. Finding Maximum Value

Maximum value is:

Rightmost Node

Example:

         50
           \
            70
              \
               90

Maximum:

90

15. Successor and Predecessor


Inorder Successor

Next larger element.

Example:

20 30 40 50 60

Successor of:

40

is:

50

Inorder Predecessor

Previous smaller element.

Predecessor of:

40

is:

30

Used in deletion.


16. Height of BST

Example:

       50
      /
    30
   /
 20

Height:

2

Recursive:

public int height(Node root){

    if(root == null)
        return -1;

    return 1 +
           Math.max(
               height(root.left),
               height(root.right)
           );
}

17. Balanced vs Unbalanced BST

Balanced:

        50
       /  \
     30    70

Height:

O(log n)

Unbalanced:

10
 \
 20
   \
    30
      \
       40

Acts like Linked List.

Height:

O(n)

18. Time Complexity Analysis

Balanced BST:

OperationComplexity
SearchO(log n)
InsertO(log n)
DeleteO(log n)

Worst Case:

OperationComplexity
SearchO(n)
InsertO(n)
DeleteO(n)

19. BST Validation

Check whether tree satisfies BST property.

Approach:

Maintain:

Minimum Allowed Value

Maximum Allowed Value

Recursive Solution:

boolean isBST(Node root,
              long min,
              long max)

Common Interview Question.


20. Common BST Problems

Easy:

  1. Search Node
  2. Insert Node
  3. Find Min
  4. Find Max

Medium:

  1. Delete Node
  2. Validate BST
  3. Lowest Common Ancestor
  4. Kth Smallest Element

Hard:

  1. Recover BST
  2. Largest BST in Binary Tree
  3. Convert BST to Doubly Linked List

21. BST vs HashMap

FeatureBSTHashMap
SearchO(log n)O(1) average
Ordered DataYesNo
Sorted TraversalYesNo
Range QueriesEasyDifficult

Use BST when:

Data must remain sorted.

22. Applications of BST

Databases

Indexing


File Systems

Hierarchical Searching


Dictionaries

Word Lookup


Range Queries

Finding values between:

50 and 100

Ordered Data Storage

Maintaining sorted records.


23. Interview Patterns

Must Know:

  1. Recursive Search
  2. Recursive Insert
  3. Recursive Delete
  4. Inorder Traversal
  5. Validate BST
  6. Height Calculation
  7. Lowest Common Ancestor
  8. Kth Smallest Element

Most Asked:

  1. Search in BST
  2. Insert into BST
  3. Delete Node
  4. Validate BST
  5. Lowest Common Ancestor
  6. Kth Smallest Element
  7. Inorder Successor

24. Revision Summary

Binary Search Tree:

  • Special Binary Tree
  • Ordered Structure

BST Rule:

Left Subtree < Root < Right Subtree

Important Operations:

  1. Search
  2. Insert
  3. Delete
  4. Traversal
  5. Find Min
  6. Find Max

Important Property:

Inorder Traversal = Sorted Order

Complexity (Balanced BST):

Search  → O(log n)

Insert  → O(log n)

Delete  → O(log n)

Must Know Interview Topics:

  • BST Search
  • BST Insert
  • BST Delete
  • BST Validation
  • Successor & Predecessor
  • Lowest Common Ancestor
  • Kth Smallest Element

Learning Path:

BST Basics → Search → Insert → Traversals → Delete → Validate BST → LCA → Kth Smallest → AVL Tree → Red Black Tree