0

I saw this question: Algorithm for merging two max heaps? and I want to add something - The max-heaps are at different size...

So my question is - If we have two max-heaps A and B that have a and b elements, and we know that the smallest element at A is bigger from the root (the biggest element) of B - How we can make a one max-heap at O(b)?

Thank you!

Community
  • 1
  • 1
Yoar
  • 183
  • 8

1 Answers1

0

If I understand correctly the smallest element in max-heap A is larger than the largest element in max-heap B, then you can append B to A (O(b) operation) and then reconstruct the max-heap for the B section of the array. Heap construction is an O(n) operation and because every element in A is greater than every element in B so the heap reconstruction in B will never update any element in A. So overall it should still be an O(b) operation.

Jason L
  • 510
  • 5
  • 16
  • I don't think is correct, because think about this: `A=[30], B=[20,19,6,18,17,5,4,15,14,13,12,3,2]` – Yoar Nov 28 '14 at 08:57