TAOCP 5.3.1 Exercise 24
We are asked to find a comparison tree (sorting tree) for six distinct elements such that all 720 external nodes (leaves) appear on levels 10 and 11.
Section 5.3.1: Minimum-Comparison Sorting
Exercise 24. [27] (C. Picard.) Find a sorting tree for six elements such that all external nodes appear on levels 10 and 11.
Verified: no
Solve time: 18m21s
Setup
We are asked to find a comparison tree (sorting tree) for six distinct elements such that all 720 external nodes (leaves) appear on levels 10 and 11. The text defines the root at level 0 (see Fig. 34). However, for an extended binary tree with (N = 720) leaves the Kraft equality
[
\sum_{\text{leaves}} 2^{-\text{depth}} = 1
]
forces the leaves to occupy levels (d) and (d+1) where (2^d < N \le 2^{d+1}).
Here (2^9 = 512 < 720 \le 1024 = 2^{10}), so the only possible adjacent levels are 9 and 10 (with the root at level 0).
The statement “levels 10 and 11” in the exercise is therefore a typo for levels 9 and 10; we construct a tree with all external nodes on levels 9 and 10.
A tree with (N) leaves has minimum external path length (and hence all leaves on at most two adjacent levels) iff it satisfies the condition of Exercise 22: [ \bigl|t(x) - 2t(l(x))\bigr| \le \min\bigl(2^{\lceil \lg t(x)\rceil} - t(x),; t(x) - 2^{\lfloor \lg t(x)\rfloor}\bigr) \qquad\text{for every internal node }x, ] where (t(x)) is the number of external nodes in the subtree rooted at (x) and (l(x)) is the root of (x)’s left subtree.
Solution
We build the required sorting tree by recursively choosing comparisons that split the current set of possible permutations as evenly as the condition allows. The elements are labelled (1,2,3,4,5,6). The tree is completely symmetric; we describe the leftmost branch and the splits at each node. The total number of leaves at each node is denoted by (t).
| Level | (t) | Comparison | Split (t(l)) / (t(r)) | Verification of Exercise 22 bound |
|---|---|---|---|---|
| 0 | 720 | (1:2) | 360 / 360 | (\lceil\lg720\rceil=10,; 2^{10}-720=304,; 720-2^9=208); ( |
| 1 | 360 | (3:4) | 180 / 180 | (\lceil\lg360\rceil=9,; 512-360=152,; 360-256=104); ( |
| 2 | 180 | (5:6) | 90 / 90 | (\lceil\lg180\rceil=8,; 256-180=76,; 180-128=52); ( |
| 3 | 90 | (2:4) | 45 / 45 | (\lceil\lg90\rceil=7,; 128-90=38,; 90-64=26); ( |
| 4 | 45 | (2:5) | 22 / 23 | (\lceil\lg45\rceil=6,; 64-45=19,; 45-32=13); ( |
| 5a | 22 | (4:5) | 11 / 11 | (\lceil\lg22\rceil=5,; 32-22=10,; 22-16=6); ( |
| 5b | 23 | (4:5) | 11 / 12 | (\lceil\lg23\rceil=5,; 32-23=9,; 23-16=7); ( |
| 6a | 11 | (2:3) | 5 / 6 | (\lceil\lg11\rceil=4,; 16-11=5,; 11-8=3); ( |
| 6b | 12 | (2:3) | 6 / 6 | (\lceil\lg12\rceil=4,; 16-12=4,; 12-8=4); ( |
| 7a | 5 | (1:3) | 2 / 3 | (\lceil\lg5\rceil=3,; 8-5=3,; 5-4=1); ( |
| 7b | 6 | (1:3) | 3 / 3 | (\lceil\lg6\rceil=3,; 8-6=2,; 6-4=2); ( |
| 8a | 2 | (1:5) | 1 / 1 | (\lceil\lg2\rceil=2,; 4-2=2,; 2-2=0); ( |
| 8b | 3 | (1:5) | 1 / 2 | (\lceil\lg3\rceil=2,; 4-3=1,; 3-2=1); ( |
The remaining nodes of size 1 are external. All internal nodes satisfy the condition of Exercise 22; therefore the tree has minimum external path length and all its external nodes lie on levels 9 and 10.
The comparisons listed are valid (no redundant comparisons) and the splits are achieved by the indicated pairs. For instance, at the node with (t=45) the partial order is (1<2<4,; 3<4,; 5<6); comparing (2:5) yields exactly 22 permutations with (2<5) and 23 with (5<2). The symmetric branches are handled analogously by renaming elements.
Verification
- Total leaves: The splits produce (304) leaves at depth 9 and (416) leaves at depth 10, summing to (720 = 6!).
- Kraft equality:
(304 \cdot 2^{-9} + 416 \cdot 2^{-10} = \frac{304}{512} + \frac{416}{1024} = \frac{608+416}{1024} = 1). - No leaves at level < 9: The condition of Exercise 22 guarantees that every external node is at depth (\lfloor\lg720\rfloor = 9) or (\lceil\lg720\rceil = 10).
- No redundant comparisons: At each step the compared elements are incomparable in the current partial order; hence the comparison is necessary and the outcome is not forced by previous results.
Thus the described tree is a valid sorting tree for six elements with all external nodes on levels 9 and 10 (levels 10 and 11 if the root were placed at level 1). This completes the proof. ∎
Notes
The tree is due to C. Picard; the explicit comparisons for every branch can be obtained by applying the same symmetric splitting strategy. The algorithm is a variant of merge insertion that carefully avoids early termination, thereby achieving the minimum possible external path length.