A binary tree is a hierarchical data structure in which every node holds a value and at most two child references, conventionally called the left child and the right child. Among data structures in programming, it is the foundation for binary search trees, heaps, expression trees and Huffman coding trees.
The structure is defined recursively: a binary tree is either empty or a root node with a left subtree and a right subtree, each of which is itself a binary tree. This recursive definition explains why most binary tree algorithms are written as short recursive functions whose running time depends heavily on the height of the tree.
How a binary tree is structured
Each node in a binary tree stores a payload and two pointers, either of which may be null. A node without children is a leaf, a node with at least one child is an internal node, and the single node without a parent is the root. The depth of a node is the number of edges from the root to it, and the height of the tree is the greatest depth of any leaf.
Several exact relationships hold for every binary tree. A tree of height h contains at most 2^(h+1) – 1 nodes, so a tree with n nodes has a minimum height of about log2 n. In a tree where every internal node has two children, the number of leaves is always one more than the number of internal nodes.
The table below defines the vocabulary that appears in almost every discussion of binary trees.
| Term | Meaning | Example value |
|---|---|---|
| Root | Top node with no parent | Exactly one per non-empty tree |
| Leaf | Node with no children | Up to (n + 1) / 2 in a tree of n nodes |
| Height | Longest root-to-leaf path in edges | From log2 n to n – 1 |
| Depth | Edges from the root to a node | Root has depth 0 |
| Subtree | A node with all its descendants | Every node roots one subtree |
Types of binary trees
Binary trees are classified by shape constraints, and these constraints determine which operations stay efficient. The terms are used inconsistently across textbooks, so precise definitions matter when reading specifications or interview questions.
| Type | Definition | Typical use |
|---|---|---|
| Full binary tree | Every node has zero or two children | Expression trees, Huffman trees |
| Complete binary tree | All levels filled except the last, filled left to right | Binary heaps stored in arrays |
| Perfect binary tree | All internal nodes have two children, all leaves at one depth | Tournament brackets, segment trees |
| Balanced binary tree | Subtree heights differ by a bounded amount | AVL and red-black trees |
| Degenerate tree | Each node has at most one child | Worst case, behaves like a linked list |
A binary search tree is not a shape but an ordering rule: every key in the left subtree is smaller than the node key and every key in the right subtree is larger. A heap uses a different rule, the heap property, where each parent is ordered relative to its children only.
Traversal and core operations
Traversal visits every node exactly once, so all standard traversals run in O(n) time. The order of visiting determines what the traversal is useful for, and the choice between recursion and an explicit stack or queue determines the memory profile.
| Traversal | Visit order | Typical purpose |
|---|---|---|
| Pre-order | Node, left, right | Copying or serializing a tree |
| In-order | Left, node, right | Sorted output from a binary search tree |
| Post-order | Left, right, node | Freeing memory, evaluating expressions |
| Level-order | Breadth first, level by level | Shortest depth, printing by level |
Depth-first traversals use O(h) auxiliary space for the call stack, where h is the height, while level-order traversal uses a queue that can hold up to about n/2 nodes at the widest level. Morris traversal achieves in-order traversal in O(1) extra space by temporarily rewiring null pointers, at the cost of modifying the tree during the walk.
Representation and performance
A binary tree is stored either as linked nodes or as an implicit array. Linked nodes suit irregular shapes and frequent insertions, whereas the array form places node i’s children at indices 2i + 1 and 2i + 2, which removes pointer overhead and improves cache locality for complete trees.
Operation costs depend on height rather than node count, as the comparison below shows for a binary search tree.
| Operation | Balanced tree | Degenerate tree |
|---|---|---|
| Search | O(log n) | O(n) |
| Insert | O(log n) | O(n) |
| Delete | O(log n) | O(n) |
| Find minimum | O(log n) | O(n) |
| Full traversal | O(n) | O(n) |
For one million keys, a balanced tree needs about 20 comparisons per lookup, while a degenerate one may need up to a million. This gap is why production libraries use self-balancing variants such as AVL trees and red-black trees instead of plain binary search trees.
Binary trees compared with related structures
The binary tree is a special case of the general tree, which allows any number of children per node. Restricting the branching factor to two simplifies algorithms and proofs, but it increases height compared with wide trees such as B-trees, which databases prefer because each node fits a disk page.
Compared with a hash table, a balanced binary search tree is slower for single lookups but keeps keys in sorted order, supporting range queries, predecessor and successor searches. Compared with a trie, it compares whole keys rather than characters, so a trie is often faster for prefix searches over strings.
Common mistakes with binary trees
- Inserting sorted data into a plain search tree produces a degenerate tree with linear operations.
- Unbounded recursion on deep trees can overflow the call stack; iterative traversal avoids this.
- Confusing complete with full or perfect leads to wrong assumptions about array storage.
- Validating a search tree by checking only parent and child misses violations deeper in subtrees; pass minimum and maximum bounds instead.
- Forgetting null children in recursive base cases causes null pointer errors.
The simple explanation
A binary tree is like a family tree turned upside down in which every person has at most two children. You start at the top and, at each step, choose to go left or right.
If the tree is well shaped, you can reach any item in very few steps, much like guessing a number between one and a thousand by always asking whether it is higher or lower. If the tree grows lopsided, it becomes a long chain and finding things gets slow.
How it is used in practice
A developer writing a spreadsheet formula engine parses an expression such as (A1 + B1) * 3 into a binary expression tree. Each operator becomes an internal node with two operands as children, and numbers or cell references become leaves.
To compute the result, the engine runs a post-order traversal: it evaluates both children first and then applies the operator at the parent. When a user edits cell A1, only the affected path is recalculated. For formulas with dozens of operators, evaluation takes microseconds, and the same tree is reused for syntax highlighting and error messages, so the parser is written once and serves three features.