2

I recently took my Computer Science exam and there was a question like that.

There are two max-heaps (array implemented). You need to come up with an algorithm that merges these two max-heaps and creates a new max-heap (array implemented)

Solution of the question is very intuitive that is:

  1. Merge two array
  2. Call heap rebuild

I looked in the Internet and encountered with same type of solutions.

However, I wrote a solution like that which I could not refute own my own.

My Algorithm

  1. Create index1 and index2 which points first element of heapArr1 and heapArr2
  2. Create a new heap array which has size of heapArr1.size + heapArr2.size
  3. In while loop
  4. compare index1 element of heapArr1 and index2 element of heapArr2
  5. Whichever is greater, write the result array and increment the index of the array that element taken until two arrays all traversed.

For example

Heap1: 12-5 -6-2-3

Heap2: 15-13-4-1-0

We wil first compare 15 and 12. Write 15

resultHeap: 15

Now compare 13 and 12

resultHeap: 15 - 13

Compare 4 and 12

resultHeap: 15 - 13 - 12

Compare 4 and 5

resultHeap: 15 - 13 - 12 - 4

if we go on like that we have

resultHeap: 15 - 13 - 12 - 5 - 6 - 4 - 2 - 3 - 1 - 0. And it is also a heap

Is this algorithm correct? Or can someone gave the refutation data set?

Lapaaci
  • 119
  • 7
  • 1
    Well you are following the steps to build a heap, which is already proven to be correct. The only case you find something wrong is if you made some mistake while following the algorithm. But you are not satisfying the condition where you need to not rebuild the heap. – Rinkesh P May 19 '22 at 16:52
  • My claim is I do not ever need to rebuild the heap. Do I need to in some case? – Lapaaci May 19 '22 at 17:11
  • That would be something debatable, cause if you look carefully you are in practice following the build heap procedure, its just that you never need to rebalance them cause the way you access them is already in level decreasing order. – Rinkesh P May 19 '22 at 17:16
  • Consider the case where you had the resultant heap stored in an array, and you are asked to build a heap from it. This is what you are doing essentially(in my example with 1 heap). – Rinkesh P May 19 '22 at 17:17
  • 1
    A sorted array is always a valid heap. And since heap2 is completely sorted, and heap1 has only two minor inversions, your algorithm seems to work. To stress your algorithm, you need to make the two input array as unsorted as possible while still being valid. For example, `{12, 6, 2, 3, 5}` and `{15, 13, 0, 1, 4}`. – user3386109 May 19 '22 at 17:55
  • See also: [merge heaps on CS@SE](https://cs.stackexchange.com/search?q=merge+heaps), [on SO](https://stackoverflow.com/search?q=merge+heaps). – greybeard May 19 '22 at 18:35

2 Answers2

3

Is this algorithm correct?

No.

can someone gave the refutation data set?

Take this input:

  • First Heap: 10, 2, 9

        10
       /  \
      2    9
    
  • Second Heap: 8, 1, 4

        8
       / \
      1   4
    

Apply the algorithm -- the brackets indicate the current index in each heap:

Heap 1 Heap 2 Result heap
[10],2,9 [8],1,4 10
10,[2],9 [8],1,4 10,8
10,[2],9 8,[1],4 10,8,2
10,2,[9] 8,[1],4 10,8,2,9 (violation)
10,2,9[] 8,[1],4 10,8,2,9,1
10,2,9[] 8,1,[4] 10,8,2,9,1,4 (violation)
      10
     /   \
    8     2
   / \   /
  9   1 4 

The result is not a valid heap as 9 should not be a child of 8, since 9 is greater. And 4 should not be the child of 2, since 4 is greater.

trincot
  • 317,000
  • 35
  • 244
  • 286
-1

This could be one possible solution for heaps of equal size.

  1. Assume 2 max(or min) binary heaps(A and B).
  2. First compare the roots, the one which is smaller(assume A) can safely be assumed to be candidate for the subtree of the other one(B), so assign it as any one child of B(assume left)
  3. Since B's children are now being replaced you have 2 new heaps to consider i.e B's original children
  4. And now you have an empty space i.e the right child of B. Follow this procedure recursively to merge B's chilren and assign the resultant heap as the right child of B.

This is just fancy pointer manipulation and it isn't actually building a heap, rather taking advantage of the fact that you already have heaps.

Apologies for not being able to express it more formally. Please correct me if you find something wrong. This is just the general idea and doesn't consider implementation specific details.

Rinkesh P
  • 588
  • 4
  • 13
  • 2
    This (`just fancy pointer manipulation`) doesn't quite fit `array implemented`. – greybeard May 19 '22 at 18:26
  • Yes. But I suppose one can find their way out by making linked representation of 2 initial heaps, merging them and making an array out of the resultant. Not exactly what op needs I guess but just wanted to put a solution out there. Atleast I am not rebuilding the heap. – Rinkesh P May 20 '22 at 02:51