A binary tree is a data structure that organizes values as a set of connected nodes, where each node has at most two children, conventionally called the left child and the right child. The structure starts from a single top node called the root and branches downward. That two-child rule is the whole definition; everything else, like search trees and balancing, builds on it. Binary trees matter because the branching shape lets software find, insert, and order data quickly, which is why they appear in search structures, databases, and language parsers. This explainer covers how they work, a concrete example, and common misconceptions.
How a binary tree works
Each node holds a value and references to up to two child nodes. A node with no children is a leaf. The depth of the tree is how many levels it has from root to deepest leaf. The power of the shape comes from how you arrange values.
In a plain binary tree the arrangement can be anything. In a binary search tree (BST), there is a rule: every value in a node left subtree is smaller, and every value in its right subtree is larger. That ordering means you can find a value by starting at the root and going left or right based on a comparison, discarding half the remaining tree at each step — the same halving idea behind a fast lookup. If the underlying idea of breaking a problem into smaller pieces interests you, Big O notation explains why that halving is so efficient.
Why balance matters
A binary search tree is only fast if it stays roughly balanced. Insert values in already-sorted order into a naive BST and it grows into a long one-sided chain — effectively a list, with slow lookups. Self-balancing variants such as red-black trees and AVL trees automatically restructure themselves on insert and delete to keep the tree short and wide, preserving fast operations.
| Operation |
Balanced binary search tree |
Unbalanced (worst case) |
| Search |
Fast, halves each step |
Slow, scans like a list |
| Insert |
Fast |
Slow |
| Delete |
Fast |
Slow |
| Shape |
Short and wide |
Long and lopsided |
| Used in |
Ordered maps, indexes |
What you want to avoid |
The takeaway: the tree shape, not just the rule, determines speed.
A concrete example
Imagine inserting the numbers 8, 3, 10, 1, 6 into a binary search tree. The 8 becomes the root. The 3 is smaller, so it goes left; the 10 is larger, so it goes right. The 1 is smaller than 8 and smaller than 3, so it lands left of the 3. The 6 is smaller than 8 but larger than 3, so it sits right of the 3. To find 6, you compare with 8 (go left), then 3 (go right), and arrive — two comparisons instead of scanning all five values.
Common misconceptions
- A binary tree is always sorted. No. Only a binary search tree enforces ordering; a plain binary tree can hold values in any arrangement.
- More children means a binary tree. A node can have at most two children. Allow more and it is a different structure entirely.
- Trees are automatically fast. Only balanced trees are. An unbalanced tree can be as slow as a list.
- You must build one yourself. Most languages ship ordered maps and sets backed by balanced trees. Reach for those before hand-rolling.
FAQ
What is the difference between a binary tree and a binary search tree?
A binary tree only requires each node to have at most two children. A binary search tree adds an ordering rule that keeps smaller values left and larger values right, enabling fast lookups.
Why are binary trees useful?
Their branching shape lets you search, insert, and order data quickly, often by halving the work at each step. They underpin ordered maps, database indexes, and parsers.
What does balancing a tree mean?
Keeping the tree short and wide rather than long and lopsided. Self-balancing trees restructure themselves on changes so operations stay fast in the worst case.
Should I implement a binary tree myself?
For learning, yes — it teaches the core ideas. For production, prefer the ordered map or set your language provides, since those are balanced, tested, and tuned.
Where to go next
Understand Big O notation, learn what a hash table is, and see what a data structure is more broadly.