You're offline — cached pages and worlds still work
Drishti Innovations logo
Drishti Innovations

Binary Search Trees

Tree data structures, search algorithms, and BST traversal. Aligned with BST visualizer simulator.

What you'll learn

  • The defining properties of a Binary Search Tree (BST)
  • How to insert, search, and delete nodes efficiently
  • Why in-order traversal produces sorted output
  • The difference between balanced and unbalanced BSTs
  • Time complexity: average O(log n) vs worst-case O(n)
  • The basics of AVL trees and self-balancing

Key concepts

BST Property

A BST is a binary tree where every node satisfies:

Left subtree values < Node value < Right subtree values

This holds recursively for every node in the tree.

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

Core Operations

OperationAverage CaseWorst Case (skewed tree)
SearchO(log n)O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)
In-orderO(n)O(n)

Insert Algorithm

  1. Start at root.
  2. If new value < current node → go left; else → go right.
  3. Repeat until an empty spot is found; insert there.

Search Algorithm

  1. Start at root.
  2. If value == current node → found.
  3. If value < current → search left subtree.
  4. If value > current → search right subtree.
  5. If null is reached → not found.

Delete Algorithm (three cases)

CaseSituationAction
1Node has no childrenSimply remove the node
2Node has one childReplace node with its child
3Node has two childrenReplace with in-order successor (smallest value in right subtree), then delete successor

In-Order Traversal → Sorted Output

Traversal order: Left → Root → Right

For the tree above: 20 → 30 → 40 → 50 → 60 → 70 → 80

This property makes BSTs useful for sorting and range queries.

Balanced vs Unbalanced BST

FeatureBalanced BSTUnbalanced (Skewed) BST
HeightO(log n)O(n)
SearchO(log n)O(n)
ExampleAVL, Red-BlackInserting sorted data

Skewed tree example — inserting 10, 20, 30, 40 in order produces a linked list, not a tree.

AVL Trees (Self-Balancing BST)

An AVL tree maintains the balance factor at every node:

Balance Factor = Height(Left subtree) − Height(Right subtree)

Allowed values: −1, 0, or +1. If violated after insert/delete, the tree is rotated to restore balance.

Rotation TypeWhen Used
Right RotationLeft-Left imbalance
Left RotationRight-Right imbalance
Left-RightLeft-Right imbalance
Right-LeftRight-Left imbalance

AVL trees guarantee O(log n) for all operations even in the worst case.


Quick check

  1. A BST contains values 15, 10, 20, 8, 12, 17, 25. Write the result of an in-order traversal.

  2. What is the worst-case time complexity for searching in a BST, and when does this worst case occur?

  3. You need to delete node 20 from the BST below. Node 20 has left child 17 and right child 25, and 25 has left child 22. What value replaces 20, and why?

  4. Explain why inserting data that is already sorted into a standard BST is inefficient.

  5. What condition does an AVL tree enforce that a standard BST does not, and how does it maintain that condition after an insertion?

Key Takeaways (TL;DR)

  • What you'll learn
  • Key concepts
  • Quick check

Master this topic with Drishti OS

Get unlimited mock tests, AI-powered mentorship, and complete video courses when you join.

Start Free Practice