Perfect Binary Tree Nodes
In one sentence: The formula counts every node in a perfect binary tree, a branching diagram in which each row is completely full, using only the number of rows below the top.
Read it out loud: “N equals 2 to the power of h plus 1, minus 1. N is the total number of nodes, the boxes of the tree. The raised 2 is a power, which means 2 multiplied by itself, the number of times shown in the exponent. The exponent is h plus 1, and the minus 1 is done last. h is the height of the tree, the number of lines from the top box down to the bottom.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Powers of two, like
A power shows repeated multiplication. means 2 multiplied by itself 3 times: . The small raised number is the exponent. Each time the exponent goes up by one, the power doubles: , , , . When powers of the same number are multiplied, the exponents add, so .
- Tree, node, root, child and leaf
A tree is a diagram of boxes joined by lines, with one box at the top. Each box is a node. The top box is the root. A node directly below another is its child, and a node with no children at the bottom is a leaf. A binary tree is one where each node has at most two children, one on the left and one on the right.
- Height, measured in edges
An edge is one line joining two nodes. The height of a tree is the number of edges on the longest path from the root down to a leaf. The root alone has height 0, and a root with two children has height 1. Height counts the lines, not the boxes, and that choice changes the exponent in the formula by one.
- Levels, or rows
A level is a row of nodes that are all the same number of edges below the root. Level 0 is the root, level 1 holds its children, and so on. The bottom level holds the leaves. A tree of height has levels, numbered from 0 to .
- Perfect tree and the dots
A perfect binary tree is one in which every node that is not a leaf has exactly two children, and every leaf sits on the same bottom level. Drawn in rows, every row is completely full. The three dots mean ‘continue in the same pattern’, so means add every power of 2 from up to .
What each symbol means
| Symbol | Meaning |
|---|---|
| The total number of nodes in the tree: every box, counting the root, the inner nodes and the leaves. It is a plain count with no unit. The example tree has 15 nodes. | |
| The height of the tree: the number of edges on the longest path from the root down to a leaf. It is a plain count with no unit, and the root alone has height 0. The example uses height 3, which gives four levels, numbered 0 to 3. |
The formula piece by piece
Start with the bottom level. Each level has double the nodes of the level above it, so level holds nodes, and these are the leaves. At height 3 the bottom level holds leaves.
The exponent is one bigger than the height, because a tree of height has levels. Doubling the bottom level’s count does not give the total, but is the number needed before subtracting one. At height 3 it is .
The whole formula as one sentence: the number of nodes in a perfect binary tree equals two to the power of one more than its height, minus one. For height 3 that is nodes.
The same total written level by level. The root has 1 node, level 1 has 2, level 2 has 4, and so on, down to the bottom level with nodes. Adding every level gives the same count as the formula, which is the second way to see why the formula works.
What the formula is doing, and why
A binary tree is a set of nodes where each node has at most two children. A perfect binary tree is one where every inner node has exactly two children and every leaf sits at the same depth. Drawn in rows, every level is completely full, with no gaps.
Counting levels. Define the height as the number of edges from the root down to the bottom. The root alone has height 0. Level is the row of nodes that are edges below the root, so level 0 is the root. The bottom level is level , and there are levels in all.
Why doubling? Each node on one level has two children on the next level. So level 0 has node, level 1 has nodes, and level 2 has nodes. In general, level has nodes. Every new level doubles the count of the level above it, which is why the total grows so fast.
Why is the sum one less than a power of two? Adding every level from 0 to gives . Each term is double the one before it, and a list like this always adds up to one less than the next power of two. Check with : , and . So the total is . The in the exponent comes from the levels, and the comes from this sum.
How big does it get? The count grows fast, because each new level doubles the bottom row. A perfect binary tree of height 20 has nodes, which is 2,097,151 nodes. Each extra level of height roughly doubles the total.
An everyday picture
Think of a knockout tournament bracket with eight team slots. The final is the root, two semifinals sit below it, four quarterfinals below those, and eight team slots form the bottom row. Each game is an inner node, and each team slot is a leaf. There are 7 games, and the 8 team slots plus the 7 games make 15 nodes, which is exactly what gives.
Worked example: A knockout bracket with 8 team slots
A knockout tournament with 8 team slots is drawn as a perfect binary tree. The final is the root, and each team slot is a leaf at the bottom. The tree’s height is : going from the final down to a team slot takes 3 lines. The example uses simple counts, so each row can be counted by hand.
- Read the height. The final is the root at the top, and a team slot is a leaf at the bottom. Going from the final down to any team takes 3 lines, so the height is . That gives four rows, numbered 0 to 3, with row 3 holding the team slots.
- Count the nodes on each row. Row 0 holds node, the final. Row 1 holds semifinals. Row 2 holds quarterfinals. Row 3 holds team slots. Each row has double the count of the row above it, because each game has two slots feeding into it.
- Add the rows. nodes in total. The 8 team slots are the leaves on the bottom row. The other nodes, 1 + 2 + 4 = 7, are games, because a game is an inner node that joins two slots.
- Use the formula in one step. . The exponent is , and . Subtracting 1 gives nodes, the same as the row total from the previous step.
- Count the games. There are 15 nodes in all, and 8 of them are team slots, so the games are . This matches a general rule for knockouts: a bracket with teams has games, here .
- Check the bottom row. The bottom row is . Eight team slots fill it with no gaps, which is what makes the tree perfect. If a slot were missing, the tree would have fewer nodes than the formula gives.
- Say it plainly. An 8-team knockout has 15 nodes: 8 team slots and 7 games. The formula found that total from the height alone, so there is no need to count every row, although counting the rows is a good way to check the answer.
Answer: The tree has 15 nodes in total: 8 team slots on the bottom row and 7 games above them. The formula gives the total from the height alone, .
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Counting the root as height 1. Height counts edges, so the root alone has height 0. This off-by-one error is one of the most common mistakes with this formula.
- Leaving out the in the exponent. Using gives 7 for instead of 15. The formula needs , because a tree of height has levels.
- Using the formula for a tree that is not perfect. If some leaves sit higher than others, or a row has a gap, the tree has fewer nodes than the formula gives.
- Mixing up nodes and edges. A tree with nodes always has edges, because each edge joins a node to its parent, and every node except the root has exactly one parent.
Where you will meet it
- Analyzing binary search trees and heaps, where the height decides how many steps a search or an insert can take.
- Tournament brackets and other knockout trees, where each leaf is a competitor and each inner node is a match.
- Estimating the memory a tree of fixed height needs, since each node takes about the same amount of space.
- Proving facts about tree-shaped data by counting levels, as in the textbook arguments about how fast tree operations run.
Questions people ask
Is a perfect binary tree the same as a complete binary tree?
No. A perfect tree has every row full. A complete tree fills every row except possibly the last, and the last row fills from the left with no gaps. Every perfect tree is complete, but not every complete tree is perfect.
How many leaves does a perfect binary tree have?
leaves for height , because each row doubles the one above it. Height 3 gives leaves, and height 10 gives leaves.
Why is there a +1 in the exponent?
A tree of height has rows, because height counts edges and the rows are numbered from 0 to . The sum of all the rows is , so the exponent must be one bigger than the height.
Related formulas
- Binary Search Steps: Binary search finds an item in a sorted list by checking the middle and throwing away half, and this formula counts how many checks that takes at most.
- Binary Number Value: Binary uses only the digits 0 and 1, and this formula turns a row of those digits into the ordinary number they stand for.
- Euler's Formula for Planar Graphs: Euler’s formula says that if you draw a connected network on a flat sheet with no crossing lines, then corners minus lines plus regions always comes to 2.