12-3 Average node depth in a randomly built binary search tree
In this problem, we prove that the average depth of a node in a randomly built binary search tree with nodes is . Although this result is weaker than that of Theorem 12.4, the technique we shall use reveals a surprising similarity between the building of a binary search tree and the execution of from Section 7.3.
We define the total path length of a binary tree as the sum, over all nodes in , of the depth of node , which we denote by .
a. Argue that the average depth of a node in is
Thus, we wish to show that the expected value of is .
b. Let and denote the left and right subtrees of tree , respectively. Argue that if has nodes, then
c. Let denote the average total path length of a randomly built binary search tree with n nodes. Show that
d. Show how to rewrite as
e. Recalling the alternative analysis of the randomized version of quicksort given in Problem 7-3, conclude that . At each recursive invocation of quicksort, we choose a random pivot element to partition the set of elements being sorted. Each node of a binary search tree partitions the set of elements that fall into the subtree rooted at that node.
f. Describe an implementation of quicksort in which the comparisons to sort a set of elements are exactly the same as the comparisons to insert the elements into a binary search tree. (The order in which comparisons are made may differ, but the same comparisons must occur.)
(Removed)