A binary search tree (BST) is a binary tree that keeps its keys in order. That one rule lets search, insert and delete each follow a single path from the root, instead of looking at every node. This page explains the rule, steps through each operation in C++, and lets you build trees and watch what their shape does to the cost.
For every node in the tree:
Both subtrees must be binary search trees too. In this page each key appears at most once; inserting a key that is already there changes nothing.
The rule means one comparison at a node rules out a whole subtree. If you are looking for 7 and the root is 8, everything on the right is larger than 8, so 7 cannot be there.
No. The rule is about whole subtrees, not just children. Picture 8 with left child 3, and 9 as the right child of 3. Each parent and child pair looks fine, but 9 is in 8's left subtree and is larger than 8. A search for 9 would go right at the root and never find it.
The smallest is the leftmost node: keep following left pointers from the root until there is no left child. The largest is the rightmost node. No comparisons are needed.
Search is naturally recursive. Start at the root and look for key. There are four cases:
nullptr: the key is not in the tree.key: found it.key is smaller than the node's key: search the left subtree.key is larger than the node's key: search the right subtree.Node* search(Node* root, int key) {
if (root == nullptr || root->key == key)
return root;
if (key < root->key)
return search(root->left, key);
return search(root->right, key);
}
Each call moves one level down, so search makes at most h + 1 comparisons, where h is the height of the tree. It takes O(h) time, and the recursion uses O(h) stack space.
4: 8, 3, 6, 4 (found after 4 comparisons). 10: 8, 10 (found after 2). 3: 8, 3 (found after 2). 20: 8, 10, 14, then 14's right child is empty, so 20 is not in the tree after 3 comparisons. Try them in the lab below with the "Search example" preset.
It is exactly where the key would go if you inserted it. That is why insert is just a search that keeps going until it falls off the tree.
A new key is always added as a leaf. Search for it from the root, going left or right at each node, until you reach an empty spot, and put the new node there.
The recursive version returns a pointer to the root of the subtree, and each caller stores that pointer back into its left or right field. At the empty spot it returns the brand new node, and that is how the new node gets linked to its parent.
Node* insertNode(Node* root, int key) {
if (root == nullptr)
return new Node(key);
if (key < root->key)
root->left = insertNode(root->left, key);
else if (key > root->key)
root->right = insertNode(root->right, key);
return root; // key already present: unchanged
}
insertNode return a Node*, and why does the caller write root->left = insertNode(root->left, key)?When the recursion reaches an empty spot, the parent's left or right pointer must change from nullptr to the new node, but the call only has a copy of that pointer. Returning the new pointer lets the parent store it. For an empty tree the root itself changes, which is why main writes root = insertNode(root, 50).
3. 10 is the root, with 5 and 15 below it. 2 and 7 go under 5, 12 and 20 under 15, then 6 and 9 under 7, 18 under 20 and 14 under 12. Check it with the matching preset in the lab.
First search for the node to delete. What happens next depends on how many children it has.
nullptr.It is larger than everything in the left subtree (it came from the right side) and smaller than everything else in the right subtree (it is the minimum there). So it fits exactly where the deleted key was.
The successor is the leftmost node of the right subtree, so it has no left child. It has at most one child, so deleting it is case 1 or case 2.
Yes: the largest key in the left subtree (the rightmost node on the left) also fits, and it has no right child. Either works; some implementations alternate between them to keep the tree from leaning to one side.
Every operation steps through the C++ on the right, with the call stack and the path from the root highlighted.
The class code, with a search and a maxValueNode added, and with insert leaving an existing key alone.
Every operation walks one path, so its cost is O(h), the height of the tree. What that means depends on the order the keys arrived in:
| Operation | Time | Balanced tree | Skewed tree |
|---|---|---|---|
| Search | O(h) | O(log n) | O(n) |
| Insert | O(h) | O(log n) | O(n) |
| Delete | O(h) | O(log n) | O(n) |
| Min / max | O(h) | O(log n) | O(n) |
| Inorder traversal | O(n) | O(n) | O(n) |
Each key is larger than every key before it, so it always goes right. The tree is a single chain of 11 nodes with height 10. Searching for 25 takes 11 comparisons, the same as scanning a list. The order of insertion, not the keys themselves, decided the shape.
Insert the middle first, then the middles of each half: 4, 2, 6, 1, 3, 5, 7. Any order where each node arrives before everything in its subtrees and the middles go first works. Try it with "Build from a list".
Inorder prints the whole left subtree (all smaller keys), then the node, then the whole right subtree (all larger keys). Applied at every node, that is exactly increasing order. This also gives a sort: insert n keys, then traverse, in O(n log n) when the tree stays balanced.
If the tree is balanced, about 20, since 2²⁰ ≈ 1,000,000. If it is a chain, up to 1,000,000. Self-balancing trees such as AVL and red-black trees restructure themselves on insert and delete to keep the height near log₂ n.