Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Binary Search Tree

A binary search tree is a linked tree structure that enforces one ordering rule at every node: every key in the left subtree is smaller than the node's key, and every key in the right subtree is larger. That invariant lets insert, search, and delete run in O(log n) time on average.

Each comparison discards an entire subtree, and a tree built from a random insertion order has O(log n) height on average.

This search pattern mirrors a higher-or-lower number-guessing game, where each comparison eliminates half the remaining candidates and twenty questions cover over a million values. A binary search tree stores those comparisons in pointers: each node tests whether the target is smaller or larger, and each edge follows one of the two outcomes.

How a Binary Search Tree Works

All three operations begin with the same root-to-leaf walk: compare the target against the current node, go left if smaller, go right if larger, and stop on an equal match. They differ only in what happens when the walk finishes.

insert(value) walks until the next step lands on an empty slot and attaches a new node there. Every inserted node starts as a leaf without restructuring nodes above it. If the walk hits an equal value, this implementation skips the insert and does not store duplicates. On an empty tree, the new node becomes the root directly.

search(value) walks until it matches the value (found) or reaches a null child (not found). Reaching null confirms the value is absent from the whole tree, because the ordering invariant rules out every skipped subtree.

delete(value) walks to the target, then splits into three cases by child count.

A leaf is unhooked directly: the parent's child pointer becomes null and no other node moves.

A node with one child is spliced out: the parent's pointer bypasses the removed node and links directly to its child. The surviving subtree moves up one level intact because all of its keys already satisfy the ordering constraint relative to the parent.

A node with two children cannot be unhooked directly without orphaning a subtree. Instead, find its in-order successor by stepping into the right subtree once and walking left as far as possible. That leftmost node holds the smallest value larger than the target, so it is a legal replacement for the target's slot: still larger than every key in the left subtree and still smaller than every remaining key in the right subtree. Copy the successor's value into the target node, then delete the successor from the right subtree. Because the successor has no left child by construction, deleting it always reduces to the leaf or one-child case. (The in-order predecessor, the rightmost node of the left subtree, works symmetrically; this lesson animates the successor version to match the displayed code.)

Binary Search Tree Step by Step

The player opens on an empty tree (BST initialized — empty). Build along with this trace using the Inputs panel: insert 50, 30, 70, 20, 40, 60, 80, in that order.

insert(50) finds an empty tree and installs 50 as the root with zero comparisons. insert(30) compares 30 < 50, moves left to a null slot, and attaches 30 as 50's left child. insert(70) mirrors this on the right. insert(20) takes two comparisons (20 < 50 goes left, 20 < 30 goes left) and attaches under 30. The remaining three keys each take two comparisons and land as 30's right child, 70's left child, and 70's right child, producing a balanced tree with 50 at the root, 30 and 70 below it, and 20, 40, 60, 80 along the bottom row.

Next, search(40) compares 40 < 50 (go left), 40 > 30 (go right), and 40 = 40 (found at depth 2), locating the target in three comparisons across seven nodes.

Next come three deletions, one per case. delete(20) walks 50 → left, 30 → left, finds 20 with zero children, and unhooks it so 30's left pointer becomes null. delete(30) walks one step left from 50 and finds 30 holding a single child, 40. The splice plays out in two beats: 50's left pointer swings past 30 to point at 40, then 30 lifts away and 40 slides up into its slot.

delete(50) exercises the two-child case at the root. Matched on the first comparison, 50 has two children, so the search finds its in-order successor: step right to 70, then left to 60, which has no left child. The value 60 is copied into the root slot, and the leaf 60 is removed from the right subtree. The final tree holds 60 at the root, 40 on its left, and 70 on its right with 80 as 70's right child, keeping the binary search tree invariant valid at every node.

In the animation, the descending cursor highlights each compared node while untouched subtrees stay at idle weight. Inserts show a ghost slot at the attach point one beat before the node lands; deletes flag the target node, then either prune it, splice around it as the parent's arrow swings to bypass it, or run the successor walk with a dashed connector pairing successor and target before copying and removing the successor.

Time and Space Complexity

OperationAverageWorst case (degenerate tree)
SearchO(log n)O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)
SpaceO(n)O(n)

The average-case time complexity is O(log n) because each comparison drops one subtree and a roughly balanced tree has O(log n) height. Nothing in a plain BST enforces balance, however. Inserting 1, 2, 3, 4, 5 attaches each new node to the right of the previous one, degenerating the tree into a linked list of height n − 1 where every operation takes O(n) time. Self-balancing variants (AVL trees, red-black trees, and treaps) prevent this degeneration, which is why a standard library's TreeMap uses a red-black tree rather than a plain BST.

The Max depth slider (3 to 5) is a layout limit for the canvas rather than a BST property. An actual binary search tree has no depth limit, so degenerate chains grow without bound. When an insert would exceed the slider's depth, the player reports the limit explicitly; the Insert button likewise disables at the perfect-tree node count for the chosen depth.

Edge Cases

  • Empty tree. search reports not found immediately, insert makes the new node the root, and delete is a no-op.
  • Delete the root. Handled like any other node: a two-child root uses successor replacement, and a one-child root promotes its child to root.
  • Insert a duplicate. The walk reaches the equal node and stops without inserting. Other conventions exist (a count field, or routing equal keys to the right), as long as insert, search, and delete all follow the same rule consistently.
  • Delete a missing value. The walk falls off the tree into null; not found, tree untouched.
  • Single-node tree, delete it. Handled by the leaf case; the tree returns to empty.

Common Mistakes

  • Removing a two-child node in place. Both subtrees would be orphaned. Copy the in-order successor (or predecessor) value into the slot first, then delete that replacement node.
  • Picking the wrong replacement. For a two-child delete, only the in-order successor (the leftmost node of the right subtree) or the in-order predecessor (the rightmost node of the left subtree) preserves the ordering invariant. Any other key breaks the invariant for at least one descendant.
  • Inserting sorted data and wondering why everything is slow. Sorted input builds the degenerate chain. If you cannot randomize insertion order, you need a self-balancing tree.
  • Confusing a BST with a heap. A BST orders left-to-right and supports ordered traversal and range queries; a heap orders parent-over-child and supports O(1) find-min but no ordered iteration.