It is not hard to find a (deterministic) algorithm that looks at every leaf and correctly determines the value of the root, but this takes O(3h) time. Describe and analyze a randomized algorithm that, on average, looks at asymptotically fewer leaves. That is, the expected number of leaves your algorithm examines should be o(3h). (That is little-Oh of 3h.)
We define a new operation Fuse for two (binary) heaps, that builds a new heap H out of two given heaps A and B as follows.
Let A be the heap with higher priority, and let a be the root of A. (And so a has to be the root of the new heap H.) Let A' and A" be the left and right subtree of a.
We flip a coin that comes up either left or right, with equal probability.
If the coin comes up "left", then we build H as follows: The root is a, the left subtree is A', and the right subtree is built recursively by fusing A" with B.
If the coin comes up "right", then we build H as follows: The root is a, the right subtree is A", and the left subtree is built recursively by fusing A' with B.
The base case is when one heap becomes empty, then the result of fusing is the other heap.
Can you find a randomized algorithm that uses n+O(k log k log n) expected comparisons? (Note that there is no constant multiplying the n.)
Hint: While scanning through a random permutation of n elements, how many times does the smallest element seen so far change? How many times does the kth smallest element so far change?