Chapter 8 · Trees

Binary Search Trees

A Binary Search Tree is a binary tree with one extra rule: for every node, all values in its left subtree are smaller, and all values in its right subtree are larger. This one constraint enables search in O(log n).

What is a binary search tree?

A binary search tree (BST) is a binary tree where every node's left subtree holds smaller values and its right subtree holds larger values. That ordering lets search, insert and delete skip half the remaining tree at each step: O(log n) when the tree is balanced. Inserting sorted data into a plain BST makes it a linked list (O(n)), which is why self-balancing trees exist.

function insert(node, v) {
  if (!node) return { value: v, left: null, right: null };
  if (v < node.value) node.left = insert(node.left, v);
  else node.right = insert(node.right, v);
  return node;
}
function contains(node, v) {
  while (node) { if (v === node.value) return true; node = v < node.value ? node.left : node.right; }
  return false;
}

The BST property

831016914

Every node: left subtree < node < right subtree

Left subtree of 8

Contains: 1, 3, 6 — all less than 8. BST property holds.

Right subtree of 8

Contains: 9, 10, 14 — all greater than 8. BST property holds.

Live demo — insert and search

Insert values and watch the BST property enforce itself: smaller values go left, larger go right. Search highlights the path taken to find a node.

Binary Search Tree
preset:
normalpathfounddelete

Tree is empty — insert a value or click a preset above

→ Insert values to build your BST

Search and insert pseudocode

bst-operations.pseudo
1search(node, target): // O(log n) balanced, O(n) worst
2 if node = null: return false // not found
3 if target = node.value: return true
4 if target < node.value:
5 return search(node.left, target) // go left
6 else:
7 return search(node.right, target) // go right
8 
9insert(node, value): // O(log n) average
10 if node = null:
11 return new Node(value) // base case — place here
12 if value < node.value:
13 node.left <- insert(node.left, value)
14 else if value > node.value:
15 node.right <- insert(node.right, value)
16 // else: value already exists — ignore or update
17 return node

Deletion — the 3 cases

1 / 3
1

Case 1: Node is a leaf

The node to delete has no children. Simply remove it by setting its parent's pointer to null. No structural changes needed.

1 of 3
bst-delete.pseudo
1delete(node, target):
2 if node = null: return null
3 if target < node.value:
4 node.left <- delete(node.left, target) // recurse left
5 else if target > node.value:
6 node.right <- delete(node.right, target) // recurse right
7 else: // found it
8 if node.left = null: return node.right // Case 1 or 2
9 if node.right = null: return node.left // Case 1 or 2
10 // Case 3: two children
11 successor <- findMin(node.right) // in-order successor
12 node.value <- successor.value // overwrite value
13 node.right <- delete(node.right, successor.value)
14 return node
15 
16findMin(node): // leftmost node = minimum
17 while node.left != null: node <- node.left
18 return node

Balanced vs degenerate

BALANCED BST

Insert: 8, 4, 12, 2, 6

Well-distributed insertions → balanced tree → height = log n

Search / Insert / DeleteO(log n)

Logarithmic time

DEGENERATE BST

Insert: 1, 2, 3, 4, 5

Sorted insertions → right-skewed tree → height = n (a linked list)

Search / Insert / DeleteO(n)

Linear time

⚠️Always sort input? Always bad for BSTs.
Inserting already-sorted data into a BST produces the worst-case degenerate tree. This is why AVL Trees and Red-Black Trees exist — they auto-rebalance on every insert/delete to guarantee O(log n) regardless of input order.

Time complexity

Search — balanced BSTO(log n)

Logarithmic time

Insert — balanced BSTO(log n)

Logarithmic time

Delete — balanced BSTO(log n)

Logarithmic time

Search / Insert / Delete — degenerate (worst case)O(n)

Linear time

🎯 Quick Check

You delete a node in a BST that has two children. What value replaces it?

🧠BST vs sorted array
A sorted array has O(log n) search (binary search) but O(n) insert. A balanced BST has O(log n) for both search AND insert AND delete. This is the core trade-off that justifies tree structures over sorted arrays.

Binary Search Trees: summary

Key points

  • In-order traversal returns the values sorted.
  • Deleting a node with two children replaces it with its in-order successor.
  • AVL and red-black trees keep the height O(log n) automatically.

Frequently asked questions

›What is the time complexity of a BST?

O(log n) for search, insert and delete when balanced; O(n) in the worst case when it degenerates into a chain.

›How do you validate a BST?

Recurse with a valid (min, max) range for each node, narrowing the range as you go left or right. Checking only direct children isn't enough.