Data Structures

Balanced Search Trees

Balanced search trees keep sorted data in a tree whose height stays small, so lookups, inserts, and deletes stay fast no matter what order the data arrives in. Here is a practical guide to the five you will meet most often: the comparison table first, then each tree in depth.

At a glance

A plain binary search tree is fast only if it stays short. Insert keys in sorted order and it turns into a long chain, and every lookup takes O(n) steps. A balanced search tree fixes this. It reshapes itself as you insert and delete, so its height stays close to log n and every operation stays fast.

The five trees below solve this in different ways. AVL and red-black trees use rotations on binary trees. B-trees and B+ trees use wide nodes built for disks. Splay trees skip the rules and move recently used items to the top. Use the table to compare them, then open each page for an interactive visualization.

Data Structures Search (worst)Insert (worst)Delete (worst)Balance ruleHeight boundBest for
AVL Tree O(log n)O(log n)O(log n)Subtree heights differ by at most 1 (rotations)At most about 1.44 log₂ nRead-heavy in-memory lookups
Red-Black Tree O(log n)O(log n)O(log n)Color rules: no two reds in a row, same black count on every pathAt most 2 log₂(n + 1)General-purpose in-memory maps and sets, write-heavy loads
B-Tree O(log n)O(log n)O(log n)Node splits and merges keep every leaf at the same depthAbout log_b n for fan-out b (3 to 4 levels for a billion keys)Disk-based indexes and file systems
B+ Tree O(log n)O(log n)O(log n)Node splits and merges; data only in linked leavesAbout log_b n; internal nodes hold no data, so fan-out is higherDatabase indexes with range queries
Splay Tree O(n) single op; O(log n) amortizedO(n) single op; O(log n) amortizedO(n) single op; O(log n) amortizedNone stored; splays accessed node to the rootNot bounded (can reach n); amortized cost is O(log n)Skewed access patterns, caches, recently-used data

Each algorithm in depth

Click any card for the full article with an interactive visualization, complexity analysis, code examples, and FAQ.

Which balanced search tree should I pick?

Start with where the data lives and how it is used.

  • Data fits in memory, and you want a sorted map or set: use a red-black tree. It is the safe default, and most standard libraries already ship one.
  • Data fits in memory and you mostly read: an AVL tree is a little faster for lookups because it is shorter.
  • Data lives on disk or in pages: use a B-tree, or a B+ tree if you also need range scans.
  • A few keys are hit again and again: a splay tree adapts to that, and it needs no extra balance fields.
  • You need a hard limit on the time of every single operation: avoid the splay tree. Its worst case for one operation is O(n).

AVL vs red-black tree: the trade-off

Both are binary search trees with O(log n) worst-case search, insert, and delete. They differ in how strict they are.

An AVL tree requires the left and right subtrees of every node to differ in height by at most 1. That gives a height of at most about 1.44 log₂ n, which is close to perfect. Shorter trees mean faster lookups. The cost is that an insert or delete can break the rule in many places, so you may rotate up to O(log n) times on a delete.

A red-black tree is looser. It only promises that the longest path is at most twice the shortest, so its height can reach 2 log₂(n + 1). Lookups can be a bit slower. But an insert needs at most 2 rotations, and a delete at most 3, so writes are cheaper. Stricter balance means faster reads and more rotations. Looser balance means slightly slower reads and fewer rotations. In practice the gap is small, which is why the choice often comes down to what your library already offers.

Why databases use B-trees and B+ trees

A disk, or an SSD, reads data in fixed-size pages, often 4 to 16 KB. Reading one page costs far more than comparing keys already in memory. So the goal is to touch as few pages as possible.

A binary tree has one key per node. For a billion keys it is about 30 levels deep, which can mean 30 page reads. A B-tree packs hundreds of keys into one node, sized to fill a page. Its fan-out, the number of children per node, is in the hundreds. That makes the tree only 3 to 4 levels deep for a billion keys, so a lookup reads 3 to 4 pages.

A B+ tree goes one step further. It keeps records only in the leaves and links the leaves in sorted order. Internal nodes hold just keys, so more of them fit in a page and the tree is even shorter. A query like WHERE age BETWEEN 30 AND 40 finds the first match, then follows the leaf links. That is why B+ trees are the usual choice for database indexes, while plain B-trees show up where range scans matter less.

B-tree vs B+ tree

The main differences are small but matter.

  • Where data lives: a B-tree stores records in every node. A B+ tree stores them only in the leaves.
  • Range scans: a B+ tree walks the linked leaves. A B-tree must go up and down the tree to visit keys in order.
  • Search cost: a B-tree can finish early if the key is in an upper node. A B+ tree always goes down to a leaf, but every lookup then takes the same number of steps.
  • Fan-out: B+ internal nodes carry no records, so they fit more keys and the tree is shorter.

Where each tree is used in real systems

  • Red-black trees: C++ std::map and std::set (in common implementations), Java TreeMap, and the Linux kernel, including the Completely Fair Scheduler, which picks the next task by finding the smallest virtual runtime.
  • AVL trees: in-memory indexes and lookup tables where reads far outnumber writes.
  • B+ trees: index structures in most relational databases (such as PostgreSQL, MySQL InnoDB, and SQLite) and in several file systems.
  • B-trees: file systems and storage engines that want a balanced on-disk index.
  • Splay trees: caches, memory allocators, and routing or packet-classification tables where some keys are far more popular than others. A catch is that every read changes the tree, so shared trees that many threads read at once need locks.

Rotations and node splits: two ways to stay balanced

A rotation is a small local change that moves a node up one level and its parent down one level, while keeping the sorted order. AVL, red-black, and splay trees all use rotations. B-trees and B+ trees do not. When a node gets too full they split it in two and push the middle key up to the parent. When a node gets too empty they merge it with a neighbor. The tree only grows taller when the root itself splits, so every leaf always stays at the same depth.

Frequently asked questions

What is a balanced search tree?

It is a search tree that reshapes itself during inserts and deletes so its height stays close to log n. That keeps search, insert, and delete fast even when keys arrive in sorted order, which would turn an ordinary binary search tree into a long chain.

What is the difference between an AVL tree and a red-black tree?

An AVL tree keeps sibling subtree heights within 1 of each other, so it is shorter and has faster lookups but needs more rotations on updates. A red-black tree allows its longest path to be twice its shortest, so lookups are slightly slower but inserts and deletes need fewer rotations. Both give O(log n) worst-case time.

Which is faster, AVL or red-black?

AVL is usually a little faster for lookups. Red-black is usually a little faster for inserts and deletes. With a mix of reads and writes, the difference is small, so either works. Most standard libraries pick red-black.

What is the difference between a B-tree and a B+ tree?

A B-tree stores records in both internal nodes and leaves. A B+ tree stores records only in the leaves and links the leaves in order. The links make range scans fast, and the data-free internal nodes allow higher fan-out, so a B+ tree is usually the better database index.

Why do databases use B+ trees instead of binary trees?

Disks read whole pages, and each read is slow. A binary tree needs about 30 reads for a billion keys. A B+ tree with hundreds of keys per node needs only 3 to 4. It also supports range queries by walking the linked leaves.

Is a splay tree balanced?

Not in the strict sense. It stores no balance information and its height can reach n. It is self-adjusting: each access moves the node to the root. Over any sequence of operations the cost is O(log n) amortized per operation, but one operation can take O(n).

What does amortized O(log n) mean?

It means the average cost per operation over a whole sequence is O(log n), even though a single operation may cost more. Splay trees use this guarantee. AVL, red-black, B-trees, and B+ trees do better: every single operation is O(log n) in the worst case.

Which self-balancing binary search tree should I use?

Use a red-black tree as the default, since it balances cheaply and is widely available. Choose an AVL tree if lookups dominate. Choose a splay tree only if access is heavily skewed and occasional slow operations are acceptable. If the data is on disk, use a B-tree or B+ tree instead.