Definition · Binary search treeA binary tree in which, at every node x, every key in the left subtree of x is less than the key at x, and every key in the right subtree of x is greater than it. Duplicate keys are excluded here.
The condition is quantified over subtrees, not over children. That is the whole of the definition and the source of the only misconception in this lesson.
Search follows one path. Compare the target with the root: equal and you are done; smaller and the target can only be in the left subtree, because every key to the right is larger; larger and only in the right. Repeat. The number of comparisons is one per level visited, so at most the height plus one. Insertion is the same walk, ending at the empty position where the search failed.
Theorem · The in-order walk of a search tree is sortedTraverse left subtree, then node, then right subtree, recursively. The sequence of keys produced is in increasing order if and only if the tree is a binary search tree.
One direction is the invariant unrolled: everything in the left subtree is smaller than the node and is emitted before it, and everything in the right subtree is larger and is emitted after. The other direction is why this is useful as a test — a walk that comes out sorted certifies the invariant everywhere, including the subtree conditions the local check never looks at.
Example · The tree that passes the local checkRoot 20, left child 10, right child 30, and 25 as the right child of 10. Local check: 10 < 20, 30 > 20, 25 > 10. All pass. In-order walk: 10, 25, 20, 30 — not sorted, and the walk names the offender. The lab builds this tree and evaluates both checks on it in your browser, reporting the local one as satisfied and the real one as violated.
Deleting a node, in three cases
No children: unlink it. One child: splice the child into its place, which preserves the invariant because the whole subtree moves up unchanged and its keys were already on the correct side of everything above. Two children: neither child can simply take the node’s place, because the other subtree would have nowhere correct to go.
So replace the key rather than the node. The in-order successor is the smallest key in the right subtree — walk right once, then left as far as possible. Every key in the left subtree is smaller than it (they were smaller than the node, which is smaller than everything right), and every remaining key in the right subtree is larger than it (it was the smallest). So it can sit where the deleted node was. And it has no left child, or it would not have been the smallest, so removing it from where it was is one of the two easy cases.
insert 50 30 70 20 40 60 80, then delete 30
50 50
30 70 40 70
20 40 60 80 20 60 80
30 has two children, 20 and 40
successor of 30 = smallest key right of it = 40, which has no children
40 takes 30's place; 20 stays where it was
in-order before 20 30 40 50 60 70 80
in-order after 20 40 50 60 70 80 one key gone, still sorted
The lab counts 13 comparisons for those eight operations and reports the resulting height as 2. Deleting the root instead of 30 is the same rule with a longer reach: the successor of 50 is 60, which lives in the other subtree, and 60 becomes the root while 70 keeps 80 as its right child. The tree is different and the in-order sequence is still sorted, which is the only thing the invariant asks.
What the counts do and do not say
Search for 40 in the seven-key tree before that deletion and the lab reports the path 50 → 30 → 40: three comparisons, and the panel prints the route so the path is a thing you can see rather than infer. Search for 55 and it reports three comparisons and not found. Run the same eight operations with the keys arriving as 20, 30, 40, 50, 60, 70, 80 and the seven insertions build a path of height 6, the whole sequence costs 23 comparisons rather than 13, and the delete leaves a tree of height 5.
That last pair is the warning. The invariant says nothing about the shape, so it says nothing about the cost: 13 comparisons and 23 comparisons come from the same keys and the same algorithm, and the only difference is the order they arrived in. A search tree is O(log n) per operation exactly when it is short, and nothing established so far makes it short. The next lesson is about what does.