Say we are given a single-tree binomial heap, such that the binomial tree is of rank r, and thus holding 2^r keys. What's the most efficient way to convert it into a k<2^r length sorted array, with the k smallest keys of the tree? Let's assume we can't use any other data structure but Lazy Binomial Heaps, and Binomial Trees. Notice that at each level the children are unnecessarily linked by order, so you might have to make some comparisons at some point.
My solution was (assuming 1<=k<=2^r):
- Create a new empty lazy binomial heap H.
- Insert the root's key into the heap.
- Create a new counter x, and set x=1.
- For each level i=0,1,... (where the root is at level 0):
- Let c be the number of nodes at level i.
- Set x=x+c.
- Iterate over the nodes in level i and:
- Insert each node N to H. (In O(1))
- If x < k, recursively make the same process for each node N, passing through x so the counting continues.
- Repeat k times:
- Extract the minimal key out of the heap and place it in the output array.
- Delete the minimal key form the heap. (amortized cost: O(1))
- Return output array.
There might be some holes in the pseudo-code, but I think the idea itself is clear. I also managed to implement it. However, I'm not sure that's the most efficient algorithm for this task.