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

Exercise 2.3-15

not stated yet in Lean solution written

Rating20
Markersrecommended
KindK1, prove: show that a stated fact is true from the written solution
SectionSection 2.3 of Volume 1

The task

Invent a notation for the nodes of a binary tree that is analogous to the Dewey decimal notation, and show that it names each node once.

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 node has a left child and a right child, so each step down is a choice between two. Use one symbol for each choice.

Hint 2

Prove that two different nodes get different names, by induction on the length of the name.

Solution

Show the solution

Notation. Name the root 11. If a node has the name α\alpha, name its left child α0\alpha0 and its right child α1\alpha1, when the child exists. The names are strings over {0,1}\{0, 1\} that start with 11.

Example. In the binary tree (1) of Section 2.3.1, CC is the right child of AA, so C=11C = 11. Then F=111F = 111, and HH, the left child of FF, is 11101110. The node JJ is 11111111. The node EE is the left child of CC, so E=110E = 110, and GG, the right child of EE, is 11011101.

Claim. Different nodes have different names, and the name of a node tells the path from the root.

Proof. The name of a node of level kk has k+1k + 1 symbols. Suppose two nodes have the same name. Then both are at the same level. Both are the root if the name is 11. Otherwise the names are αb\alpha b for the same α\alpha and bb. By induction on the length, the nodes named α\alpha are equal, so the two nodes are the child on the side bb of this node. There is only one such child, so the nodes are equal. The nodes of the path from the root to a node have the names that are the prefixes of its name, in the order of increasing length.

Remark on numbers. Read a name as a binary number with leading digit 11. The root is 11, and the children of aa are 2a2a and 2a+12a + 1. So the nodes of a binary tree correspond to positive integers, in the way that memory addresses of a complete tree are numbered.

Check

Python3 made 300 random binary trees of up to 15 nodes and gave names by the rule. The names were always distinct. The name of HH in tree (1) is 11101110, which is the integer 14. Its parent FF is 111111, which is 7, and 14=2⋅714 = 2 \cdot 7.

Remarks

Exercise 2.3.1-5 uses this notation to describe the three orders of traversal. Galton used the same naming for pedigrees.

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.