Binary Search Trees Explained

A contacts app has to keep names A→Z, find any of them quickly, and add new ones all day. A sorted array finds fast but inserts slowly; a hash table finds fast but forgets the order. A binary search tree does all three.

Data structures · 2026-11-25 · 7 min read

The one rule

A binary search tree (BST) is made of nodes, each with up to two children. Every node follows one rule: everything in its left subtree is smaller, everything in its right subtree is bigger. That rule is the whole trick.

Searching: half the tree per step

To find “Eve”, start at the root. Is Eve smaller than this name? Go left. Bigger? Go right. Each step throws away about half of what is left, so a balanced tree of a million names needs only about 20 steps: O(log n).

def find(node, name):
    while node:
        if name == node.name: return node
        node = node.left if name < node.name else node.right
    return None

Inserting: no shifting

To add a name, search for it; where the search falls off the tree, hang a new node. Nothing else moves. Compare that with a sorted array, where adding “Eve” near the front shifts every later name along: O(n).

The catch: sorted input

Insert names already in A→Z order and every new one is bigger than the last, so each goes to the right. The “tree” becomes one long line, and searching it is O(n) again, no better than a list.

Self-balancing trees fix this by rotating nodes after inserts so the height stays around log n whatever the input order:

  • AVL trees keep the two sides of every node within one level of each other: very fast lookups.
  • Red-black trees balance a little more loosely with cheaper inserts. Java’s TreeMap and C++’s std::map use them.
  • B-trees put many keys in each node; database indexes use them.

Range queries: what hash tables can’t do

Because the tree is ordered, you can ask for “every name from K to N”: find K, then walk in order until you pass N, skipping whole subtrees that are out of range. That costs O(log n + k) for k results. A hash table scatters keys randomly, so it would have to check every one.

OperationBalanced BSTHash tableSorted array
Find a keyO(log n)O(1) averageO(log n)
InsertO(log n)O(1) averageO(n)
Items in orderO(n)Sort firstO(n)
Range (K to N)O(log n + k)O(n)O(log n + k)
Smallest / largestO(log n)O(n)O(1)

Where you meet them

Java’s TreeMap and TreeSet, C++’s std::map and std::set, and database indexes (as B-trees) are all search trees. Python has no built-in one; the sortedcontainers package fills the gap.

Lesson: hash tables →

See it happen

In the interactive lesson a contacts app has to stay A→Z. You grow a tree, watch an A→Z import squash it into a line, rebalance it, then light up only the names from K to N.

Play: binary search trees →

Frequently asked questions

What is a binary search tree?

A tree where each node has up to two children, with smaller values on the left and bigger values on the right. That ordering lets you search, insert and list items in order quickly.

Why is BST search O(log n)?

Each comparison discards one side of the tree, roughly halving what is left. That only holds while the tree is balanced; a lopsided tree can degrade to O(n).

What is a balanced binary search tree?

A BST that restructures itself (with rotations) so its height stays around log n, whatever order data arrives in. AVL and red-black trees are the common kinds.

BST or hash table?

Use a hash table when you only need to look keys up. Use a balanced BST when you also need keys in order, ranges, or the smallest and largest key.

Keep reading

← All guides

Try AI Flowchart free →