Connected acyclic graphs. Root, leaves, parent-child relationships.
Self-serve tutorial - low prerequisites, straightforward concepts.
Trees are the “just enough edges” graphs: they connect everything without ever looping back. That simple idea powers file systems, organization charts, search structures, network design, and many algorithms.
A tree is a connected, acyclic (cycle-free) undirected graph. This is equivalent to saying there is a unique simple path between any two vertices, and also equivalent to having exactly |E| = |V| − 1 edges. If you pick a root, the tree becomes a rooted tree with parent/child relationships; leaves are nodes with no children.
In graphs, you often face a tension:
A tree hits a sweet spot: it’s connected, but it has no cycles. That means it has exactly the minimum number of edges needed to stay connected.
A tree is an undirected graph such that:
1) Connected: for any vertices , there exists a path between them.
2) Acyclic: there is no cycle (no closed loop of distinct vertices/edges).
You can think of a tree as a structure where edges form “branches” that never loop back.
Imagine building a network by adding edges one at a time:
A tree is exactly the state where:
For a finite undirected graph , the following are equivalent:
1) is a tree (connected + acyclic)
2) There is a unique simple path between any two vertices
3) is connected and has exactly $$
These equivalences are not trivia—they’re the fastest way to recognize and reason about trees.
Use toggles/buttons:
And live indicators:
This setup reinforces the key equivalences:
This section is the backbone. Trees are simple because three different “views” all describe the same object.
Assume is connected and acyclic.
Take any two vertices and .
Then those two paths must diverge at some point and rejoin later, forming a loop—i.e., a cycle. That contradicts acyclic.
So in a connected acyclic graph, the path between any two vertices is unique.
Key intuition:
Now assume between any two vertices there is a unique simple path.
Contradiction. So there can be no cycles.
This is the “edge counting” signature of trees.
If is a tree with vertices, then it has exactly edges.
Start from a single vertex:
Now imagine building any tree by adding one new vertex at a time.
So each time you add a vertex, you add exactly one edge.
After adding up to vertices:
Let’s start with isolated vertices. Components = .
Each edge that connects two different components reduces components by 1.
To reach 1 component, you must reduce by , requiring at least edges.
A tree is cycle-free, meaning you never waste an edge inside a component. So it uses exactly edges.
These are extremely useful and very testable in an interactive canvas.
Let be a tree. Pick any two vertices .
So one extra edge over forces a cycle.
Take any edge in a tree.
So every edge is a bridge (a cut edge) in a tree.
For a finite undirected graph, any two of the following imply the third:
This is a powerful “tree detector.”
| What you know | Fast conclusion | ||||
|---|---|---|---|---|---|
| connected + acyclic | it’s a tree; unique paths; $ | E | = | V | -1$ |
| connected + $ | E | = | V | -1$ | must be acyclic → tree |
| acyclic + $ | E | = | V | -1$ | must be connected → tree |
(That last row surprises people: if you have no cycles and still have edges, you can’t afford to be disconnected.)
So far, trees were undirected: edges have no orientation. Many applications (hierarchies, parsing, search) need direction—so we pick a root.
A rooted tree is a tree with one distinguished vertex called the root (often written ).
Once you choose a root, every other vertex has:
That previous vertex is the parent.
For a rooted tree with root :
You can define it formally:
Once parent is defined:
These relationships are not extra edges—they are interpretations of the same undirected structure after choosing a root.
A leaf (in a rooted tree) is a node with no children.
Important subtlety:
These align nicely when the root is not isolated:
With a root, you can measure “how far down” nodes are.
These quantities matter in algorithmic runtime (e.g., searching a deep tree can be costly).
On an interactive canvas:
This makes the idea of “parent(x) is defined by the unique path to root” concrete.
Trees show up whenever you want structure without ambiguity.
Root choice is natural here (CEO, root directory, etc.).
Trees are friendly because:
Typical tasks:
Given a connected graph with cycles, a spanning tree is a subgraph that:
So a spanning tree is what you get when you “remove enough edges to break all cycles but keep connectivity.”
This connects directly to Minimum Spanning Trees (MST): among all spanning trees, choose minimum total weight.
Union-Find helps track connected components as you add edges.
That aligns perfectly with the “tree building” view:
That’s exactly what Kruskal’s MST algorithm uses Union-Find to detect.
When you restrict the shape or labeling of a rooted tree, you get powerful data structures:
These will reuse the parent/child/leaf vocabulary heavily.
Consider the undirected graph with vertices V = {A, B, C, D, E, F} and edges:
E = { (A,B), (B,C), (C,D), (D,E), (E,F) }.
We will verify it is a tree using multiple equivalent characterizations and show how a single edge addition breaks it.
Step 1: Count vertices and edges.
|V| = 6 (A through F)
|E| = 5
Step 2: Check the edge-count signature.
Compute |V| − 1 = 6 − 1 = 5.
We have |E| = 5 = |V| − 1.
This is consistent with being a tree, but not sufficient alone unless we also know connected (or acyclic).
Step 3: Check connectedness.
From A you can reach B, then C, then D, then E, then F.
So every vertex is reachable from A → graph is connected.
Step 4: Conclude it’s a tree.
We have connected + |E| = |V| − 1 ⇒ it must be acyclic.
Therefore the graph is a tree.
Step 5: Demonstrate the “add an edge ⇒ cycle” fact.
Add one new edge (B,E).
Because the original graph is a tree, there is a unique path from B to E:
B → C → D → E.
Adding (B,E) creates a cycle:
B → C → D → E → B.
Now it is no longer a tree.
Insight: The fastest practical test for a finite graph is: if it’s connected and has |E| = |V| − 1, it’s a tree. The interactive takeaway: adding any extra edge to a tree creates exactly one cycle, which you can highlight as “unique path between endpoints + new edge.”
We have this undirected tree (edges shown). We will choose a root and compute parent(x) for all nodes.
Inline diagram:
A
B
/ \
C D
/ \
E F
Edges: (A,B), (B,C), (B,D), (D,E), (D,F).
Choose root r = A.
Step 1: Identify the root.
Root r = A.
By definition, parent(A) is undefined.
Step 2: Find parent of B.
The unique path from A to B is A → B.
So parent(B) = A.
Step 3: Find parent of C.
Unique path A → B → C.
So parent(C) = B.
Step 4: Find parent of D.
Unique path A → B → D.
So parent(D) = B.
Step 5: Find parent of E and F.
Unique path A → B → D → E ⇒ parent(E) = D.
Unique path A → B → D → F ⇒ parent(F) = D.
Step 6: List children of each node.
Children(A) = {B}
Children(B) = {C, D}
Children(D) = {E, F}
Children(C) = ∅, Children(E) = ∅, Children(F) = ∅
Step 7: Identify leaves (rooted definition: nodes with no children).
Leaves are C, E, F.
(They are also degree-1 nodes in the undirected sense.)
Insight: Choosing a root doesn’t change the underlying edges—it changes your viewpoint. parent(x) is not arbitrary: it is forced by the unique path to the root. Leaves become visually obvious when you orient edges away from the root and look for nodes with no outgoing child edges.
Take the rooted tree from Example 2. Remove edge (B,D). What happens to the graph? Which nodes become unreachable from the root A?
Step 1: Recall the structure.
A—B connects to C and D; D connects to E and F.
Step 2: Remove edge (B,D).
This deletes the only connection between the set {D,E,F} and the set {A,B,C}.
Step 3: Use the tree property (unique paths / bridges).
In a tree every edge is a bridge: removing it disconnects the graph into exactly two components.
Step 4: Identify components after removal.
Component 1: {A, B, C}
Component 2: {D, E, F}
Step 5: Reachability from root A.
From A you can reach B and C.
You can no longer reach D, E, or F.
Insight: In general graphs, removing an edge might not matter (there could be alternate routes). In trees, edges are never redundant: remove one edge and you always lose connectivity.
A tree is an undirected graph that is connected and acyclic (has no cycles).
For finite graphs, these are equivalent: connected+acyclic ⇔ unique simple path between any two vertices ⇔ |E| = |V| − 1 (with connectedness).
A tree has the minimum number of edges needed to stay connected: with vertices it has exactly edges.
Adding one edge to a tree creates exactly one cycle; removing any edge from a tree disconnects it.
A rooted tree is a tree with a distinguished root; the root induces parent/child directions without changing the underlying edges.
For , is the neighbor just above on the unique path from the root to ; is undefined.
A leaf in a rooted tree is a node with no children (often matching degree-1 vertices in the undirected view).
Thinking a tree must be drawn like a botanical tree (with a top and bottom). Trees are graphs; layout is arbitrary.
Assuming alone guarantees a tree. You also need connectedness (or acyclicity).
Confusing undirected-degree leaves (degree 1) with rooted leaves (no children) without checking which definition is being used.
Treating parent(x) as an extra label you can choose freely; in a rooted tree, parent(x) is determined by the unique path to the root.
You have an undirected graph with |V| = 8 and |E| = 7. You also know it is acyclic. Must it be a tree? Explain.
Hint: Use the equivalence: acyclic + |E| = |V| − 1 ⇒ connected.
Yes. Since |E| = |V| − 1 (7 = 8 − 1) and the graph is acyclic, it cannot be disconnected. If it were disconnected into k ≥ 2 components, each component being acyclic would be a tree/forest component with at most (|Vᵢ| − 1) edges, giving total edges ≤ (|V| − k) ≤ |V| − 2, contradiction. Therefore it is connected and acyclic, hence a tree.
In a tree with 10 vertices, how many edges are there? If you add 3 new edges (between existing vertices), what is the minimum number of cycles created?
Hint: Trees have |E| = |V| − 1. Also, each added edge to a tree creates exactly one cycle (though cycles can share edges).
A tree with 10 vertices has 9 edges. Adding 3 edges creates at least 3 cycles: each added edge closes a unique cycle with the pre-existing unique path between its endpoints. So the minimum number of cycles created is 3.
Given the rooted tree below, compute parent(x) for all nodes and list the leaves.
R
/ \
A B
/ \
C D
\
E
Edges: (R,A), (R,B), (B,C), (B,D), (D,E). Root is R.
Hint: parent(x) is the previous node on the unique path from R to x. Leaves have no children.
parent(R) undefined.
parent(A)=R.
parent(B)=R.
parent(C)=B.
parent(D)=B.
parent(E)=D.
Children: R→{A,B}, B→{C,D}, D→{E}.
Leaves (no children): A, C, E.
Unlocks and next steps:
Related prerequisite: