Binary Search Trees (BST)

November 19, 2025
#binary-search-tree #bst #tree #data-structures #searching #algorithms #interviewing

Introduction

A Binary Search Tree (BST) is a specialized binary tree with an important ordering property: for every node, all values in the left subtree are less than the node’s value, and all values in the right subtree are greater. This simple property enables efficient searching, insertion, and deletion operations.

BSTs are one of the most important data structures in computer science, forming the basis for efficient ordered collections, database indexing, and many algorithmic solutions. By the end of this tutorial, you’ll understand how to implement, traverse, and use BSTs effectively.

Review Binary Trees, Binary Search, and Big-O Notation first if needed.

What is a Binary Search Tree?

A BST is a binary tree with the BST property:

  • For every node N:
    • All values in the left subtree < N.value
    • All values in the right subtree > N.value
  • This property holds recursively for every node in the tree

Example of a valid BST:

       8
      / \
     3   10
    / \    \
   1   6   14
      / \  /
     4  7 13

NOT a BST (violates ordering):

       8
      / \
     3   10
    / \    \
   1  12   14  ← 12 > 8, shouldn't be in left subtree

Why use a Binary Search Tree?

Choose a BST when:

  1. You need ordered data with efficient operations
  2. You need O(log n) search in average case
  3. You need to find min/max, predecessor/successor efficiently
  4. You need range queries (find all values between x and y)
  5. You want dynamic data (unlike binary search on sorted array)
  6. You need efficient insert/delete while maintaining order

Avoid a BST when:

  • You don’t need ordering (use hash table)
  • You need guaranteed O(log n) (use self-balancing trees: AVL, Red-Black)
  • You need O(1) access by index (use array)
  • Data is mostly sequential access (use linked list)

Real-world applications

BSTs are used extensively:

  • Databases: Index structures (though usually B-trees)
  • File systems: Organizing directory structures
  • Compilers: Symbol tables for variable lookup
  • Graphics: Spatial indexing for 2D/3D scenes
  • Autocomplete: Trie-based systems (specialized BST variant)
  • Priority queues: When both ordering and dynamic updates needed
  • Game development: Storing game objects by priority
  • Network routing: Routing table lookups

BST Implementation

Common BST operations

Finding minimum and maximum

Deleting a node

Deletion is the most complex operation in a BST. Three cases:

  1. Leaf node (no children): Simply remove
  2. One child: Replace node with its child
  3. Two children: Replace with in-order successor (or predecessor), then delete successor

Validating a BST

Check if a binary tree satisfies the BST property:

In-order traversal gives sorted order

Complexity analysis

Operation Average Case Worst Case Best Case
Search O(log n) O(n) O(1)
Insert O(log n) O(n) O(1)
Delete O(log n) O(n) O(1)
Find Min/Max O(log n) O(n) O(1)
In-order Traversal O(n) O(n) O(n)
Space O(n) O(n) O(n)

Key insight: Worst case O(n) occurs when the tree becomes skewed (essentially a linked list). This happens with sorted or reverse-sorted insertions.

Solution: Use self-balancing BSTs (AVL trees, Red-Black trees) to guarantee O(log n) operations.

BST vs other data structures

Feature BST Hash Table Sorted Array
Search O(log n) avg O(1) avg O(log n)
Insert O(log n) avg O(1) avg O(n)
Delete O(log n) avg O(1) avg O(n)
Ordered ✅ Yes ❌ No ✅ Yes
Min/Max O(log n) O(n) O(1)
Range queries ✅ Efficient ❌ Inefficient ✅ Efficient
Successor/Predecessor O(log n) ❌ N/A O(1)

Common pitfalls

  • Not handling duplicates: Decide whether to allow duplicates or reject them
  • Unbalanced trees: Without balancing, BST degrades to O(n)
  • Incorrect deletion: The two-children case is tricky—test thoroughly
  • Validation errors: When validating, must check entire subtree ranges, not just children
  • Memory leaks: In manual-memory languages, ensure deleted nodes are freed
  • Integer overflow: When using min/max bounds in validation

When to prefer other data structures

  • Need guaranteed O(log n) → Use AVL tree or Red-Black tree
  • Don’t need ordering → Use hash table
  • Need O(1) access by index → Use array
  • Data is mostly insertions at ends → Use deque
  • Need priority-based access → Use heap (min-heap or max-heap)

Practice problems

  1. Find the kth smallest element in a BST
  2. Convert a sorted array to a balanced BST
  3. Find the lowest common ancestor (LCA) of two nodes in a BST
  4. Check if two BSTs are identical
  5. Find the in-order successor of a given node
  6. Convert a BST to a sorted doubly linked list
  7. Find the distance between two nodes in a BST
  8. Serialize and deserialize a BST
  9. Find all nodes in a given range [k1, k2]
  10. Recover a BST where two nodes are swapped by mistake
  11. Implement an iterator for BST (in-order traversal)
  12. Find the closest value to a target in a BST
  13. Count the number of BSTs that can be formed with n nodes
  14. Merge two BSTs into one balanced BST
  15. Find the mode (most frequent element) in a BST

Complexity summary

  • Time complexity:
    • Search, Insert, Delete: O(log n) average, O(n) worst (skewed)
    • Find Min/Max: O(log n) average, O(n) worst
    • Traversal: O(n) always
  • Space complexity:
    • Storage: O(n) for n nodes
    • Recursion stack: O(h) where h is height (O(log n) balanced, O(n) skewed)

Binary Search Trees combine the benefits of binary search with dynamic insertion and deletion. While basic BSTs can degrade to O(n) operations, they form the foundation for self-balancing variants (AVL, Red-Black) that guarantee O(log n) performance.

Thanks for visiting
We are actively updating content to this site. Thanks for visiting! Please bookmark this page and visit again soon.
Sponsor