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-20

Exercise 2.3-20

not stated yet in Lean solution written

RatingM21
MarkersM (mathematical), recommended
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 2.3 of Volume 1

The task

A 0-2-tree is a tree in which each node has 0 or 2 children. Show that each 0-2-tree has an odd number of nodes, and give a one-to-one correspondence between binary trees of nn nodes and 0-2-trees of 2n+12n+1 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

A 0-2-tree is one node, or a root with two 0-2-trees. Use induction on this form for the parity.

Hint 2

Map the empty binary tree to the one-node tree. Map a root with left and right subtrees to a root with the images of the subtrees.

Solution

Show the solution

Odd size. A 0-2-tree has one node, or a root and two 0-2-trees. By induction on the number of nodes, a tree of one node has size 11, which is odd. A tree with a root and two subtrees of sizes s1s_1 and s2s_2 has size 1+s1+s21 + s_1 + s_2. Both s1s_1 and s2s_2 are odd by induction, so their sum is even, and the total is odd.

The map. Define φ\varphi from binary trees to 0-2-trees by recursion. φ(∅)\varphi(\varnothing) is the 0-2-tree with one node. If BB has a root rr, a left subtree LL and a right subtree RR, then φ(B)\varphi(B) is the 0-2-tree with a root and two children subtrees φ(L)\varphi(L) and φ(R)\varphi(R) in this order. The root of φ(B)\varphi(B) corresponds to the root rr.

Size. A binary tree BB of nn nodes goes to a 0-2-tree of 2n+12n + 1 nodes. For n=0n = 0 the size is 11. For n>0n > 0, with ∣L∣=a|L| = a and ∣R∣=b|R| = b, a+b=n−1a + b = n - 1, the size is 1+(2a+1)+(2b+1)=2(a+b+1)+1=2n+11 + (2a + 1) + (2b + 1) = 2(a + b + 1) + 1 = 2n + 1.

One to one. Each 0-2-tree with more than one node has a root and exactly two children subtrees, so it is the image of exactly one binary tree, built from the preimages of the two subtrees. The tree with one node is the image of ∅\varnothing only. So φ\varphi has an inverse: remove the leaves of the 0-2-tree. By induction on the size of the 0-2-tree, φ\varphi is a bijection from the binary trees with nn nodes onto the 0-2-trees with 2n+12n + 1 nodes.

Check

Python3 counted 0-2-trees by the number NN of nodes. The counts for N=1,…,7N = 1, \ldots, 7 are 1,0,1,0,2,0,51, 0, 1, 0, 2, 0, 5, which is zero for even NN. For N=2n+1N = 2n + 1, n=0,…,8n = 0, \ldots, 8, the counts are 1,1,2,5,14,42,132,429,14301, 1, 2, 5, 14, 42, 132, 429, 1430. These are the numbers of binary trees with nn nodes, (2nn)/(n+1)\binom{2n}{n}/(n+1).

Remarks

The interior nodes of a 0-2-tree are the nodes of the binary tree, and the leaves are the empty subtrees. This is the idea of an extended binary tree.

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.