Binary Search Tree (BST)

A Binary Search Tree (BST) is a node-based binary tree data structure which has the following properties:


1. Core Operations

I. Search and Insert

Both operations use the BST property to eliminate half of the tree at each step.

II. Deletion

Deletion is the most complex operation in a BST and involves three scenarios:

  1. Node is a Leaf: Simply remove the node.
  2. Node has One Child: Copy the child to the node and delete the child.
  3. Node has Two Children: Find the In-order Successor (the smallest node in the right subtree), copy its value to the current node, and delete the successor.

2. Complexity Analysis

Operation Average Case Worst Case (Skewed)
Search O(log N) O(N)
Insert O(log N) O(N)
Delete O(log N) O(N)

The Skewed Case: If items are inserted in sorted order (e.g., 1, 2, 3, 4, 5), the BST becomes a single line, essentially a Linked List. This is why Self-Balancing Trees (like AVL or Red-Black) are used in production.


3. Implementation (Recursive)

class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insert(root, val) {
  if (!root) return new Node(val);
  
  if (val < root.val) {
    root.left = insert(root.left, val);
  } else {
    root.right = insert(root.right, val);
  }
  return root;
}

4. Key Properties to Remember


Interview Pro-Tips: Why use a BST?

If an interviewer asks why we use BSTs instead of just sorting an array:


Technical Summary

  1. Binary Search Property: The fundamental rule for navigation.
  2. Logarithmic: The target efficiency for a balanced tree.
  3. In-order: The mechanism for retrieving data in sorted order.
  4. Balancing: Necessary to prevent performance degradation in production.