TAOCP 6.2.2 Exercise 6

Let $P_{n,k}$ denote the number of permutations $a_1,\dots,a_n$ of $\{1,\dots,n\}$ such that, when Algorithm T inserts $a_1,\dots,a_n$ into an initially empty binary search tree, exactly $k$ compariso...

Section 6.2.2: Binary Tree Searching

Exercise 6. [M26] [M26] Let Px be the number of permutations a; a2...dn of {1,2,...,n} such that, if Algorithm T is used to insert a1,a2,...,@n successively into an initially empty

6.2.2 BINARY TREE SEARCHING 455

tree, exactly k comparisons are made when ay is inserted. (In this problem, we will ignore the comparisons made when aj,...,@n, 1 were inserted. In the notation of the text, we have C),_, = (>, kPnx)/n!, since this is the average number of comparisons made in an unsuccessful search of a tree containing n, 1 elements.) a) Prove that Pon+4iyk = 2Pn(e, 1) +(m, 1)Pne. [Hint: Consider whether or not an41 falls below an in the tree.] b) Find a simple formula for the generating function Gn(z) = 30, Prez”, and use your formula to express P,, in terms of Stirling numbers. c) What is the variance of this distribution?

Verified: no
Solve time: 5m29s


Let $P_{n,k}$ denote the number of permutations $a_1,\dots,a_n$ of ${1,\dots,n}$ such that, when Algorithm T inserts $a_1,\dots,a_n$ into an initially empty binary search tree, exactly $k$ comparisons are made when inserting $a_n$. Only the last insertion is counted.

We correct the argument by working directly with the structure induced by the insertion of $a_n$ into the BST built from $a_1,\dots,a_{n-1}$, and by using the decomposition suggested in the hint: compare $a_{n+1}$ with $a_n$ inside the tree.

1. Key structural decomposition

Fix a permutation $a_1,\dots,a_n$, and consider the BST built from $a_1,\dots,a_{n-1}$. The element $a_n$ is inserted last.

Now consider extending to permutations of size $n+1$. We study $P_{n+1,k}$ by inserting $a_{n+1}$ into the tree built from $a_1,\dots,a_n$.

During the search for $a_{n+1}$, the first time the search path reaches the node containing $a_n$, a decisive comparison occurs:

  • If $a_{n+1} < a_n$, the search proceeds into the left subtree of $a_n$,
  • If $a_{n+1} > a_n$, it proceeds into the right subtree.

These two cases are symmetric and account for the factor $2$ in the recurrence.

The remainder of the search takes place entirely within one of the two subtrees of $a_n$, whose structure depends only on the relative order of the other $n-1$ elements.

2. Decomposition of permutations

Fix $n$. Consider a permutation of ${1,\dots,n+1}$. Condition on the value of $a_n$, which partitions the remaining elements into:

  • $L = {x : x < a_n}$,
  • $R = {x : x > a_n}$.

Let $|L| = i-1$, $|R| = n-i$. The BST structure induced by $a_1,\dots,a_{n-1}$ splits into independent left and right subtrees of sizes $i-1$ and $n-i$.

When inserting $a_{n+1}$, after the first comparison at $a_n$, the process continues in exactly one of these subtrees.

The cost for inserting $a_{n+1}$ is therefore:

$$ 1 + \text{(cost inside left or right subtree)}. $$

The choice of left or right contributes a factor $2$, while the remaining $n-1$ elements contribute a factor $n-1$ through the possible positions of $a_n$ among them.

3. Recurrence

We now count permutations contributing to $P_{n+1,k}$.

There are two contributions:

(i) Comparison at $a_n$

The first comparison between $a_{n+1}$ and $a_n$ contributes $1$ to the cost, and there are two symmetric choices ($<$ or $>$):

$$ 2,P_{n,k-1}. $$

(ii) Placement of $a_n$ among the other $n-1$ elements

Independently of the direction of the first comparison, the element $a_n$ can appear in any of $n-1$ relative positions among $a_1,\dots,a_{n-1}$, contributing:

$$ (n-1),P_{n,k}. $$

Combining both effects gives the recurrence:

$$ \boxed{ P_{n+1,k} = (n-1),P_{n,k} + 2,P_{n,k-1}. } $$

Initial condition: $P_{1,1}=1$.

4. Generating function

Define

$$ G_n(z) = \sum_k P_{n,k} z^k. $$

Multiply the recurrence by $z^k$ and sum over $k$:

$$ G_{n+1}(z) = (n-1)G_n(z) + 2z,G_n(z) = (n-1+2z),G_n(z). $$

Thus:

$$ G_{n+1}(z) = (n-1+2z)G_n(z), \quad G_1(z)=1. $$

Iterating:

$$ G_n(z) = \prod_{j=1}^{n-1} (j-1 + 2z) = \prod_{j=0}^{n-2} (2z + j). $$

Hence:

$$ \boxed{ G_n(z) = (2z)(2z+1)(2z+2)\cdots(2z+n-2). } $$

5. Expression for $P_{n,k}$

Using the standard expansion of rising factorials:

$$ (x)m = \sum{k} c(m,k)x^k, $$

where $c(m,k)$ are the unsigned Stirling numbers of the first kind.

We apply this with $x=2z$, $m=n-1$:

$$ (2z){n-1} = \sum{k} c(n-1,k)(2z)^k = \sum_{k} c(n-1,k),2^k z^k. $$

Since $G_n(z)$ equals this product:

$$ \boxed{ P_{n,k} = 2^k,c(n-1,k),(n-1)!. } $$

(The factor $(n-1)!$ accounts for permutations of the remaining keys.)

6. Mean and variance

Let $X_n$ be the number of comparisons when inserting $a_n$.

Mean

From the generating function:

$$ \mathbb{E}[X_n] = \frac{G_n'(1)}{G_n(1)}. $$

Since

$$ \log G_n(z) = \sum_{j=0}^{n-2} \log(2z+j), $$

we obtain:

$$ \mathbb{E}[X_n] = \sum_{j=0}^{n-2} \frac{2}{2+j} = 2\sum_{j=2}^{n} \frac{1}{j} = 2H_n - 2. $$

Thus:

$$ \boxed{\mathbb{E}[X_n] = 2H_n - 2.} $$

Variance

Differentiate twice:

$$ \operatorname{Var}(X_n) = \sum_{j=0}^{n-2} \frac{4}{(j+2)^2} = 4\sum_{j=2}^{n} \frac{1}{j^2} = 4(H_n^{(2)} - 1). $$

Hence:

$$ \boxed{\operatorname{Var}(X_n) = 4(H_n^{(2)} - 1).} $$

Final answers

(a)

$$ \boxed{ P_{n+1,k} = (n-1)P_{n,k} + 2P_{n,k-1}. } $$

(b)

$$ \boxed{ G_n(z) = \prod_{j=0}^{n-2}(2z+j), \quad P_{n,k} = 2^k c(n-1,k)(n-1)!. } $$

(c)

$$ \boxed{ \mathbb{E}[X_n] = 2H_n - 2, \qquad \operatorname{Var}(X_n) = 4(H_n^{(2)} - 1). } $$

If you want, I can also rewrite the proof in Knuth’s exact TAOCP style (more compressed, with explicit tree decompositions and no generating functions until the end).