
The key takeaways
- BST invariant: every key in the left subtree is less than the node and every key in the right subtree is greater, applied recursively at every node
- All operations are O(h): O(log n) on a balanced tree, O(n) on a degenerate one built from sorted input
- Deletion is the hard case: two-child deletion replaces the node with its in-order successor, which always has at most one child
- Inorder traversal gives sorted output for free: visiting left, node, right produces keys in ascending order without any re-sort
- Validation requires range bounds, not parent-child checks: pass lower and upper limits down each recursive call to catch subtree violations
- Hibbard deletion causes silent O(sqrt n) drift: mixed insert/delete workloads quietly degrade unbalanced BSTs; use AVL or red-black trees for mutation-heavy workloads
You have a sorted list. You can binary search it in O(log n). But you can't insert or delete in O(log n) because shifting elements costs O(n). You want both: fast lookup and fast mutation.
The binary search tree is the answer to that exact tension. One structural rule, applied recursively, buys you O(log n) search, insert, delete, min, max, floor, ceiling, rank, and select. All from a single constraint baked into every node.
Reach for a BST when your data is ordered and dynamic: you're inserting and deleting while still needing sorted access, range queries, or "nearest value" lookups.
Your interviewer wants to hear this. Your colleagues probably do not.
The One Rule
Pick any node in the tree. Call its key k. Every key in the left subtree is strictly less than k. Every key in the right subtree is strictly greater. That's it. Apply it recursively and you have a BST.
The invariant holds at the root, at every child, and all the way down. Not just parent-to-child. Everywhere.
Node 8: everything left (3, 1, 6, 4, 7) is less. Everything right (10, 14, 13) is greater. Node 3: everything left (1) is less. Everything right (6, 4, 7) is greater. The invariant holds at every node, not just the root.
This is where people trip up on the validation problem. The rule isn't "left child < node < right child." It's about the entire subtree. A node with value 6 sitting in the right subtree of node 8 violates the BST property even though 6 might be greater than its own parent. More on that shortly.
Memory Layout
Each node is a small heap-allocated struct: a key (and optionally a value), a pointer to the left child, a pointer to the right child.
+----------+----------+----------+
| key | *left | *right |
| (8 bytes)| (8 bytes)| (8 bytes)|
+----------+----------+----------+That's 24 bytes per node before your payload. You might add a *parent pointer for O(1) successor traversal. Balanced variants add a height (AVL) or color bit (red-black) or size field (order statistics).
24 bytes minimum. Times however many nodes you have. Each one a separate heap allocation.
The catch: every node is a separate heap allocation. Nodes from different insertions end up scattered across memory. A BST traversal is a pointer-chasing exercise, and pointer chasing destroys cache performance. Each step down the tree is a potential cache miss. A sorted array binary search, by contrast, pulls multiple elements into cache lines on every access. This is why Rust's BTreeMap uses a B-tree (packing many keys per node) rather than a classical BST.
Binary Search Tree Operations
| Operation | Best Case | Average Case | Worst Case | Space |
|---|---|---|---|---|
| Search | O(log n) | O(log n) | O(n) | O(h) |
| Insert | O(log n) | O(log n) | O(n) | O(h) |
| Delete | O(log n) | O(log n) | O(n) | O(h) |
| Min / Max | O(log n) | O(log n) | O(n) | O(1) |
| Floor / Ceiling | O(log n) | O(log n) | O(n) | O(h) |
| Successor / Predecessor | O(log n) | O(log n) | O(n) | O(1) |
| Inorder Traversal | O(n) | O(n) | O(n) | O(h) |
| Rank / Select (with sizes) | O(log n) | O(log n) | O(n) | O(h) |
h is tree height, O(log n) for balanced trees and O(n) for degenerate ones. Space column is call-stack depth for recursive implementations.
Search
Compare the target against the current node's key. Less than: go left. Greater than: go right. Equal: found it. Hit null: not present.
At every node, the invariant eliminates one entire subtree from consideration. You never search both sides. The number of comparisons equals the depth of the target node. Worst case is the height of the tree.
def search(node, key):
if node is None or node.key == key:
return node
if key < node.key:
return search(node.left, key)
return search(node.right, key)Insert
Follow the same path as search, but instead of returning None when you fall off the tree, you attach a new node there. The comparison logic is identical. The invariant is preserved because you're always placing the new key exactly where search would look for it later.
Delete
This is the operation that earns its complexity. Three cases:
Case 1: The node is a leaf. Remove it. Done.
Case 2: The node has one child. Replace the node with its child. Done. The subtree rooted at that child already satisfies the BST property relative to the deleted node's ancestors, because the deleted node satisfied it.
Case 3: The node has two children. This is where it gets interesting.
Before deleting 8: After (replace with successor 9):
8 9
/ \ / \
3 10 -> 3 10
/ \ / \
9 14 (9 gone) 14You can't just remove a two-child node. You need something to take its place without violating the invariant for either subtree. The in-order successor (the smallest key in the right subtree) works perfectly. It's greater than everything in the left subtree (since the deleted node was), and it's the smallest thing in the right subtree, so the rest of the right subtree still sits to its right.
Finding the in-order successor means walking left from the right child until you hit a node with no left child. That node always has at most one child (no left child, by definition of "minimum"). So deleting it reduces to Case 1 or Case 2. Copy its key up to the node you're "deleting," then remove the actual successor. Total cost: two traversals, both O(h).
Case 3 always reduces to Case 1 or 2 at the successor node. No magic, just two traversals.
Min and Max
Min: walk left until you can't. Max: walk right until you can't. The left-spine is the sorted prefix of the tree; the rightmost node is the largest element.
Floor and Ceiling
Floor of key k: the largest key less than or equal to k.
If k equals the root, the root is the floor. If k is less than the root, the floor must be in the left subtree. If k is greater than the root, the floor might be in the right subtree (if there's something in there that's still ≤ k), and the root is a candidate fallback if the right subtree has nothing smaller than k.
Ceiling is symmetric.
In-order Traversal
Left subtree, then node, then right subtree. Because of the invariant, this visits nodes in ascending key order. An in-order traversal of a BST is a sort. This gives you the BST's most underrated property: you get a sorted sequence "for free" out of a structure that also gives you O(log n) insertion and deletion.
Balanced vs Degenerate: Where O(log n) Lives and Dies
Insert these keys in order: 1, 2, 3, 4, 5.
That's a linked list. Height is n. Every operation is O(n). All the logarithmic guarantees evaporate.
Now insert them in a different order: 3, 1, 4, 2, 5.
Height is 3. Much better.
Same five keys. Completely different performance. The shape depends entirely on insertion order.
The shape of the tree depends entirely on insertion order. A BST built from random permutations of n keys has expected height approximately 4.31 ln n. Devroye proved this in 1986. For n = 1,000,000 that's around 30, which is fine. But "random" in theory doesn't mean random in practice. If your keys arrive sorted (timestamps, auto-increment IDs, sequential reads), you get the degenerate case. Every time.
This is why production-grade ordered maps use self-balancing variants. Java's TreeMap and C++'s std::map are both red-black trees. Red-black trees maintain the invariant that no path from root to leaf is more than twice as long as the shortest, giving a guaranteed height of O(log n). AVL trees are more strictly balanced (height difference between siblings at most 1) but pay with more rotations on insert/delete.
Two Non-Obvious Traps
The Validation Mistake Everyone Makes
"Check if a binary tree is a valid BST." This problem is on almost every interview list. And almost everyone gets the first approach wrong. Go check your last BST validation code. I'll wait.
The wrong approach: at each node, check that left.key < node.key < right.key. This fails on trees like:
5
/ \
3 7
/ \
2 6 <- 6 > 5, violating the BST property at the rootNode 5's right subtree must contain only keys greater than 5. But node 6 is in the left subtree and is greater than 5. A parent-only check misses this.
The correct approach passes a valid range down with each recursive call. Start with (-∞, +∞) at the root. Going left tightens the upper bound to the parent's key. Going right tightens the lower bound.
def is_valid_bst(node, lower=float('-inf'), upper=float('inf')):
if node is None:
return True
if not (lower < node.key < upper):
return False
return (is_valid_bst(node.left, lower, node.key) and
is_valid_bst(node.right, node.key, upper))
The parent-child check passes 6 as valid under 3. The range-bounds check sees that 6 is not in (-inf, 5) and catches the violation.
The Hibbard Deletion Problem
The standard delete algorithm was described by Hibbard in 1962. It always replaces a two-child node with its in-order successor (minimum of the right subtree). This seems arbitrary, and it is.
Here's the problem Knuth noticed: if you repeatedly insert and delete keys at random using Hibbard deletion, the tree doesn't stay random. It drifts. The height grows toward O(sqrt n) instead of O(log n). The asymmetry of always pulling from the right subtree biases the structure over many operations.
This was an open research problem for decades. The practical upshot: if your workload is heavy on interleaved insertions and deletions, an unbalanced BST will quietly degrade. A self-balancing tree (AVL, red-black, treap) does not have this problem.
Implementations
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self, key):
self.root = self._insert(self.root, key)
def _insert(self, node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = self._insert(node.left, key)
elif key > node.key:
node.right = self._insert(node.right, key)
return node
def search(self, key):
return self._search(self.root, key) is not None
def _search(self, node, key):
if node is None or node.key == key:
return node
if key < node.key:
return self._search(node.left, key)
return self._search(node.right, key)
def delete(self, key):
self.root = self._delete(self.root, key)
def _delete(self, node, key):
if node is None:
return None
if key < node.key:
node.left = self._delete(node.left, key)
elif key > node.key:
node.right = self._delete(node.right, key)
else:
if node.left is None:
return node.right
if node.right is None:
return node.left
successor = self._min(node.right)
node.key = successor.key
node.right = self._delete(node.right, successor.key)
return node
def _min(self, node):
while node.left:
node = node.left
return node
def inorder(self):
result = []
self._inorder(self.root, result)
return result
def _inorder(self, node, result):
if node:
self._inorder(node.left, result)
result.append(node.key)
self._inorder(node.right, result)class Node(object):
def __init__(self, key):
self.key = key
self.left = None
self.right = None
class BST(object):
def __init__(self):
self.root = None
def insert(self, key):
self.root = self._insert(self.root, key)
def _insert(self, node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = self._insert(node.left, key)
elif key > node.key:
node.right = self._insert(node.right, key)
return node
def search(self, key):
return self._search(self.root, key) is not None
def _search(self, node, key):
if node is None or node.key == key:
return node
if key < node.key:
return self._search(node.left, key)
return self._search(node.right, key)
def delete(self, key):
self.root = self._delete(self.root, key)
def _delete(self, node, key):
if node is None:
return None
if key < node.key:
node.left = self._delete(node.left, key)
elif key > node.key:
node.right = self._delete(node.right, key)
else:
if node.left is None:
return node.right
if node.right is None:
return node.left
successor = self._min(node.right)
node.key = successor.key
node.right = self._delete(node.right, successor.key)
return node
def _min(self, node):
while node.left:
node = node.left
return node
def inorder(self):
result = []
self._inorder(self.root, result)
return result
def _inorder(self, node, result):
if node:
self._inorder(node.left, result)
result.append(node.key)
self._inorder(node.right, result)class Node {
constructor(key) {
this.key = key;
this.left = null;
this.right = null;
}
}
class BST {
constructor() {
this.root = null;
}
insert(key) {
this.root = this._insert(this.root, key);
}
_insert(node, key) {
if (node === null) return new Node(key);
if (key < node.key) node.left = this._insert(node.left, key);
else if (key > node.key) node.right = this._insert(node.right, key);
return node;
}
search(key) {
return this._search(this.root, key) !== null;
}
_search(node, key) {
if (node === null || node.key === key) return node;
if (key < node.key) return this._search(node.left, key);
return this._search(node.right, key);
}
delete(key) {
this.root = this._delete(this.root, key);
}
_delete(node, key) {
if (node === null) return null;
if (key < node.key) {
node.left = this._delete(node.left, key);
} else if (key > node.key) {
node.right = this._delete(node.right, key);
} else {
if (node.left === null) return node.right;
if (node.right === null) return node.left;
const successor = this._min(node.right);
node.key = successor.key;
node.right = this._delete(node.right, successor.key);
}
return node;
}
_min(node) {
while (node.left !== null) node = node.left;
return node;
}
inorder() {
const result = [];
this._inorder(this.root, result);
return result;
}
_inorder(node, result) {
if (node !== null) {
this._inorder(node.left, result);
result.push(node.key);
this._inorder(node.right, result);
}
}
}class BSTNode<K> {
key: K;
left: BSTNode<K> | null = null;
right: BSTNode<K> | null = null;
constructor(key: K) {
this.key = key;
}
}
class BST<K> {
private root: BSTNode<K> | null = null;
constructor(private compare: (a: K, b: K) => number) {}
insert(key: K): void {
this.root = this.insertRec(this.root, key);
}
private insertRec(node: BSTNode<K> | null, key: K): BSTNode<K> {
if (node === null) return new BSTNode(key);
const cmp = this.compare(key, node.key);
if (cmp < 0) node.left = this.insertRec(node.left, key);
else if (cmp > 0) node.right = this.insertRec(node.right, key);
return node;
}
search(key: K): boolean {
return this.searchRec(this.root, key) !== null;
}
private searchRec(node: BSTNode<K> | null, key: K): BSTNode<K> | null {
if (node === null) return null;
const cmp = this.compare(key, node.key);
if (cmp === 0) return node;
if (cmp < 0) return this.searchRec(node.left, key);
return this.searchRec(node.right, key);
}
delete(key: K): void {
this.root = this.deleteRec(this.root, key);
}
private deleteRec(node: BSTNode<K> | null, key: K): BSTNode<K> | null {
if (node === null) return null;
const cmp = this.compare(key, node.key);
if (cmp < 0) {
node.left = this.deleteRec(node.left, key);
} else if (cmp > 0) {
node.right = this.deleteRec(node.right, key);
} else {
if (node.left === null) return node.right;
if (node.right === null) return node.left;
const successor = this.minNode(node.right);
node.key = successor.key;
node.right = this.deleteRec(node.right, successor.key);
}
return node;
}
private minNode(node: BSTNode<K>): BSTNode<K> {
let current = node;
while (current.left !== null) current = current.left;
return current;
}
inorder(): K[] {
const result: K[] = [];
this.inorderRec(this.root, result);
return result;
}
private inorderRec(node: BSTNode<K> | null, result: K[]): void {
if (node !== null) {
this.inorderRec(node.left, result);
result.push(node.key);
this.inorderRec(node.right, result);
}
}
}
// Usage
const bst = new BST<number>((a, b) => a - b);
bst.insert(5);
bst.insert(3);
bst.insert(7);
console.log(bst.inorder()); // [3, 5, 7]import java.util.ArrayList;
import java.util.List;
public class BST<K extends Comparable<K>> {
private static class Node<K> {
K key;
Node<K> left;
Node<K> right;
Node(K key) {
this.key = key;
}
}
private Node<K> root;
public void insert(K key) {
root = insert(root, key);
}
private Node<K> insert(Node<K> node, K key) {
if (node == null) return new Node<>(key);
int cmp = key.compareTo(node.key);
if (cmp < 0) node.left = insert(node.left, key);
else if (cmp > 0) node.right = insert(node.right, key);
return node;
}
public boolean search(K key) {
return search(root, key) != null;
}
private Node<K> search(Node<K> node, K key) {
if (node == null) return null;
int cmp = key.compareTo(node.key);
if (cmp < 0) return search(node.left, key);
if (cmp > 0) return search(node.right, key);
return node;
}
public void delete(K key) {
root = delete(root, key);
}
private Node<K> delete(Node<K> node, K key) {
if (node == null) return null;
int cmp = key.compareTo(node.key);
if (cmp < 0) {
node.left = delete(node.left, key);
} else if (cmp > 0) {
node.right = delete(node.right, key);
} else {
if (node.left == null) return node.right;
if (node.right == null) return node.left;
Node<K> successor = min(node.right);
node.key = successor.key;
node.right = delete(node.right, successor.key);
}
return node;
}
private Node<K> min(Node<K> node) {
while (node.left != null) node = node.left;
return node;
}
public List<K> inorder() {
List<K> result = new ArrayList<>();
inorder(root, result);
return result;
}
private void inorder(Node<K> node, List<K> result) {
if (node == null) return;
inorder(node.left, result);
result.add(node.key);
inorder(node.right, result);
}
}#include <functional>
#include <vector>
template <typename K, typename Compare = std::less<K>>
class BST {
struct Node {
K key;
Node* left = nullptr;
Node* right = nullptr;
explicit Node(K k) : key(std::move(k)) {}
};
Node* root_ = nullptr;
Compare cmp_;
Node* insert(Node* node, const K& key) {
if (!node) return new Node(key);
if (cmp_(key, node->key)) node->left = insert(node->left, key);
else if (cmp_(node->key, key)) node->right = insert(node->right, key);
return node;
}
Node* search(Node* node, const K& key) const {
if (!node) return nullptr;
if (cmp_(key, node->key)) return search(node->left, key);
if (cmp_(node->key, key)) return search(node->right, key);
return node;
}
Node* min_node(Node* node) const {
while (node->left) node = node->left;
return node;
}
Node* erase(Node* node, const K& key) {
if (!node) return nullptr;
if (cmp_(key, node->key)) {
node->left = erase(node->left, key);
} else if (cmp_(node->key, key)) {
node->right = erase(node->right, key);
} else {
if (!node->left) { Node* r = node->right; delete node; return r; }
if (!node->right) { Node* l = node->left; delete node; return l; }
Node* succ = min_node(node->right);
node->key = succ->key;
node->right = erase(node->right, succ->key);
}
return node;
}
void inorder(Node* node, std::vector<K>& result) const {
if (!node) return;
inorder(node->left, result);
result.push_back(node->key);
inorder(node->right, result);
}
void clear(Node* node) {
if (!node) return;
clear(node->left);
clear(node->right);
delete node;
}
public:
~BST() { clear(root_); }
void insert(const K& key) { root_ = insert(root_, key); }
bool search(const K& key) const { return search(root_, key) != nullptr; }
void erase(const K& key) { root_ = erase(root_, key); }
std::vector<K> inorder() const {
std::vector<K> result;
inorder(root_, result);
return result;
}
};#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int key;
struct Node* left;
struct Node* right;
} Node;
static Node* node_create(int key) {
Node* node = (Node*)malloc(sizeof(Node));
node->key = key;
node->left = node->right = NULL;
return node;
}
Node* bst_insert(Node* root, int key) {
if (!root) return node_create(key);
if (key < root->key) root->left = bst_insert(root->left, key);
else if (key > root->key) root->right = bst_insert(root->right, key);
return root;
}
Node* bst_search(Node* root, int key) {
if (!root || root->key == key) return root;
if (key < root->key) return bst_search(root->left, key);
return bst_search(root->right, key);
}
static Node* bst_min(Node* node) {
while (node->left) node = node->left;
return node;
}
Node* bst_delete(Node* root, int key) {
if (!root) return NULL;
if (key < root->key) {
root->left = bst_delete(root->left, key);
} else if (key > root->key) {
root->right = bst_delete(root->right, key);
} else {
if (!root->left) { Node* r = root->right; free(root); return r; }
if (!root->right) { Node* l = root->left; free(root); return l; }
Node* succ = bst_min(root->right);
root->key = succ->key;
root->right = bst_delete(root->right, succ->key);
}
return root;
}
void bst_inorder(Node* root) {
if (!root) return;
bst_inorder(root->left);
printf("%d ", root->key);
bst_inorder(root->right);
}package bst
type Node struct {
Key int
Left *Node
Right *Node
}
type BST struct {
Root *Node
}
func (b *BST) Insert(key int) {
b.Root = insertRec(b.Root, key)
}
func insertRec(node *Node, key int) *Node {
if node == nil {
return &Node{Key: key}
}
if key < node.Key {
node.Left = insertRec(node.Left, key)
} else if key > node.Key {
node.Right = insertRec(node.Right, key)
}
return node
}
func (b *BST) Search(key int) *Node {
return searchRec(b.Root, key)
}
func searchRec(node *Node, key int) *Node {
if node == nil || node.Key == key {
return node
}
if key < node.Key {
return searchRec(node.Left, key)
}
return searchRec(node.Right, key)
}
func (b *BST) Delete(key int) {
b.Root = deleteRec(b.Root, key)
}
func deleteRec(node *Node, key int) *Node {
if node == nil {
return nil
}
if key < node.Key {
node.Left = deleteRec(node.Left, key)
} else if key > node.Key {
node.Right = deleteRec(node.Right, key)
} else {
if node.Left == nil {
return node.Right
}
if node.Right == nil {
return node.Left
}
succ := minNode(node.Right)
node.Key = succ.Key
node.Right = deleteRec(node.Right, succ.Key)
}
return node
}
func minNode(node *Node) *Node {
for node.Left != nil {
node = node.Left
}
return node
}
func (b *BST) Inorder() []int {
var result []int
inorderRec(b.Root, &result)
return result
}
func inorderRec(node *Node, result *[]int) {
if node == nil {
return
}
inorderRec(node.Left, result)
*result = append(*result, node.Key)
inorderRec(node.Right, result)
}use std::cmp::Ordering;
type Tree<K> = Option<Box<Node<K>>>;
struct Node<K> {
key: K,
left: Tree<K>,
right: Tree<K>,
}
pub struct BST<K: Ord> {
root: Tree<K>,
}
impl<K: Ord> BST<K> {
pub fn new() -> Self {
BST { root: None }
}
pub fn insert(&mut self, key: K) {
insert_rec(&mut self.root, key);
}
pub fn contains(&self, key: &K) -> bool {
contains_rec(&self.root, key)
}
pub fn delete(&mut self, key: &K) {
delete_rec(&mut self.root, key);
}
pub fn inorder(&self) -> Vec<&K> {
let mut result = Vec::new();
inorder_rec(&self.root, &mut result);
result
}
}
fn insert_rec<K: Ord>(tree: &mut Tree<K>, key: K) {
match tree {
None => *tree = Some(Box::new(Node { key, left: None, right: None })),
Some(node) => match key.cmp(&node.key) {
Ordering::Less => insert_rec(&mut node.left, key),
Ordering::Greater => insert_rec(&mut node.right, key),
Ordering::Equal => {}
},
}
}
fn contains_rec<K: Ord>(tree: &Tree<K>, key: &K) -> bool {
match tree {
None => false,
Some(node) => match key.cmp(&node.key) {
Ordering::Less => contains_rec(&node.left, key),
Ordering::Greater => contains_rec(&node.right, key),
Ordering::Equal => true,
},
}
}
fn pop_min<K: Ord>(tree: &mut Tree<K>) -> K {
assert!(tree.is_some());
if tree.as_ref().unwrap().left.is_none() {
let Node { key, right, .. } = *tree.take().unwrap();
*tree = right;
key
} else {
pop_min(&mut tree.as_mut().unwrap().left)
}
}
fn delete_rec<K: Ord>(tree: &mut Tree<K>, key: &K) {
if let Some(node) = tree {
match key.cmp(&node.key) {
Ordering::Less => delete_rec(&mut node.left, key),
Ordering::Greater => delete_rec(&mut node.right, key),
Ordering::Equal => {
if node.left.is_none() {
*tree = node.right.take();
} else if node.right.is_none() {
*tree = node.left.take();
} else {
node.key = pop_min(&mut node.right);
}
}
}
}
}
fn inorder_rec<'a, K: Ord>(tree: &'a Tree<K>, result: &mut Vec<&'a K>) {
if let Some(node) = tree {
inorder_rec(&node.left, result);
result.push(&node.key);
inorder_rec(&node.right, result);
}
}class BSTNode<K: Comparable> {
var key: K
var left: BSTNode<K>?
var right: BSTNode<K>?
init(_ key: K) {
self.key = key
}
}
class BST<K: Comparable> {
var root: BSTNode<K>?
func insert(_ key: K) {
root = insertRec(root, key)
}
private func insertRec(_ node: BSTNode<K>?, _ key: K) -> BSTNode<K> {
guard let node = node else { return BSTNode(key) }
if key < node.key {
node.left = insertRec(node.left, key)
} else if key > node.key {
node.right = insertRec(node.right, key)
}
return node
}
func search(_ key: K) -> Bool {
return searchRec(root, key)
}
private func searchRec(_ node: BSTNode<K>?, _ key: K) -> Bool {
guard let node = node else { return false }
if key == node.key { return true }
if key < node.key { return searchRec(node.left, key) }
return searchRec(node.right, key)
}
func delete(_ key: K) {
root = deleteRec(root, key)
}
private func deleteRec(_ node: BSTNode<K>?, _ key: K) -> BSTNode<K>? {
guard let node = node else { return nil }
if key < node.key {
node.left = deleteRec(node.left, key)
} else if key > node.key {
node.right = deleteRec(node.right, key)
} else {
if node.left == nil { return node.right }
if node.right == nil { return node.left }
let successor = minNode(node.right!)
node.key = successor.key
node.right = deleteRec(node.right, successor.key)
}
return node
}
private func minNode(_ node: BSTNode<K>) -> BSTNode<K> {
var current = node
while let left = current.left { current = left }
return current
}
func inorder() -> [K] {
var result: [K] = []
inorderRec(root, &result)
return result
}
private func inorderRec(_ node: BSTNode<K>?, _ result: inout [K]) {
guard let node = node else { return }
inorderRec(node.left, &result)
result.append(node.key)
inorderRec(node.right, &result)
}
}class BST<K : Comparable<K>> {
private data class Node<K>(
var key: K,
var left: Node<K>? = null,
var right: Node<K>? = null
)
private var root: Node<K>? = null
fun insert(key: K) {
root = insertRec(root, key)
}
private fun insertRec(node: Node<K>?, key: K): Node<K> {
if (node == null) return Node(key)
when {
key < node.key -> node.left = insertRec(node.left, key)
key > node.key -> node.right = insertRec(node.right, key)
}
return node
}
fun search(key: K): Boolean = searchRec(root, key)
private fun searchRec(node: Node<K>?, key: K): Boolean {
node ?: return false
return when {
key == node.key -> true
key < node.key -> searchRec(node.left, key)
else -> searchRec(node.right, key)
}
}
fun delete(key: K) {
root = deleteRec(root, key)
}
private fun deleteRec(node: Node<K>?, key: K): Node<K>? {
node ?: return null
when {
key < node.key -> node.left = deleteRec(node.left, key)
key > node.key -> node.right = deleteRec(node.right, key)
else -> {
if (node.left == null) return node.right
if (node.right == null) return node.left
val successor = minNode(node.right!!)
node.key = successor.key
node.right = deleteRec(node.right, successor.key)
}
}
return node
}
private fun minNode(node: Node<K>): Node<K> {
var current = node
while (current.left != null) current = current.left!!
return current
}
fun inorder(): List<K> {
val result = mutableListOf<K>()
inorderRec(root, result)
return result
}
private fun inorderRec(node: Node<K>?, result: MutableList<K>) {
if (node == null) return
inorderRec(node.left, result)
result.add(node.key)
inorderRec(node.right, result)
}
}using System.Collections.Generic;
public class BST<K> where K : System.IComparable<K>
{
private class Node
{
public K Key;
public Node? Left, Right;
public Node(K key) { Key = key; }
}
private Node? root;
public void Insert(K key) => root = Insert(root, key);
private Node Insert(Node? node, K key)
{
if (node == null) return new Node(key);
int cmp = key.CompareTo(node.Key);
if (cmp < 0) node.Left = Insert(node.Left, key);
else if (cmp > 0) node.Right = Insert(node.Right, key);
return node;
}
public bool Search(K key) => Search(root, key) != null;
private Node? Search(Node? node, K key)
{
if (node == null) return null;
int cmp = key.CompareTo(node.Key);
if (cmp < 0) return Search(node.Left, key);
if (cmp > 0) return Search(node.Right, key);
return node;
}
public void Delete(K key) => root = Delete(root, key);
private Node? Delete(Node? node, K key)
{
if (node == null) return null;
int cmp = key.CompareTo(node.Key);
if (cmp < 0) node.Left = Delete(node.Left, key);
else if (cmp > 0) node.Right = Delete(node.Right, key);
else
{
if (node.Left == null) return node.Right;
if (node.Right == null) return node.Left;
var successor = Min(node.Right);
node.Key = successor.Key;
node.Right = Delete(node.Right, successor.Key);
}
return node;
}
private Node Min(Node node)
{
while (node.Left != null) node = node.Left;
return node;
}
public IEnumerable<K> Inorder()
{
var result = new List<K>();
Inorder(root, result);
return result;
}
private void Inorder(Node? node, List<K> result)
{
if (node == null) return;
Inorder(node.Left, result);
result.Add(node.Key);
Inorder(node.Right, result);
}
}class BST
Node = Struct.new(:key, :left, :right)
def initialize
@root = nil
end
def insert(key)
@root = insert_rec(@root, key)
end
def search(key)
!search_rec(@root, key).nil?
end
def delete(key)
@root = delete_rec(@root, key)
end
def inorder
result = []
inorder_rec(@root, result)
result
end
private
def insert_rec(node, key)
return Node.new(key, nil, nil) if node.nil?
if key < node.key
node.left = insert_rec(node.left, key)
elsif key > node.key
node.right = insert_rec(node.right, key)
end
node
end
def search_rec(node, key)
return nil if node.nil?
return node if node.key == key
key < node.key ? search_rec(node.left, key) : search_rec(node.right, key)
end
def min_node(node)
node = node.left while node.left
node
end
def delete_rec(node, key)
return nil if node.nil?
if key < node.key
node.left = delete_rec(node.left, key)
elsif key > node.key
node.right = delete_rec(node.right, key)
else
return node.right if node.left.nil?
return node.left if node.right.nil?
successor = min_node(node.right)
node.key = successor.key
node.right = delete_rec(node.right, successor.key)
end
node
end
def inorder_rec(node, result)
return if node.nil?
inorder_rec(node.left, result)
result << node.key
inorder_rec(node.right, result)
end
end<?php
class BSTNode {
public int $key;
public ?BSTNode $left;
public ?BSTNode $right;
public function __construct(int $key) {
$this->key = $key;
$this->left = null;
$this->right = null;
}
}
class BST {
private ?BSTNode $root = null;
public function insert(int $key): void {
$this->root = $this->insertRec($this->root, $key);
}
private function insertRec(?BSTNode $node, int $key): BSTNode {
if ($node === null) return new BSTNode($key);
if ($key < $node->key) $node->left = $this->insertRec($node->left, $key);
elseif ($key > $node->key) $node->right = $this->insertRec($node->right, $key);
return $node;
}
public function search(int $key): bool {
return $this->searchRec($this->root, $key) !== null;
}
private function searchRec(?BSTNode $node, int $key): ?BSTNode {
if ($node === null || $node->key === $key) return $node;
if ($key < $node->key) return $this->searchRec($node->left, $key);
return $this->searchRec($node->right, $key);
}
public function delete(int $key): void {
$this->root = $this->deleteRec($this->root, $key);
}
private function deleteRec(?BSTNode $node, int $key): ?BSTNode {
if ($node === null) return null;
if ($key < $node->key) {
$node->left = $this->deleteRec($node->left, $key);
} elseif ($key > $node->key) {
$node->right = $this->deleteRec($node->right, $key);
} else {
if ($node->left === null) return $node->right;
if ($node->right === null) return $node->left;
$successor = $this->minNode($node->right);
$node->key = $successor->key;
$node->right = $this->deleteRec($node->right, $successor->key);
}
return $node;
}
private function minNode(BSTNode $node): BSTNode {
while ($node->left !== null) $node = $node->left;
return $node;
}
public function inorder(): array {
$result = [];
$this->inorderRec($this->root, $result);
return $result;
}
private function inorderRec(?BSTNode $node, array &$result): void {
if ($node === null) return;
$this->inorderRec($node->left, $result);
$result[] = $node->key;
$this->inorderRec($node->right, $result);
}
}What Problems BSTs Solve
The core use case is any problem where you need a sorted collection that changes over time. An array is sorted but inflexible. A hash map is fast but unordered. A BST gives you both order and mutability.
Specific problem classes:
- Dynamic ordered sets. Insert and delete while maintaining sorted order. You need this when the set changes faster than a full re-sort is practical.
- Range queries. "Give me all keys between 50 and 100." Descend the tree once to find the lower bound, then traverse in-order until you exceed the upper bound. O(log n + k) where k is the number of results.
- Order statistics. "What's the kth smallest element?" With subtree size fields at each node, this is O(log n). Without sizes, it's O(n) inorder traversal.
- Floor and ceiling. "What's the largest key less than or equal to X?" Common in interval scheduling, rate limiting buckets, calendar event lookup.
- Predecessor and successor queries. "What comes right before or after this element?" O(h) with the techniques described above.
- Sorted iteration. Any time you need to process keys in order after dynamic updates, inorder traversal gives you O(n) with no re-sort.
Recognizing the Pattern
Look for ordered access plus dynamic updates together. The word "tree" might not appear anywhere in the problem statement.
Concrete signals:
- "Find the kth smallest/largest in a stream"
- "Find the closest value to X in a set"
- "Insert/delete values and answer range queries"
- "In-order traversal produces the answer"
- The word "sorted" appears and the set isn't static
Worked Example 1: Kth Smallest Element in a BST (LeetCode 230)
Problem: Given the root of a BST, return the kth smallest value.
Why BST fits: inorder traversal visits BST nodes in ascending order. You don't need to collect everything. Just count to k.
def kth_smallest(root, k):
stack = []
current = root
count = 0
while stack or current:
while current:
stack.append(current)
current = current.left
current = stack.pop()
count += 1
if count == k:
return current.key
current = current.rightThis is O(h + k) time, O(h) space. The BST property does all the work: you never sort anything, you never scan the full tree. The invariant gives you sorted order for free.
Worked Example 2: Validate Binary Search Tree (LeetCode 98)
Problem: Given a binary tree, determine if it's a valid BST.
Why BST fits: you're checking whether a tree satisfies the BST invariant. The standard recursive solution with range bounds is the clearest expression of what the invariant actually says.
def is_valid_bst(root, lower=float('-inf'), upper=float('inf')):
if root is None:
return True
if not (lower < root.key < upper):
return False
return (is_valid_bst(root.left, lower, root.key) and
is_valid_bst(root.right, root.key, upper))Going left: the new upper bound is the current node's key (nothing in the left subtree can be >= it). Going right: the new lower bound is the current node's key (nothing in the right subtree can be <= it). This enforces the global constraint, not just the parent-child check.
The Recap
- The invariant: left subtree < node < right subtree, recursive at every node.
- Every BST operation takes O(h) time: O(log n) on a balanced tree, O(n) on a degenerate one.
- Delete is the hardest case: two-child deletion replaces the node with its in-order successor (minimum of the right subtree), which always has at most one child.
- Inorder traversal gives sorted output in O(n) for free.
- A BST built from random keys has expected height ~4.31 ln n. Sorted input degrades to O(n).
- Hibbard deletion causes drift toward O(sqrt n) height under mixed insert/delete workloads. Use a self-balancing tree if your workload is heavy on deletions.
- Validation requires global range bounds, not just parent-child comparisons.
- Language standard libraries use self-balancing variants: red-black trees in Java (
TreeMap), C++ (std::map), and C# (SortedDictionary). Rust uses B-trees (BTreeMap) for cache efficiency. - Reach for a BST when you need a sorted dynamic collection, range queries, floor/ceiling lookups, or order statistics.
If you're working through BST problems for interviews, pattern recognition is usually where people lose points. You can know the operations cold and still miss that a problem wants a BST because the problem says "stream" or "closest value" rather than "tree." SpaceComplexity runs voice-based mock interviews where you work through problems like these out loud, and the rubric specifically scores whether you catch the right data structure early. That's a skill you build by practicing the recognition, not by reading about it.
For more on the interview communication side of tree problems, the post on staying vocal when you're stuck covers exactly what interviewers are listening for when you hit a hard case like the two-child deletion. And if the dynamic programming connection interests you (BSTs as one part of a larger algorithmic toolkit), the dynamic programming framework post walks through the recursive-with-memory pattern that appears in optimal BST construction as well.
Further Reading
- Binary search tree on Wikipedia covers the full history, variants, and formal analysis
- Sedgewick's BST chapter at Princeton (algs4) is the best textbook treatment of BSTs with subtree sizes, rank, and select
- GeeksforGeeks BST complexity comparison contrasts binary trees, plain BSTs, and AVL trees at a glance
- Rust BTreeMap documentation explains why the standard library uses a B-tree instead of a BST for ordered maps
- cppreference std::map documents the red-black tree backing C++'s ordered map with all iterator guarantees
