Trees and Spanning Trees
What You’ll Learn
Section titled “What You’ll Learn”In this lesson you’ll learn the definition of a tree and the equivalent ways to characterize one, the vocabulary of rooted trees, why a tree on vertices always has edges, what a spanning tree is, and how Kruskal’s and Prim’s algorithms find a minimum spanning tree.
The Concept
Section titled “The Concept”A tree is a connected graph with no cycles.
That’s it, but the consequences are unusually rich. Trees are the most useful special case of graph in all of computer science.
Equivalent characterizations
Section titled “Equivalent characterizations”For a graph with vertices, all of the following say the same thing:
- is connected and has no cycles.
- is connected and has exactly edges.
- has no cycles and has exactly edges.
- There is exactly one path between every pair of vertices.
- is connected, and removing any edge disconnects it. (Every edge is a bridge.)
- has no cycles, and adding any edge creates exactly one cycle.
Any one of these implies the rest. Characterization 4 is often the most useful intuition: a tree is a graph where routes are unique. No choices, no redundancy, no alternate paths.
Why edges
Section titled “Why n−1n-1n−1 edges”Start with one vertex and no edges. Every time you add a new vertex to a tree, you must connect it with exactly one edge: zero edges would disconnect it, and two or more would create a cycle.
So the edge count always trails the vertex count by exactly one:
This also means a tree is the minimally connected graph. Remove any edge and it falls apart. Trees have zero redundancy, which is efficient and fragile in equal measure.
A forest
Section titled “A forest”A graph with no cycles that isn’t necessarily connected is a forest. Each component is a tree. A forest with vertices and components has
edges.
Rooted trees
Section titled “Rooted trees”Designate one vertex as the root and the tree acquires a hierarchy. Now everything hangs downward from the root, and you get a full vocabulary:
- Root: the designated top vertex.
- Parent: the vertex directly above a given vertex. The root has none.
- Child: a vertex directly below.
- Sibling: vertices with the same parent.
- Leaf: a vertex with no children.
- Internal vertex: any non-leaf.
- Ancestor / descendant: anything above / below on the path to or from the root.
- Depth (or level): distance from the root. The root has depth 0.
- Height: the greatest depth in the tree.
- Subtree: a vertex plus all its descendants.
A binary tree allows at most 2 children per vertex. An -ary tree allows at most .
A full -ary tree has every internal vertex holding exactly children. A balanced tree keeps all leaves at roughly the same depth, which is what makes search operations fast.
Two useful bounds for a binary tree of height :
- At most vertices at depth .
- At most vertices total.
Flip that around and a balanced binary tree holding vertices has height about . That logarithm is the entire reason balanced trees are the standard data structure for search: a million items sit within about 20 steps of the root.
Spanning trees
Section titled “Spanning trees”A spanning tree of a connected graph is a subgraph that:
- includes every vertex of , and
- is a tree.
You’re throwing away edges until only enough remain to keep everything connected, and no more.
Every connected graph has at least one spanning tree, and most have many. has of them, a result called Cayley’s formula. has spanning trees.
Minimum spanning trees
Section titled “Minimum spanning trees”In a weighted connected graph, a minimum spanning tree (MST) is a spanning tree whose total edge weight is as small as possible.
Two classic greedy algorithms find one, and remarkably, greedy works here. That’s unusual, and it’s why these algorithms are so short.
Kruskal’s algorithm (sort edges globally):
- Sort all edges by weight, ascending.
- Go through them in order. Add an edge if it does not create a cycle with edges already chosen; otherwise skip it.
- Stop after edges.
Prim’s algorithm (grow one tree):
- Start at any vertex.
- Repeatedly add the cheapest edge connecting a vertex already in the tree to a vertex not yet in it.
- Stop after edges.
Both always produce a minimum spanning tree. If the edge weights are all distinct, the MST is unique and both algorithms find the same one. With ties, there can be several MSTs with equal total weight, and the algorithms may land on different ones.
Kruskal thinks globally and can build several fragments that merge later. Prim keeps one connected blob that grows outward. Kruskal is usually easier by hand; Prim is often faster on dense graphs.
Running Kruskal on the graph from Example 5 below keeps four edges and skips three:
Worked Examples
Section titled “Worked Examples”Example 1: Is it a tree?
A connected graph has 10 vertices and 12 edges. Is it a tree?
Solution. A tree on 10 vertices has exactly 9 edges. This has 12, so no.
The 3 extra edges mean the graph contains cycles. In general, a connected graph with vertices and edges has independent cycles, so this one has 3.
Example 2: Complete the count.
A tree has 15 vertices. How many edges? If 9 of them are leaves, how many internal vertices?
Solution.
Internal vertices: .
Example 3: Rooted tree vocabulary.
A rooted tree: root has children and . has children and . has child . has children and .
Identify the leaves, the height, the depth of , and the descendants of .
Solution.
- Leaves (no children): , , , .
- Depths: is 0. are 1. are 2. are 3.
- Height: 3, the maximum depth.
- Depth of : 3.
- Descendants of : , , .
Total vertices: 7. Edges: 6, as required.
Example 4: Binary tree height.
What is the minimum possible height of a binary tree with 100 vertices?
Solution. A binary tree of height holds at most vertices.
- : at most . Not enough.
- : at most . Enough.
Minimum height 6. A balanced tree achieves it; a badly built one could be as tall as 99.
Example 5: Kruskal’s algorithm.
Find the MST of this weighted graph.
Vertices: . Edges:
Solution. Sort ascending: , , , , , , .
Process in order:
- - add. Tree:
- - add, no cycle. Tree:
- - skip. and are already connected via , so this would close a cycle.
- - add. Tree:
- - skip. and already connected.
- - add. Tree:
We now have 4 edges for 5 vertices, so we stop.
Example 6: Prim’s algorithm on the same graph.
Solution. Start at .
- In tree: . Cheapest edge leaving: vs . Take . Tree:
- Edges leaving : , , . Cheapest is . Tree:
- Edges leaving: , , . Cheapest is . Tree:
- Edges leaving: , . Cheapest is . Tree: all 5 vertices.
Same tree, same weight, arrived at in a different order. That’s the expected outcome when weights are distinct.
Example 7: Counting spanning trees.
How many spanning trees does have?
Solution. By Cayley’s formula, :
Real-World Applications
Section titled “Real-World Applications”File systems are rooted trees. Directories are internal vertices, files are leaves, and the unique-path property is why every file has exactly one absolute path. The .. reference is just “go to my parent.”
The DOM in every web page is a rooted tree, which is why CSS selectors can talk about children, descendants, and siblings. XML and JSON parse into trees for the same reason.
Balanced search trees are the workhorse of databases. B-trees and B+ trees index nearly every relational database on earth, and their whole value proposition is the height bound: finding one row among a billion takes a handful of disk reads instead of a billion.
Minimum spanning trees solve real infrastructure problems, and the algorithms were invented for exactly that. Laying fiber to connect a set of towns with the least cable, wiring a building’s electrical network, designing pipeline layouts, and planning circuit board traces are all MST problems. Network routing protocols use spanning trees to eliminate loops, which is literally what the Spanning Tree Protocol on Ethernet switches does.
Decision trees drive machine learning classifiers and medical triage protocols. Huffman coding, the basis of much file compression, builds a binary tree where frequently used symbols sit near the root and get short codes.
Version control history is a tree (a directed acyclic graph once you allow merges), with branches as literal branches. Organizational charts, taxonomies, family trees, and tournament brackets are all trees, and they all inherit the unique-path property.
Retrying will remove your ✅ checkmark until you pass again.