Let L(v) denote the set of leaves in the descendants of node v in a tree, and let p(v,w) denote the number of edges of the simple path from node v to node w. For a non-leaf node v, maxw∈L(v)p(v,w) is called the height of v. Let the height of a leaf be 0. The height of the root of a tree is called the height of the tree.
Here, we have a binary tree Tn with height n≥0, in which each node v must have one of the following properties:
v is a leaf.
v has only one child, and the height of v is 1.
v has two children, and the heights of the two children of v differ by 1.
Let Nn denote the number of nodes in Tn for n≥0. Let r=21+5.
Answer the following questions:
(1) Calculate N5.
(2) Express Nn in terms of Nn−1 and Nn−2 for n≥2.
(3) Prove that Nn≥rn for every n≥0.
(4) Prove that Nn≤rn+2 for every n≥0.
(5) Consider the problem of assigning each of the given Nn integers to a distinct node of Tn. The integer assigned to each node v must be no smaller than any of the integers assigned to v's children. Show an O(rn) algorithm that computes such an assignment, with a proof that the algorithm runs indeed in O(rn) time. Note that the Nn integers may not be sorted in the input.
Assign the integers arbitrarily, then process the nodes in postorder. At each node, repeatedly swap its value with the larger child while that child is larger. Since both child subtrees are already heaps, this produces the required max-heap order.
A sift at the root of a height-h subtree costs O(h). Hence the total cost satisfies