Trees in Graph Theory
A tree in graph theory is a connected graph with no cycles. Every tree with n vertices has exactly n - 1 edges, and there is exactly one path between any two of its vertices. A rooted tree picks one vertex as the root, which gives every other vertex a parent, children and a level.
Before this lesson: Graph Theory Basics, Bipartite Graphs and Dijkstra’s Algorithm
What is a tree in graph theory?
A cycle is a closed path that returns to its start without repeating an edge or a vertex. A graph is acyclic if it has no cycles. A tree is a graph that is both connected and acyclic: you can get from any vertex to any other, but never by going around a loop.
Think of a family tree, a folder system on a computer or the branches of a real tree. Each one spreads out without ever joining back on itself. If you cut any single branch, the part beyond the cut falls off, so a tree has no spare edges.
A leaf is a vertex of degree 1, and a vertex of degree 2 or more is an internal vertex. A forest is an acyclic graph that may be disconnected, so each of its connected pieces is a tree.
Properties of trees
The properties below are all equivalent. If a graph with n vertices has any one of them, it has all of them, and it is a tree.
- G is connected and has no cycles (the definition).
- There is exactly one path between every pair of vertices.
- G is connected and has exactly n - 1 edges.
- G has no cycles and has exactly n - 1 edges.
- G is connected, but removing any one edge disconnects it. (Every edge is a bridge.)
- G has no cycles, but adding any one new edge creates exactly one cycle.
Two more facts are used constantly. Every tree with at least 2 vertices has at least 2 leaves. A forest with n vertices and c trees has n - c edges, because each tree has one fewer edge than vertices.
e = n - 1 \qquad\text{(tree)} \qquad\qquad e = n - c \qquad\text{(forest with } c \text{ trees)}Why a tree has n − 1 edges
The proof uses induction on n. A tree with 1 vertex has 0 edges. For a tree with n \ge 2 vertices, take a longest path. Its last vertex is a leaf: any other neighbor would either extend the path or close a cycle.
Remove that leaf and its one edge. What remains is still connected and acyclic, so it is a tree with n - 1 vertices, and by the induction hypothesis it has n - 2 edges. Putting the leaf back adds one edge, for n - 1 in all. This is a proof by mathematical induction, and the same leaf-removal idea proves many other facts about trees.
Rooted trees
A rooted tree is a tree with one vertex chosen as the root. Every edge is then directed away from the root, which gives the tree a family structure.
- The parent of a vertex v (other than the root) is the vertex just before v on the unique path from the root to v. The vertex v is a child of its parent, and vertices with the same parent are siblings.
- The ancestors of v are the vertices on the path from the root to v, not counting v. The descendants of v are all vertices that have v as an ancestor.
- In a rooted tree, a leaf is a vertex with no children, and an internal vertex is a vertex with at least one child.
- The level (or depth) of v is the length of the path from the root to v, so the root is at level 0. The height of the tree is the largest level.
- The subtree at v is v together with all of its descendants.
A rooted tree is an m-ary tree if every internal vertex has at most m children, and a full m-ary tree if every internal vertex has exactly m children. A 2-ary tree is called a binary tree. Counting the vertices of a full m-ary tree gives three useful formulas, where i is the number of internal vertices and \ell is the number of leaves.
n = mi + 1 \qquad \ell = (m - 1)i + 1 \qquad n = i + \ell
The first formula holds because every vertex except the root is the child of exactly one internal vertex, and each internal vertex has m children. An m-ary tree of height h also has at most m^h leaves, so a tree with \ell leaves has height at least \log_m \ell.
Trees in graph theory examples
Example 1: Is the graph a tree?
Decide whether each graph in the first figure is a tree. G1 has vertices A to G and edges AB, AC, BD, BE, CF, CG. G2 has vertices P to V and edges PQ, QR, RP, ST, TU, UV.
n = 7
Count the vertices of G1.
e = 6
Count the edges of G1.
e = n - 1
Compare: 6 = 7 − 1, so G1 has the right number of edges for a tree.
A \to B \to D,\; A \to B \to E,\; A \to C \to F,\; A \to C \to G
Check connectivity: these paths reach every vertex from A.
\text{G1 is a tree}A connected graph with n - 1 edges is a tree.
n = 7,\; e = 6
G2 also has 7 vertices and 6 edges.
P \to Q \to R \to P
But G2 contains a cycle.
\text{G2 is not a tree}A tree has no cycles.
The edge count alone does not decide the question. G2 has n - 1 edges, but its triangle uses up an edge that should have joined {P, Q, R} to {S, T, U, V}, so the graph is also disconnected.
Example 2: Counting leaves from degrees
A tree has 4 vertices of degree 3, 2 vertices of degree 2, and every other vertex is a leaf. How many leaves does it have, and how many vertices and edges?
\ell = \text{number of leaves}Name the unknown.
n = 4 + 2 + \ell = 6 + \ell
Add up the vertices.
e = n - 1 = 5 + \ell
A tree has n - 1 edges.
4(3) + 2(2) + \ell(1) = 2e
Use the handshaking lemma: the degrees add up to 2e.
12 + 4 + \ell = 2(5 + \ell)
Multiply and substitute e = 5 + \ell.
16 + \ell = 10 + 2\ell
Simplify both sides.
6 = \ell
Subtract 10 + \ell from both sides.
n = 6 + 6 = 12
Find the number of vertices.
e = 12 - 1 = 11
Find the number of edges.
12 + 4 + 6 = 22 = 2(11)
Check: the degree sum is twice the edge count.
The tree has 6 leaves, 12 vertices and 11 edges.
Example 3: Rooted tree vocabulary
Use the rooted tree in the second figure, with root r.
\text{parent}(h) = dThe vertex just above h is d.
\text{children}(e) = \{f, g\}The vertices just below e.
\text{siblings of } c = \{d\}c and d share the parent a.
\text{ancestors}(g) = \{e, b, r\}Follow the path from g up to the root.
\text{descendants}(a) = \{c, d, h\}Everything below a.
\text{leaves} = \{c, h, f, g\}Vertices with no children.
\text{internal} = \{r, a, b, d, e\}Vertices with at least one child.
\text{level}(h) = 3The path r, a, d, h has 3 edges.
\text{height} = 3The largest level is 3.
Notice that b has only one child, yet it is not a leaf. In a rooted tree, use “no children” to find the leaves, not “degree 1”: a root with one child has degree 1 but is not a leaf.
Example 4: Counting a full 3-ary tree
A full 3-ary tree has 13 internal vertices. Find the number of vertices and leaves, and the smallest height it could have.
m = 3,\; i = 13
Identify the values.
n = 3(13) + 1
Use n = mi + 1.
n = 39 + 1 = 40
Multiply, then add.
\ell = (3 - 1)(13) + 1
Use \ell = (m - 1)i + 1.
\ell = 2(13) + 1 = 27
Multiply, then add.
i + \ell = 13 + 27 = 40
Check: internal vertices plus leaves give n.
27 \le 3^h
An m-ary tree of height h has at most m^h leaves.
3^2 = 9 \lt 27 \quad\text{and}\quad 3^3 = 27Try heights with exponent rules: height 2 is too small.
h \ge 3
The height is at least 3.
The tree has 40 vertices and 27 leaves, and its height is at least 3. Height exactly 3 happens when every leaf sits on level 3.
Example 5: A single-elimination tournament (word problem)
A chess club runs a single-elimination tournament for 64 players: each match has two players, and the loser goes home. How many matches are played, and how many rounds are needed?
\text{leaves} = \text{players},\; \text{internal vertices} = \text{matches}Model the bracket as a full binary tree: each match has 2 incoming players or winners.
\ell = (2 - 1)i + 1
Use \ell = (m - 1)i + 1 with m = 2.
64 = i + 1
Substitute \ell = 64.
i = 63
Subtract 1 from both sides.
64 \le 2^h
A binary tree of height h has at most 2^h leaves.
h \ge \log_2 64 = 6
Take the base-2 logarithm of both sides.
The tournament has 63 matches, and it needs at least 6 rounds (a balanced bracket uses exactly 6). There is a shortcut for the match count: every match eliminates exactly one player, and 63 players must be eliminated.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Treating n - 1 edges as enough on its own | “7 vertices and 6 edges, so G2 is a tree.” | Also check that the graph is connected or that it has no cycles. Either one plus n - 1 edges is enough. |
| Calling every degree-1 vertex a leaf in a rooted tree | A root with one child is listed as a leaf. | In a rooted tree, a leaf is a vertex with no children. |
| Mixing up level and height | “The height of the tree in Example 3 is 9, its number of vertices.” | Level is the distance from the root to one vertex; height is the largest level. |
| Using full-tree formulas on a tree that is not full | A binary tree in which one internal vertex has a single child, with \ell = i + 1 used anyway. | Use n = mi + 1 and \ell = (m - 1)i + 1 only when every internal vertex has exactly m children. |
| Using n - 1 edges for a forest | A forest of 3 trees with 20 vertices is said to have 19 edges. | A forest with c trees has n - c edges: here 17. |
| Forgetting the root is at level 0 | The children of the root are said to be on level 2. | Count edges from the root: the root is level 0, its children level 1. |
Related ideas
Every tree is bipartite. Color the vertices by whether their level from any chosen root is even or odd. Each edge joins consecutive levels, so a tree with at least 2 vertices is a bipartite graph with chromatic number 2 (see graph coloring).
Spanning trees. A spanning tree of a connected graph is a subgraph that is a tree and includes every vertex. The paths found by Dijkstra’s algorithm form one, the shortest-path tree. The next lesson, Minimum Spanning Trees, finds the spanning tree with the smallest total weight.
Counting labeled trees. Cayley’s formula says there are n^{n-2} different trees on n labeled vertices. For n = 4 that is 4^2 = 16 trees: 12 are paths and 4 are stars.
Binary trees in computing. Search trees, expression trees and file systems are rooted trees. Tree Traversal shows how to visit every vertex of a binary tree in a fixed order.
Practice problems
- A tree has 25 vertices. How many edges does it have?
- A connected graph has 10 vertices and 9 edges. Is it a tree? Explain.
- A forest has 30 vertices and 4 trees. How many edges does it have?
- Can a graph with 8 vertices and 8 edges be a tree?
- A tree has 3 vertices of degree 4, one vertex of degree 3, and every other vertex is a leaf. How many leaves does it have?
- In the rooted tree of Example 3, list the ancestors of h and every vertex at level 2.
- A full binary tree has 20 internal vertices. How many vertices and leaves does it have?
- A full 4-ary tree has 61 vertices. How many internal vertices and leaves does it have?
- Can a full 5-ary tree have exactly 100 leaves?
- A school phone tree starts with the principal. Every person who makes calls calls exactly 3 others, and 121 people take part in all (the principal included). How many people make calls, how many only receive a call, and what is the fewest rounds of calls needed?
Show answers
1.
n = 25
Identify the number of vertices.
e = 25 - 1 = 24
A tree has n - 1 edges.
The tree has 24 edges.
2.
e = 9 = 10 - 1
Compare the edge count with n - 1.
\text{connected and } e = n - 1This is one of the equivalent conditions for a tree.
Yes, it is a tree.
3.
n = 30,\; c = 4
Identify the values.
e = n - c = 30 - 4 = 26
A forest with c trees has n - c edges.
The forest has 26 edges.
4.
n - 1 = 7
A tree on 8 vertices has exactly 7 edges.
8 \ne 7
Compare with the given edge count.
No. With 8 edges on 8 vertices the graph must contain a cycle.
5.
n = 3 + 1 + \ell = 4 + \ell
Add up the vertices.
e = n - 1 = 3 + \ell
A tree has n - 1 edges.
3(4) + 1(3) + \ell = 2(3 + \ell)
The degrees add up to 2e.
15 + \ell = 6 + 2\ell
Simplify both sides.
9 = \ell
Subtract 6 + \ell from both sides.
12 + 3 + 9 = 24 = 2(12)
Check with e = 3 + 9 = 12.
The tree has 9 leaves (13 vertices and 12 edges).
6.
r \to a \to d \to h
Follow the path from the root to h.
\text{ancestors}(h) = \{d, a, r\}List the vertices before h.
\text{level 2} = \{c, d, e\}Vertices 2 edges below the root.
The ancestors of h are d, a and r; the level-2 vertices are c, d and e.
7.
n = 2(20) + 1 = 41
Use n = mi + 1 with m = 2.
\ell = (2 - 1)(20) + 1 = 21
Use \ell = (m - 1)i + 1.
20 + 21 = 41
Check: i + \ell = n.
The tree has 41 vertices and 21 leaves.
8.
61 = 4i + 1
Use n = mi + 1 with m = 4.
60 = 4i
Subtract 1 from both sides.
i = 15
Divide both sides by 4.
\ell = (4 - 1)(15) + 1 = 46
Use \ell = (m - 1)i + 1.
15 + 46 = 61
Check: i + \ell = n.
The tree has 15 internal vertices and 46 leaves.
9.
100 = (5 - 1)i + 1
Use \ell = (m - 1)i + 1 with m = 5.
99 = 4i
Subtract 1 from both sides.
i = 24.75
Divide both sides by 4.
No. The number of internal vertices must be a whole number, so a full 5-ary tree cannot have exactly 100 leaves.
10.
n = 121,\; m = 3
Model the calls as a full 3-ary tree rooted at the principal.
121 = 3i + 1
Use n = mi + 1.
120 = 3i
Subtract 1 from both sides.
i = 40
Divide both sides by 3.
\ell = 121 - 40 = 81
Use n = i + \ell.
81 \le 3^h
A 3-ary tree of height h has at most 3^h leaves.
3^4 = 81
The smallest power of 3 that reaches 81.
h \ge 4
So at least 4 rounds.
40 people make calls, 81 only receive a call, and at least 4 rounds of calls are needed.
Trees in graph theory FAQ
What is a tree in graph theory?
A tree is a connected graph with no cycles. Equivalently, it is a graph with exactly one path between every pair of vertices.
How many edges does a tree have?
A tree with n vertices always has n - 1 edges. A forest made of c separate trees has n - c edges.
What is the difference between a tree and a forest?
A forest is any graph with no cycles, so it may be disconnected. Each connected piece of a forest is a tree, and a tree is simply a forest with one piece.
What is a rooted tree?
A rooted tree is a tree with one vertex chosen as the root. Choosing a root gives every other vertex a parent, children and a level, which is how family trees and computer file systems are organized.
Summary
- A tree is a connected graph with no cycles; a forest is a graph with no cycles.
- A tree with n vertices has n - 1 edges and exactly one path between any two vertices; a forest with c trees has n - c edges.
- Any two of “connected”, “no cycles” and “n - 1 edges” together prove a graph is a tree.
- Every tree with at least 2 vertices has at least 2 leaves, and every tree is bipartite.
- In a rooted tree, the root is at level 0, leaves have no children, and the height is the largest level.
- A full m-ary tree with i internal vertices has mi + 1 vertices and (m - 1)i + 1 leaves.
Previous lesson: Dijkstra’s Algorithm
Next lesson: Minimum Spanning Trees
Back to the Discrete Math Index