Is there an efficient algorithm for merging 2 max-heaps that are stored as arrays?
-
13Yes. What have you tried so far? – Jonathan Feinberg Oct 20 '09 at 15:11
-
What do you mean by efficient? – PeterAllenWebb Oct 20 '09 at 15:14
-
well, if I just insert every element to new heap in a random order that would be average of O(nlogn) I think. so I am looking maybe for O(log(n)^2) – ThP Oct 20 '09 at 15:54
-
Put the comment inside the question, it will make it clear that you have already thought about the problem and are interested in solutions other than the trivial one. – Konrad Rudolph Oct 20 '09 at 16:07
-
2@Yaron: you can build the new heap in O(N + k). Just concatenate the arrays and build a new heap using the default method. – Alexandru Nov 26 '09 at 01:42
-
what's the default method ? I guess the default method takes O( n log n) not O(N+k) – Arian Apr 25 '13 at 13:04
-
@ArianHosseinzadeh The default heapify method takes an array of size n and builds a heap in theta(n) time. – Larry Jan 28 '16 at 07:26
-
https://en.wikipedia.org/wiki/Shadow_heap – Alexey Mar 12 '23 at 14:15
3 Answers
It depends on what the type of the heap is.
If it's a standard heap where every node has up to two children and which gets filled up that the leaves are on a maximum of two different rows, you cannot get better than O(n) for merge.
Just put the two arrays together and create a new heap out of them which takes O(n).
For better merging performance, you could use another heap variant like a Fibonacci-Heap which can merge in O(1) amortized.
Update: Note that it is worse to insert all elements of the first heap one by one to the second heap or vice versa since an insertion takes O(log(n)). As your comment states, you don't seem to know how the heap is optimally built in the beginning (again for a standard binary heap)
- Create an array and put in the elements of both heaps in some arbitrary order
- now start at the lowest level. The lowest level contains trivial max-heaps of size 1 so this level is done
- move a level up. When the heap condition of one of the "sub-heap"s gets violated, swap the root of the "sub-heap" with it's bigger child. Afterwards, level 2 is done
- move to level 3. When the heap condition gets violated, process as before. Swap it down with it's bigger child and process recursively until everything matches up to level 3
- ...
- when you reach the top, you created a new heap in O(n).
I omit a proof here but you can explain this since you have done most of the heap on the bottom levels where you didn't have to swap much content to re-establish the heap condition. You have operated on much smaller "sub heaps" which is much better than what you would do if you would insert every element into one of the heaps => then, you willoperate every time on the whole heap which takes O(n) every time.
Update 2: A binomial heap allows merging in O(log(n)) and would conform to your O(log(n)^2) requirement.

- 17,014
- 17
- 89
- 148
-
"you cannot get better than O(n) for merge" -- what is n, and O(n) of what? It is surely possible to use way fewer comparisons (and data moves) than if you simply put two arrays together and rebuild the heap. – Alexey Mar 11 '23 at 22:17
Two binary heaps of sizes n and k can be merged in O(log n * log k) comparisons. See

- 31,633
- 21
- 89
- 96
-
10This requires that the heap be implemented with pointers like a regular pointer-based tree, which is different from the common practice . – phoeagon Oct 01 '13 at 04:22
-
I was going to say the same thing @phoeagon - I started reading it and it says it requires O(k) data operations when it copies one array into another, but at the very end of the paper it points out that if using pointers instead of an array-based heap the data operations can be done in constant time leaving you back with O(log n * log k) operations. – Asaf Oct 18 '18 at 04:37
I think what you're looking for in this case is a Binomial Heap.
A binomial heap is a collection of binomial trees, a member of the merge-able heap family. The worst-case running time for a union (merge) on 2+ binomial heaps with n total items in the heaps is O(lg n).
See http://en.wikipedia.org/wiki/Binomial_heap for more information.

- 171
- 6