Question on problem 11.6 - Compute the k largest elements in a max-heap

#1

Hi Tsung-Hsien,

I’am currently solving the task 11.6 and come on with a recursive solution instead of the max-heap of possible candidates.
The idea is:
1 start from the root
2. collect the biggest child
3. recursively explore the biggest child
4. and on recursion return collect and explore the 2nd child (if it exists and if we have not yet found required k elements)

The question is: is the recursive solution OK? Will it give the same complexity?

Thank you!

Elmira

Java implementation, max-heap is stored in the array where the 0-element is ignored, so the left child = 2* parent, right child = 2*parent+1 :

public static Iterable find(int[] maxHeap, int k) {
        if (maxHeap == null || maxHeap.length == 0 || k <= 0) return null;
        if (k >= maxHeap.length) return Arrays.asList(maxHeap);

        Queue<Integer> queue = new Queue<>();
        queue.enqueue(maxHeap[1]);
        find(maxHeap, queue, 1, k, maxHeap.length - 1);
        return queue;
    }

    private static void find(int[] maxHeap, Queue queue, int parentIndex, int k, int N) {
        if (queue.size() == k) return;
        if (parentIndex * 2 > N) return;

        int left = parentIndex * 2;
        int right = left == N ? -1 : left + 1;

        if (right != -1 && maxHeap[right] > maxHeap[left]) {
            queue.enqueue(maxHeap[right]);
            find(maxHeap, queue, right, k, N);

            if (queue.size() < k) {
                queue.enqueue(maxHeap[left]);
                find(maxHeap, queue, left, k, N);
            }
        } else {
            queue.enqueue(maxHeap[left]);
            find(maxHeap, queue, left, k, N);

            if (right != -1 && queue.size() < k) {
                queue.enqueue(maxHeap[right]);
                find(maxHeap, queue, right, k, N);
            }
        }
    }
0 Likes

#2

Please ignore this topic - > the solution is not correct.

Elmira

0 Likes

#3

Hey Elmira,

I haven’t checked the detail of your algorithm; however, would you mind to share why this solution is incorrect as I believe many others will be benefited from this also. Because there are some interview questions are giving you a problem and find the bugs in it, and yours might serve this purpose.

0 Likes

#4

Hi Tsung-Hsien,
sure,
the solution provided below is incorrect, let’s consider the following situation with max-heap and k=3:

          30
        /     \
      25       20
     /   \     /  \
  12     10   7   8   

initially we collect the root element (30).
Than we explore the left child (25) which is greater than right child (20).
Than according to that incorrect solution, we collect the left child (25) and explore its children: left child (12) and right child (10). Than we collect the biggest child (12)! But this is an error as we missed the early visited node (20), which is greater than all children of node (25).
I.e. as a result we will have: 30, 25, 12.
However this is incorrect and the correct one should be {30, 25, 20}.

Therefore the index priority queue is required for candidates checking!
That was my mistake, sorry.

Elmira.

0 Likes