Binary trees
Nodes that each branch at most two ways. Seven steps: the parts of a binary tree, its height, the full, complete and perfect shapes, and how a complete tree fits in a plain array.
In 30 seconds
A binary tree starts at a root node and branches downward. Every node has at most two children, called left and right; a node with none is a leaf.
Three shapes have names. Full: each node has zero or two children. Complete: every level is filled, the last one from the left. Perfect: every level is totally full.
The height is the longest path from the root down. Packed trees are very low for their size, about log2 of the node count, and a complete tree can be stored in an array with no pointers.
Binary trees, step by step
Height is counted in edges here, so a single node has height 0; some books count nodes instead, giving one more. A few authors also use “full” and “complete” the other way round.
- Step 1: Two child slots per node. A binary tree is made of nodes. Each node holds a value and has two slots for children, a left one and a right one; either can be empty. The top node is the root, and a node with no children is a leaf. Here C has only a left child, and G, E and F are the leaves.
- Step 2: Depth and height. The depth of a node is how many edges lie between it and the root, so nodes at the same depth form a level. The height of the tree is the depth of its deepest leaf: the longest way down. This tree has 7 nodes and height 3. Height matters because many tree operations walk one path down from the root.
- Step 3: Full: 0 or 2 children. A binary tree is full when no node has just one child: every node is either a leaf or has both children. The shape can still be lopsided, as here. A full tree with L leaves always has L − 1 inner nodes: 4 leaves and 3 inner nodes make 7.
- Step 4: Complete: packed to the left. A binary tree is complete when every level is entirely filled, except possibly the last one, and the last level is filled from the left without gaps. New nodes can only go in the next free position: K would be the right child of E. Complete does not imply full: here E has just one child.
- Step 5: Perfect: every level full. A perfect binary tree has every level completely full, so it is both full and complete, and all its leaves are at the same depth. Each level holds twice as many nodes as the one above: 1, 2, 4, 8. With height h that adds up to 2^(h+1) − 1 nodes, which is 15 for height 3. Over half of them are leaves.
- Step 6: Height versus node count. The same 7 nodes can make a tree of height 2 or of height 6. Packed level by level, the height is about log2 of the node count, which grows very slowly. Strung out with one child each, the tree is just a linked list and the height is n − 1. This gap is why balanced trees are worth the effort.
- Step 7: A complete tree in an array. Because a complete tree has no gaps, its nodes can be written into an array level by level, left to right, with no pointers at all. The shape is recovered by arithmetic: the children of the node at index i are at 2i + 1 and 2i + 2. For B at index 1 that gives 3 and 4: D and E. This is how heaps are stored.
Try it yourself
Learn this first
Spotted a mistake on this page?