taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 2.3-21

Exercise 2.3-21

not stated yet in Lean solution written

RatingM22
MarkersM (mathematical)
KindK3, find a value: compute a number, a set or a closed form from the written solution
SectionSection 2.3 of Volume 1

The task

A tree has njn_j nodes of degree jj for j=1,…,mj = 1, \ldots, m. Find the number of terminal nodes.

The exact wording is in your copy of the book.

Hints

Open one hint at a time. Try the exercise again after each.

Hint 1

Count the nodes of the tree in two ways: as the sum of the njn_j, and as one root plus the children of all nodes.

Solution

Show the solution

Let n0n_0 be the number of terminal nodes, which have degree 0. The tree has n0+n1+⋯+nmn_0 + n_1 + \cdots + n_m nodes.

Each node except the root is the child of exactly one node. A node of degree jj has jj children. So the number of nodes except the root is 0⋅n0+1⋅n1+2⋅n2+⋯+m⋅nm0 \cdot n_0 + 1 \cdot n_1 + 2 \cdot n_2 + \cdots + m \cdot n_m. Hence

n0+n1+⋯+nm=1+n1+2n2+⋯+mnm.n_0 + n_1 + \cdots + n_m = 1 + n_1 + 2 n_2 + \cdots + m n_m.

Subtract n1+⋯+nmn_1 + \cdots + n_m from both sides:

n0=1+n2+2n3+⋯+(m−1) nm=1+∑j≥2(j−1) nj.n_0 = 1 + n_2 + 2 n_3 + \cdots + (m-1)\, n_m = 1 + \sum_{j \ge 2} (j - 1)\, n_j.

Answer: n0=1+∑j=2m(j−1) njn_0 = 1 + \sum_{j=2}^{m} (j-1)\, n_j. The nodes of degree 1 do not appear.

Check

Python3 made 500 random trees with up to 30 nodes and compared the number of nodes of degree 0 with 1+∑j≥2(j−1)nj1 + \sum_{j \ge 2}(j-1) n_j. All 500 agreed. For the tree of Fig. 19 the degrees are AA 2, BB 2, CC 3, EE 1 and the five other nodes 0. So n1=1n_1 = 1, n2=2n_2 = 2, n3=1n_3 = 1, and n0=1+1⋅2+2⋅1=5n_0 = 1 + 1 \cdot 2 + 2 \cdot 1 = 5, which agrees with the five terminal nodes H,J,D,G,FH, J, D, G, F.

Remarks

For a 0-2-tree this gives n0=1+n2n_0 = 1 + n_2, so the number of leaves is one more than the number of nodes with two children.

This solution is drafted, and a person has not reviewed it yet. The source.

In Lean

not stated yet The exercise is not stated in Lean yet.

The status is computed from the Lean build. The node of the map.

Something is wrong? Report it.