Store keys by shape so search, insert, and delete each walk a single path down the tree.
A binary search tree (BST) keeps numbers sorted *by shape*. Every node holds a key, and obeys one rule everywhere: all smaller keys live in its left branch, all larger keys in its right. That single invariant turns the tree into a decision diagram — at each node you compare once and step left or right, so you only ever walk *one path* from the root to where the answer lives.
Insert 50, 30, 70, 20, 40, 60, 80 into an empty tree. Each new key starts at the root and steps left (smaller) or right (larger) until it falls off the tree into an empty slot, where it becomes a new leaf:
insert order: 50 30 70 20 40 60 80
50
/ \
30 70
/ \ / \
20 40 60 80
40 went: 40 < 50 -> left to 30; 40 > 30 -> right -> empty slot.Searching for 60: 60 > 50 go right to 70; 60 < 70 go left to 60 — found in 3 steps. The cost is the height of the tree, not the number of nodes.
An in-order traversal — visit left branch, then the node, then right branch — emits a BST's keys from smallest to largest. For the tree above it prints 20 30 40 50 60 70 80. This is why a BST is sometimes described as a sorted structure you can also insert into cheaply.
function inorder(node, out) {
if (!node) return;
inorder(node.left, out); // smaller keys first
out.push(node.key); // then this node
inorder(node.right, out); // then larger keys
}10, 20, 30, 40 in order and the tree degenerates into a right-leaning chain of height *n* — every search becomes O(n), no better than a linked list. Self-balancing variants (AVL, red-black) exist precisely to prevent this.