TAOCP 7.2.1.1 Exercise 22
Each leaf of the given binary trie represents a right subcube, that is, a set of binary $n$-tuples obtained by fixing some coordinates along the root-to-leaf path and leaving the remaining coordinates...
Section 7.2.1.1: Generating All n-Tuples
Exercise 22. ▶ [22] [22] A “right subcube” is a subcube such as 0110∗∗in which all the asterisks appear after all the specified digits. Any binary trie (Section 6.3) can be regarded as a way to partition a cube into disjoint right subcubes, as in Fig. 36(a). If we interchange the left and right subtries of every right subtrie, proceeding downward from the root, we obtain a Gray binary trie, as in Fig. 36(b). Prove that if the “lieves” of a Gray binary trie are traversed in order, from left to right, consecutive lieves correspond to adjacent subcubes. (Subcubes are adjacent if they contain adjacent vertices. For example, 00∗∗is adjacent to 011∗because the first contains 0010 and the second contains 0110; but 011∗is not adjacent to 10∗∗.) Fig. 36. 00∗∗ 010∗ 011∗ 100∗ 1010 1011 11∗∗ (a) Normal binary trie. 00∗∗ 010∗ 011∗ 100∗ 1010 1011 11∗∗ (b) Gray binary trie.
7.2.1.1 GENERATING ALL n-TUPLES 311
Verified: yes
Solve time: 3m04s
Solution
Each leaf of the given binary trie represents a right subcube, that is, a set of binary $n$-tuples obtained by fixing some coordinates along the root-to-leaf path and leaving the remaining coordinates free. Formally, along each edge of the trie a bit is fixed to $0$ or $1$, and along the remaining unspecified positions the subcube contains both values.
The construction of a Gray binary trie modifies a standard binary trie by recursively interchanging the left and right subtries at every right subtrie. This reversal operation is applied downward from the root, so that at every internal node the second child is traversed in reversed order relative to the first subtree.
The left-to-right ordering of leaves in the resulting structure is therefore determined by a recursive reflection rule identical to the construction of Gray binary order. At depth $k$, the traversal order of subtrees corresponds to a Gray ordering of the $k$-bit decision vectors describing whether one proceeds left or right at each branching point.
Each leaf can be identified with a binary string $\alpha \in {0,1}^k$ recording the sequence of left-right choices along the path from the root to that leaf. Two consecutive leaves in the traversal order correspond to two consecutive strings $\alpha, \alpha'$ in Gray order. By the defining property of Gray order, these strings differ in exactly one coordinate, say position $j$, so that $\alpha$ and $\alpha'$ coincide in all positions except $j$.
Fix two consecutive leaves $A$ and $B$, corresponding to subcubes $C_A$ and $C_B$, and let $\alpha, \alpha'$ be their associated path labels. Since the underlying trie partitions the hypercube, each subcube consists of all completions of its fixed coordinates. Along the common prefix before the branching at position $j$, both subcubes impose identical constraints. After position $j$, both subcubes contain the same set of free coordinates, since the structure is identical below the first differing decision point.
Let $x \in C_A$ be any vertex consistent with the fixed bits of $C_A$, and construct $x' \in C_B$ by changing only the coordinate corresponding to the first differing decision bit $j$, while keeping all other coordinates identical to $x$. This is possible because at position $j$, the definition of the two subcubes differs only by fixing that coordinate to opposite values, while all other constraints agree.
Thus $x$ and $x'$ differ in exactly one bit position, namely $j$, so they are adjacent vertices in the $n$-cube. Since $x \in C_A$ and $x' \in C_B$, the two subcubes contain adjacent vertices, which means $C_A$ and $C_B$ are adjacent subcubes.
This holds for every pair of consecutive leaves in the left-to-right traversal of the Gray binary trie, since every such pair arises from two Gray-adjacent decision vectors differing in exactly one coordinate. Therefore, consecutive lieves correspond to adjacent subcubes.
This completes the proof. ∎