Arrays, stacks, queues and linked lists are linear: each item has one item after it. A tree is hierarchical: one item can lead to several others, which lead to more. This page introduces the vocabulary of trees, shows how a binary tree is built from nodes and pointers in C++, and lets you build trees and watch the classic recursive algorithms run on them.
A tree is a set of nodes connected by edges. One node, the root, is at the top. Every other node has exactly one parent, and a node can have any number of children. There are no loops: from the root there is exactly one path down to each node.
You already know trees: a family tree, a company org chart, the folders on your computer, the outline of an essay. Each is a hierarchy, where every item belongs to exactly one thing above it.
In a binary tree, every node has at most two children, called the left child and the right child. Left and right are different positions: a node with only a left child is a different tree from a node with only a right child.
| Term | Meaning | In the example |
|---|---|---|
| Root | The one node with no parent, at the top. | 8 |
| Parent / child | A node directly above / directly below another, joined by an edge. | 6 is the parent of 4 and 7 |
| Siblings | Nodes with the same parent. | 3 and 10 |
| Leaf | A node with no children. | 1, 4, 7, 13 |
| Internal node | A node with at least one child. | 8, 3, 10, 6, 14 |
| Ancestors / descendants | Every node on the path up to the root / every node below. | Ancestors of 7: 6, 3, 8 |
| Subtree | A node together with all of its descendants. Every node is the root of its own subtree. | 3, 1, 6, 4, 7 |
| Depth (level) | The number of edges from the root down to the node. The root has depth 0. | depth of 6 is 2 |
| Height | The number of edges on the longest path from a node down to a leaf. A leaf has height 0, and an empty tree has height −1. | height of the tree is 3 |
| Size | The number of nodes. | 9 |
8. Every node except the root has exactly one edge going up to its parent, so a tree with n nodes always has n − 1 edges, whatever its shape.
No. If it did, there would be two different paths from the root to it, and that would make a cycle in the drawing. Structures that allow it are called graphs (or DAGs if the edges have a direction), which you met with topological sort.
Each node holds three things: its data, a pointer to its left child, and a pointer to its right child. A missing child is a nullptr. The whole tree is reached through one pointer to the root.
struct Node {
int data;
Node* left; // nullptr if there is no left child
Node* right; // nullptr if there is no right child
Node(int value) : data(value), left(nullptr), right(nullptr) {}
};
// Building the top of the example tree by hand:
Node* root = new Node(8);
root->left = new Node(3);
root->right = new Node(10);
root->left->left = new Node(1);
A class with public members works the same way. This is the same idea as a linked list node, except that each node has two "next" pointers instead of one.
nullptr.Node* left) instead of a whole child node (Node left)?A Node that contained a whole Node would contain another whole Node inside that, and so on forever, so it would have infinite size; C++ will not compile it. A pointer is a fixed size and can also be nullptr, which is how we say "no child here."
Build your own binary tree. Click a node to select it: its subtree is shaded, its path to the root is outlined, and the facts on the right describe it. Use the buttons to add children, change values, or remove a subtree.
The explorer labels a tree with these shape names when they apply. Try the presets, then try to build each one yourself.
A perfect tree: 1 + 2 + 4 + … + 2h = 2h+1 − 1 nodes. Height 3 gives 15.
The smallest is about log₂ n (exactly ⌊log₂ n⌋), when the tree is as bushy as possible. The largest is n − 1, when it is skewed into a single chain. This gap is why balanced trees matter: searching a path costs O(height), which can be O(log n) or O(n).
A traversal visits every node once. Because a tree is defined recursively (a node, plus a left subtree, plus a right subtree), the natural traversals are recursive too. They differ only in when the node itself is visited:
Height and size follow the same pattern: ask each subtree for its answer, then combine the two answers at the node.
Preorder 8 3 1 6 4 7 10 14 13. Inorder 1 3 4 6 7 8 10 13 14. Postorder 1 4 7 6 3 13 14 10 8. Notice that the inorder comes out sorted; the example happens to be a binary search tree, which is coming up next.
Postorder. A node may only be deleted after both of its subtrees are deleted, because once it is gone you can no longer reach its children through it.
Preorder: create the copy of a node first, then copy its left and right subtrees and attach them to it.
No: different shapes can share a preorder (for example 1 with a left child 2, and 1 with a right child 2 both give "1 2"). With both the preorder and the inorder of a tree with distinct values, the tree is unique: the preorder tells you the root, and the inorder tells you which values are in its left and right subtrees.
Level order must finish all nodes at depth d before any at depth d + 1. A queue is first-in, first-out, so children wait behind everything already on the current level. Recursion goes as deep as it can first, which is depth-first order.
O(n): each node is visited once and does a constant amount of work. The extra memory is O(height) for the recursion stack, or up to O(width) for the level-order queue.
A heap is always complete, so its nodes fill the array with no gaps and the children of index i are always at 2i + 1 and 2i + 2. A general tree can have missing children anywhere; storing it in an array would leave holes, and a skewed tree of height h would need about 2h+1 slots for only h + 1 nodes.
× at the root, with + (children 3 and 4) on the left and 5 on the right. Preorder: × + 3 4 5 (prefix notation). Inorder: 3 + 4 × 5, which needs parentheses to mean the right thing. Postorder: 3 4 + 5 × (postfix, which a stack can evaluate directly).