Exercise · Volume 1 · 2.3-15
Exercise 2.3-15
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Markers | recommended |
| Kind | K1, prove: show that a stated fact is true from the written solution |
| Section | Section 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 . If a node has the name , name its left child and its right child , when the child exists. The names are strings over that start with .
Example. In the binary tree (1) of Section 2.3.1, is the right child of , so . Then , and , the left child of , is . The node is . The node is the left child of , so , and , the right child of , is .
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 has symbols. Suppose two nodes have the same name. Then both are at the same level. Both are the root if the name is . Otherwise the names are for the same and . By induction on the length, the nodes named are equal, so the two nodes are the child on the side 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 . The root is , and the children of are and . 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 in tree (1) is , which is the integer 14. Its parent is , which is 7, and .
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.