What Is a Binary Search Tree and How Does It Work?

A binary search tree (BST) is a way of organizing data so you can find, add, or remove items quickly. It works like a decision tree: at each step, you go left or right based on whether the value you’re looking for is smaller or larger than the current item. This simple rule makes searching through even large collections of data remarkably fast.

How the Ordering Rule Works

A binary search tree is built from “nodes,” where each node holds a value and can point to up to two children: one on the left and one on the right. The core rule is straightforward: every value in a node’s left subtree is smaller than that node, and every value in its right subtree is larger. This applies at every level, not just the immediate children.

Picture a node holding the number 50. Every value branching off to its left (30, 20, 40, and so on) must be less than 50. Every value branching off to its right (70, 60, 80) must be greater. Now zoom into the node holding 30. Its left children must all be less than 30, and its right children must all be between 30 and 50. This recursive structure is what makes the whole thing work.

When you search for a value, you start at the top node (called the root) and compare. If your target is smaller, you move left. If it’s larger, you move right. You keep going until you either find the value or hit a dead end. Each comparison eliminates roughly half the remaining data, which is the same principle behind looking up a word in a dictionary by flipping to the middle rather than reading every page.

Why It’s Fast (and When It Isn’t)

On average, searching, inserting, or deleting a value in a BST takes O(log n) time, where n is the number of items stored. In practical terms, that means doubling the data only adds one extra step. A tree holding 1,000 items takes about 10 comparisons to search. A tree holding 1,000,000 items takes about 20.

That efficiency depends on the tree being reasonably balanced, meaning values are spread somewhat evenly between left and right branches. The problem arises when data arrives in a bad order. If you insert values that are already sorted (1, 2, 3, 4, 5…), every new value goes to the right, and the tree degrades into a straight chain, essentially a linked list. In that worst case, every operation takes O(n) time, meaning you’d potentially check every single item. A tree of 1,000,000 items would require up to 1,000,000 comparisons instead of 20.

Traversal: Reading Data Back Out

One of the most useful properties of a BST is that you can read all its values back in sorted order without doing any extra sorting work. This is done through “in-order traversal,” which follows a simple pattern: visit the left subtree, then the current node, then the right subtree. Because smaller values always live on the left and larger values on the right, this naturally produces an ascending sequence.

Two other traversal patterns exist. Pre-order traversal visits the current node first, then the left subtree, then the right. This is useful when you need to copy or serialize the tree’s structure. Post-order traversal visits the left subtree, then the right subtree, then the current node. This pattern comes up when you need to delete or process child nodes before their parent. All three traversals visit every node exactly once.

What BSTs Are Used For

Binary search trees are one of the most fundamental data structures in computer science because they combine fast lookups with the ability to add and remove items on the fly. An ordered array gives you fast search, but inserting a new value means shifting everything over. A linked list lets you insert easily, but finding a specific item means checking each one. A BST gives you the best of both.

The most common real-world application is the symbol table, also called a dictionary or map. This is any system that stores key-value pairs and lets you look up a value by its key. Programming language compilers use symbol tables to track variable names. Databases use tree-based structures to index records. File systems use them to locate files. Anytime software needs to maintain a collection of items in sorted order while still supporting fast insertions and deletions, a BST or one of its variants is a natural fit.

Self-Balancing Trees Solve the Worst Case

Because a basic BST can degrade into a chain with sorted input, computer scientists developed self-balancing variants that automatically reorganize themselves to stay efficient. The two most widely used are AVL trees and Red-Black trees.

An AVL tree enforces a strict rule: at every node, the left and right subtrees can differ in height by no more than 1. After each insertion or deletion, the tree checks this condition and performs “rotations” (small structural rearrangements) to restore balance. This guarantees O(log n) performance for every operation, but the constant checking and rotating adds some overhead, particularly during heavy insertions.

A Red-Black tree takes a more relaxed approach. Instead of enforcing a strict height difference, it assigns a color (red or black) to each node and follows a set of rules about how those colors can be arranged. The result is that the longest path from top to bottom is never more than twice the length of the shortest path. It’s not perfectly balanced, but it’s balanced enough to guarantee O(log n) time for search, insert, and delete. Because it requires fewer rotations than an AVL tree, it tends to perform better when you’re doing a lot of insertions and deletions. Red-Black trees are the backbone of many standard library implementations, including Java’s TreeMap and C++’s std::map.

The tradeoff is simple: AVL trees are slightly faster for lookups because they’re more tightly balanced, while Red-Black trees are faster for workloads with frequent modifications. Both eliminate the catastrophic worst case that a plain BST can fall into.