Trees

A tree is a hierarchical data structure of nodes connected by edges, with one root and no cycles — each node has children, forming branches. Trees model anything naturally nested or hierarchical: file systems, the DOM, org charts, decision processes, parsed expressions. They also power efficient ordered operations: a balanced search tree gives O(log n) insert, lookup, and delete while keeping data sorted — something a hash table can't do.

The most important family is the binary search tree (BST), where each node's left subtree holds smaller values and its right subtree larger ones. That ordering invariant is what enables fast search — as long as the tree stays balanced, which is the central concern.

TL;DR

Quick Example

A binary tree node points to its children; ordering makes it a BST:

Core Concepts

Why Balance Matters

A BST's O(log n) performance depends on it being balanced (height ~log n). Insert sorted data into a naive BST and it degenerates into a glorified linked list — height n, operations O(n):

Self-balancing trees (AVL, red-black) automatically rotate to stay balanced, guaranteeing O(log n). Most language standard libraries' ordered maps/sets use a balanced tree (often red-black) under the hood.

Traversals

The first three are depth-first and naturally recursive.

Specialized Trees

Best Practices

Common Mistakes

A plain BST with sorted inserts

Using a tree when a hash table is enough

FAQ

When should I use a tree instead of a hash table?

Use a tree (specifically a balanced BST / ordered map) when you need ordering: range queries ("all keys between X and Y"), sorted iteration, or finding the nearest key. A hash table gives faster O(1) lookups but has no order. So if you only do exact key lookups, use a hash table; if you also need sorted traversal or ranges, use a balanced tree. Many languages' TreeMap/std::map are exactly this ordered-tree structure.

Why does a binary search tree need to be balanced?

The BST's O(log n) performance comes from halving the search space at each level, which requires the height to be ~log n. If the tree becomes lopsided — for example, inserting already-sorted data into a naive BST — it degenerates into a chain of height n, and operations become O(n), no better than a list. Self-balancing trees (AVL, red-black) perform rotations on insert/delete to keep the height logarithmic, guaranteeing O(log n).

What's the difference between the tree traversals?

They differ in when you visit a node relative to its children. In-order (left, node, right) visits a BST in sorted order. Pre-order (node, then children) is used to copy or serialize a tree. Post-order (children, then node) processes children first — useful for deleting or evaluating. Level-order (BFS) visits breadth-first, level by level, using a queue. The first three are depth-first and recursive; level-order is iterative with a queue.

What are B-trees and why do databases use them?

A B-tree is a balanced tree where each node holds many keys and has many children, keeping it short and wide. That shape minimizes disk reads — each node maps to a disk page, so even huge datasets need only a few levels (a few disk seeks) to reach any key. Databases and filesystems use B-trees (and B+-trees) for indexes because they provide O(log n) lookups and efficient range scans while playing well with block-based storage. See Database Indexing.

Related Topics

References