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
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.
Tree is empty — insert a value or click a preset above
→ Insert values to build your BST
Search and insert pseudocode
1search(node, target): // O(log n) balanced, O(n) worst2 if node = null: return false // not found3 if target = node.value: return true4 if target < node.value:5 return search(node.left, target) // go left6 else:7 return search(node.right, target) // go right89insert(node, value): // O(log n) average10 if node = null:11 return new Node(value) // base case — place here12 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 update17 return node
Deletion — the 3 cases
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.
1delete(node, target):2 if node = null: return null3 if target < node.value:4 node.left <- delete(node.left, target) // recurse left5 else if target > node.value:6 node.right <- delete(node.right, target) // recurse right7 else: // found it8 if node.left = null: return node.right // Case 1 or 29 if node.right = null: return node.left // Case 1 or 210 // Case 3: two children11 successor <- findMin(node.right) // in-order successor12 node.value <- successor.value // overwrite value13 node.right <- delete(node.right, successor.value)14 return node1516findMin(node): // leftmost node = minimum17 while node.left != null: node <- node.left18 return node
Balanced vs degenerate
BALANCED BST
Insert: 8, 4, 12, 2, 6
Well-distributed insertions → balanced tree → height = log n
Logarithmic time
DEGENERATE BST
Insert: 1, 2, 3, 4, 5
Sorted insertions → right-skewed tree → height = n (a linked list)
Linear time
Time complexity
Logarithmic time
Logarithmic time
Logarithmic time
Linear time
🎯 Quick Check
You delete a node in a BST that has two children. What value replaces it?
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.