BST: search, insert, min and max
In a binary search tree smaller keys go left and larger keys go right. Each comparison picks a direction, so search and insert follow one path.
The idea
In a binary search tree, smaller keys sit on the left of a node and larger keys on the right.
The rule holds at every node, not only at the root. That is what lets one comparison discard a whole subtree.
Intuition
Looking for 6. It is smaller than 8, so the whole right subtree of 8 cannot hold it.
At 3, the key 6 is bigger, so 1 is out too. Each comparison drops a whole subtree.
In a balanced tree each comparison removes about half of the keys left. That is why search is fast.
Complexity
Search follows one path, so it costs the height of the tree. A balanced tree has height about log2 n.
Insert follows the same path and adds one leaf, so it costs the same. Min and max also follow one path.
Pitfalls
Keys inserted in sorted order build a chain. Search then costs n comparisons, like a linear scan.
A plain BST does not rebalance itself. Balanced variants such as AVL trees fix this.
Variations
Find the key closest to 7.4: walk the same path and remember the best key seen. Here that is 7.
Code
Pseudocode
node ← root
while node ≠ null
if key = node.val: return node
if key < node.val: node ← node.left
else: node ← node.right
return null
insert(root, key)
if root = null: return new node(key)
node ← root
loop
if key = node.val: return root
if key < node.val
if node.left = null: node.left ← new node(key); return root
node ← node.left
else
if node.right = null: node.right ← new node(key); return root
node ← node.right
findMin(root)
node ← root
while node.left ≠ null: node ← node.left
return node.val
findMax(root)
node ← root
while node.right ≠ null: node ← node.right
return node.val
Python
def search(root, key):
node = root
while node:
if key == node.val:
return node
if key < node.val:
node = node.left
else:
node = node.right
return None
def insert(root, key):
if root is None:
return TreeNode(key)
node = root
while True:
if key == node.val:
return root
if key < node.val:
if node.left is None:
node.left = TreeNode(key)
return root
node = node.left
else:
if node.right is None:
node.right = TreeNode(key)
return root
node = node.right
def find_min(root):
node = root
while node.left:
node = node.left
return node.val
def find_max(root):
node = root
while node.right:
node = node.right
return node.val
JavaScript
function search(root, key) {
let node = root;
while (node !== null) {
if (key === node.val) return node;
if (key < node.val) node = node.left;
else node = node.right;
}
return null;
}
function insert(root, key) {
if (root === null) return new TreeNode(key);
let node = root;
for (;;) {
if (key === node.val) return root;
if (key < node.val) {
if (node.left === null) { node.left = new TreeNode(key); return root; }
node = node.left;
} else {
if (node.right === null) { node.right = new TreeNode(key); return root; }
node = node.right;
}
}
}
function findMin(root) {
let node = root;
while (node.left !== null) node = node.left;
return node.val;
}
function findMax(root) {
let node = root;
while (node.right !== null) node = node.right;
return node.val;
}
Cost at a glance
| Access | O(log n) / O(n) |
|---|---|
| Search | O(log n) / O(n) |
| Insert | O(log n) / O(n) |
| Delete | O(log n) / O(n) |
| Space | O(n) |
Practice problems (7)
Insert a sequence Warm-up
The keys 5, 8, 3, 7, 1, 9, 4 are inserted into an empty binary search tree in that order. Answer each question before the step plays.
Which tree is a search tree? Warm-up
Exactly one of these trees is a valid binary search tree. Pick it.
Cost of a search Warm-up
This function searches a binary search tree with n keys. The tree may be badly unbalanced. Give its worst-case time and space.
Search Standard
Given the root of a binary search tree and a val, find the node whose value equals val and return the subtree rooted at it. Return null if there is no such node.
Insert Standard
Insert a new key val into the binary search tree with the given root and return the root of the resulting tree. The key is not yet in the tree. The new key becomes a leaf.
Min and max Standard
Given the root of a binary search tree, return a list [min, max] with the smallest and the largest key. Return an empty list for an empty tree.
Closest value Challenge
Given the root of a binary search tree and a number target, return the key in the tree that is closest to target. If two keys are equally close, return the smaller one.
The tree is not empty.
Related topics
Next on the path: BST delete; successor and predecessor