Discrete Mathematics · Unit 2 · Topic 09

Trees: connected, with no cycles.

Short answer

A tree is a connected graph with no cycles. A tree with \(n\) vertices has exactly \(n - 1\) edges and exactly one path between any two vertices.

A minimum spanning tree joins all vertices of a weighted graph at the least total cost; Kruskal's and Prim's algorithms find one. Example: a tree with 15 vertices has 14 edges.

EngineeringComputer Science / ITBSc
01

What a tree is

Before you start: you need the words from graph theory basics: vertex, edge, degree, path, cycle, connected.

A tree is a connected graph with no cycles. A graph with no cycles that may be in several pieces is a forest; each piece is a tree. Family trees, folder structures and knockout tournaments are all trees.

Properties of trees

For a graph \(T\) with \(n\) vertices, these all say the same thing:

(1) \(T\) is connected and has no cycles. (2) There is exactly one path between any two vertices. (3) \(T\) is connected and has \(n - 1\) edges. (4) \(T\) has no cycles and has \(n - 1\) edges.

Forests

A forest with \(n\) vertices and \(c\) components has \(n - c\) edges.

Why \(n - 1\) edges? Every tree with at least 2 vertices has a leaf, a vertex of degree 1: walk along a longest path, and its last vertex can't have another neighbour (that would give a longer path or a cycle). Remove the leaf and its edge. What is left is still a tree, with one vertex and one edge fewer. Keep going until one vertex and 0 edges are left. Vertices and edges dropped together every time, so the tree had one more vertex than edges.

Two more facts follow. Removing any edge from a tree disconnects it, and adding any edge creates exactly one cycle.

02

Rooted trees and binary trees

Pick one vertex as the root and hang the tree from it. Now every other vertex has a parent (the next vertex on the path to the root), its children are the vertices below it, and vertices with no children are leaves. The others are internal vertices. The level of a vertex is its distance from the root, and the height of the tree is the largest level.

In an \(m\)-ary tree every vertex has at most \(m\) children; \(m = 2\) gives a binary tree, where each child is either a left child or a right child. A full \(m\)-ary tree is one where every internal vertex has exactly \(m\) children.

Counting formulas

A full \(m\)-ary tree with \(i\) internal vertices has \(n = mi + 1\) vertices and \(l = (m - 1)i + 1\) leaves.

A binary tree of height \(h\) has at most \(2^h\) leaves and at most \(2^{h+1} - 1\) vertices.

The first formula holds because every vertex except the root is a child of exactly one internal vertex, and each internal vertex has \(m\) children: \(n - 1 = mi\).

03

Spanning trees and minimum spanning trees

A spanning tree of a connected graph \(G\) is a tree that uses every vertex of \(G\) and some of its edges. Every connected graph has one: keep removing an edge from some cycle until no cycles are left. A graph with \(n\) vertices has spanning trees with exactly \(n - 1\) edges. The complete graph \(K_n\) has \(n^{n-2}\) of them (Cayley's formula).

If the edges have weights (costs, distances), a minimum spanning tree (MST) is a spanning tree with the smallest total weight. Two greedy algorithms find one.

  1. Kruskal's algorithm. Sort all edges by weight. Go through them from lightest to heaviest, adding an edge unless it makes a cycle with the edges already chosen. Stop at \(n - 1\) edges.
  2. Prim's algorithm. Start from any vertex. At each step, add the lightest edge that joins a vertex already in the tree to a vertex not yet in it. Stop when all vertices are in.

Kruskal grows a forest that joins up at the end; Prim grows one tree outwards. When all the weights are different, the MST is unique, so both algorithms give the same tree.

Weighted graph W on vertices A to F with its minimum spanning tree in orange 352678491ABCDEF
Weighted graph \(W\): 6 vertices, 9 edges. The orange edges form the minimum spanning tree found by both Kruskal's and Prim's algorithms, with total weight \(1 + 2 + 3 + 4 + 6 = 16\).
04

Tree traversals

A traversal visits every vertex of a rooted binary tree once, in a fixed order. The three standard ones differ only in when the root is visited.

TraversalOrderOn tree \(T\) below
Preorderroot, left subtree, right subtreeA B D E G C F H
Inorderleft subtree, root, right subtreeD B G E A C H F
Postorderleft subtree, right subtree, rootD G E B H F C A

Each rule applies again inside every subtree. For an expression tree, where leaves are numbers or variables and internal vertices are operators, preorder gives prefix (Polish) notation, inorder gives the usual infix form (with brackets added), and postorder gives postfix notation.

Binary tree T with root A, children B and C, and leaves D, G and H ABCDEFGH
Binary tree \(T\) with root \(A\). \(C\) has only a right child \(F\); \(E\) and \(F\) have only a left child. Left and right matter: swapping them changes the inorder sequence.
05

Solved examples

Example 1: Counting edges

Easy

How many edges does a tree with 15 vertices have? How many edges does a forest with 20 vertices and 3 components have?

  1. A tree with \(n\) vertices has \(n - 1\) edges: \(15 - 1 = 14\).
  2. Each of the 3 trees in the forest has one edge fewer than its vertices, so the forest has \(20 - 3 = 17\) edges.

Answer14 edges; 17 edges

Example 2: Counting leaves from degrees

Medium

A tree has 2 vertices of degree 4, 1 vertex of degree 3 and 2 vertices of degree 2. All the other vertices are leaves. How many leaves are there?

  1. Let there be \(L\) leaves. Then \(n = 5 + L\) and the tree has \(n - 1 = 4 + L\) edges.
  2. Sum of degrees: \(2(4) + 3 + 2(2) + L = 15 + L\).
  3. Handshaking: \(15 + L = 2(4 + L) = 8 + 2L\), so \(L = 7\).

Answer7 leaves (12 vertices, 11 edges)

Example 3: A knockout tournament

Easy

32 players enter a knockout tennis tournament. How many matches are played?

  1. Draw the tournament as a full binary tree: each match is an internal vertex with two children (the two players or earlier matches), and the 32 players are the leaves.
  2. For a full binary tree, \(l = (2 - 1)i + 1 = i + 1\). So \(32 = i + 1\) and \(i = 31\).
  3. A quicker way to see it: every match knocks out exactly one player, and 31 players must be knocked out.

Answer31 matches

Example 4: Spanning trees of \(K_4\)

Medium

How many spanning trees does \(K_4\) have?

  1. Cayley's formula: \(4^{4-2} = 16\).
  2. Check by counting: a spanning tree of \(K_4\) has 3 of its 6 edges. There are \(\binom{6}{3} = 20\) ways to choose 3 edges.
  3. Three edges on 4 vertices fail to be a tree only if they form a triangle. \(K_4\) has 4 triangles, so \(20 - 4 = 16\).

Answer16 spanning trees

Example 5: Kruskal's algorithm

Exam level

Use Kruskal's algorithm to find a minimum spanning tree of the weighted graph \(W\) in the diagram.

  1. Edges in order of weight: \(EF\) 1, \(BC\) 2, \(AB\) 3, \(DE\) 4, \(AC\) 5, \(BD\) 6, \(CD\) 7, \(CE\) 8, \(DF\) 9.
  2. Add \(EF\) (1), \(BC\) (2), \(AB\) (3), \(DE\) (4). No cycles so far.
  3. \(AC\) (5): \(A\) and \(C\) are already joined through \(B\). Adding it would make the cycle \(A\text{-}B\text{-}C\text{-}A\). Reject.
  4. \(BD\) (6): joins \(\{A, B, C\}\) to \(\{D, E, F\}\). Add it. Now there are 5 edges for 6 vertices, so stop.
  5. Total weight: \(1 + 2 + 3 + 4 + 6 = 16\).

AnswerMST edges \(EF, BC, AB, DE, BD\); total weight 16

Example 6: Prim's algorithm

Exam level

Use Prim's algorithm, starting from \(A\), on the same graph \(W\).

  1. Tree \(\{A\}\). Edges leaving it: \(AB\) 3, \(AC\) 5. Add \(AB\).
  2. Tree \(\{A, B\}\). Leaving: \(AC\) 5, \(BC\) 2, \(BD\) 6. Add \(BC\).
  3. Tree \(\{A, B, C\}\). Leaving: \(BD\) 6, \(CD\) 7, \(CE\) 8. (\(AC\) now joins two tree vertices, so it is out.) Add \(BD\).
  4. Tree \(\{A, B, C, D\}\). Leaving: \(CE\) 8, \(DE\) 4, \(DF\) 9. Add \(DE\).
  5. Tree \(\{A, B, C, D, E\}\). Leaving: \(DF\) 9, \(EF\) 1. Add \(EF\). All 6 vertices are in.

AnswerSame tree as Kruskal: \(AB, BC, BD, DE, EF\), total weight 16

Example 7: Three traversals

Medium

Write the preorder, inorder and postorder traversals of the binary tree \(T\) in the diagram.

  1. Preorder (root first): \(A\), then the left subtree of \(A\) in preorder: \(B, D, E, G\), then the right subtree: \(C, F, H\).
  2. Inorder (root in the middle): left subtree of \(A\) gives \(D, B, G, E\) (note \(G\) is the left child of \(E\)); then \(A\); then the right subtree gives \(C, H, F\), since \(C\) has no left child.
  3. Postorder (root last): \(D, G, E, B\), then \(H, F, C\), then \(A\).

AnswerPre: A B D E G C F H. In: D B G E A C H F. Post: D G E B H F C A.

Example 8: Rebuilding a tree

Exam level

A binary tree has preorder P Q S T R U and inorder S Q T P U R. Draw the tree and write its postorder.

  1. Preorder starts with the root, so the root is \(P\).
  2. In the inorder, everything left of \(P\) (S Q T) is the left subtree and everything right of \(P\) (U R) is the right subtree.
  3. Left subtree: its preorder is Q S T, so its root is \(Q\); the inorder S Q T puts \(S\) on the left of \(Q\) and \(T\) on the right.
  4. Right subtree: its preorder is R U, so its root is \(R\); the inorder U R puts \(U\) on the left of \(R\).
  5. Postorder: left subtree (S T Q), right subtree (U R), root (P).

AnswerRoot \(P\) with children \(Q\) (children \(S\), \(T\)) and \(R\) (left child \(U\)); postorder S T Q U R P.

Vipul Sir's tip

In Kruskal questions, write the sorted edge list down the margin and tick or cross each edge as you go, with a word like "cycle" next to each cross. Examiners give marks for showing why an edge was rejected. And always state the total weight at the end.

06

Common mistakes

1. Thinking \(n - 1\) edges alone makes a tree

A triangle plus one isolated vertex has 4 vertices and 3 edges, but it is not a tree: it has a cycle and is not connected. You need \(n - 1\) edges and connected (or no cycles).

2. Running Prim like Kruskal

In Prim's algorithm you may only add edges that touch the tree you have so far. In graph \(W\) starting from \(A\), the lightest edge overall is \(EF\) (1), but it is not available at the first step; the first edge is \(AB\) (3).

3. Putting the root first in inorder

Inorder is left, root, right. For tree \(T\) it starts with \(D\), the leftmost vertex, not with \(A\). Starting with the root is preorder.

07

Practice questions

Try each one on paper before you open the answer.

Q1A tree has 25 edges. How many vertices does it have?

\(n - 1 = 25\), so \(n = 26\).

Q2Can a tree with 6 vertices have degrees \(1, 1, 1, 1, 2, 2\)?

No. A tree with 6 vertices has 5 edges, so its degrees must add up to 10. These add up to 8. (Such degrees belong to a forest with 2 components.)

Q3A full binary tree has 20 internal vertices. How many leaves and how many vertices does it have?

\(l = (2 - 1)(20) + 1 = 21\) leaves and \(n = 2(20) + 1 = 41\) vertices.

Q4What is the largest number of vertices in a binary tree of height 4?

\(2^{4+1} - 1 = 31\): levels 0 to 4 hold \(1 + 2 + 4 + 8 + 16\) vertices.

Q5Draw the expression tree for \((a - b) / (c + d * e)\) and write its prefix and postfix forms.

Root \(/\); left subtree \(-\) with leaves \(a, b\); right subtree \(+\) with left leaf \(c\) and right child \(*\) (leaves \(d, e\)).

Prefix (preorder): / − a b + c * d e. Postfix (postorder): a b − c d e * + /.

Q6Find a minimum spanning tree of the graph on \(A, B, C, D\) with weights \(AB = 3,\ BC = 4,\ CD = 2,\ DA = 6,\ AC = 1,\ BD = 5\).

Kruskal: \(AC\) (1), \(CD\) (2), \(AB\) (3). That is 3 edges for 4 vertices, so stop. Total weight \(1 + 2 + 3 = 6\).

Stuck on a question?

Bring it to a free demo class and work through it with Vipul Sir. Book on WhatsApp.

08

Frequently asked questions

What is a tree in graph theory?

A connected graph with no cycles. Equivalently, a graph with exactly one path between any two vertices. A tree with \(n\) vertices always has \(n - 1\) edges.

What is the difference between Kruskal's and Prim's algorithm?

Kruskal sorts all the edges and adds the lightest one that doesn't make a cycle, anywhere in the graph. Prim grows a single tree from a starting vertex, always adding the lightest edge leaving the tree. Both give a minimum spanning tree.

What is a spanning tree?

A tree inside a connected graph that includes every vertex. For a graph with \(n\) vertices it has \(n - 1\) edges, and a graph usually has many different spanning trees.

What is inorder traversal?

For a binary tree: traverse the left subtree, visit the root, then traverse the right subtree, applying the same rule inside each subtree. On a binary search tree it lists the keys in increasing order.

Related lessons

← All 10 Discrete Mathematics topics
Vipul Sir
Written by Vipul Sir

Vipul Sir has taught mathematics for over 15 years to HSC, CBSE, ICSE, IGCSE/IB, Diploma, Engineering and BSc students at VVS Classes in Goregaon and Vile Parle, Mumbai. He teaches every class himself, concepts first and exam technique second.

Discrete Mathematics

Get Discrete Mathematics right, with Vipul Sir beside you.

Concept-first classes in small batches in Goregaon and Vile Parle, or online. Your first class is free.

WhatsApp↗