data structures - Algorithm for merging two max heaps? -
Is there an efficient algorithm for dissolving two max-masses that are stored as arrays?
It depends on the type of stack.
If this is a standard heap where every node is above two and the children who are filled on two separate rows, then you are not better than the O (n) for the merge Can.
Just put two arrows together and make a new pile of them that takes O (n).
For better merging performance, you can merge another Hep version using Fibonacci-OP (O) which amortized O (1).
Update: Note that first put all the elements of the stack in one heap after the other, or vice versa, because an insertion takes o (log). According to your comment, you do not know how the heap has been improved in the beginning (again for a standard binary stack)
- Create an array and insert it in the elements of both the heaps In some arbitrary order
- Now start at the lowest level. At the lowest level are the smallest size of size 1, so this level is completed
- Move up one level when The violation of the stack position of any of the "sub-stacks" Expertise, then swap the root of the "sub-stack" with her older child. Later, level 2 is done
- Go to Level 3 When the position of the stack is in violation, then proceed before
- ...
- When you reach the top, you have created a new heap in O (N) .
I am not leaving a proof here, but you can explain it because you have stacked a lot on the lower level where you do not have much material to restore the heap The condition is swapped You have run on a very small "sub-stack" which is much better than you if you insert each element into one of the heaps => Then you will work on the entire heap at all times, Takes .
Update 2: allows to merge into a bin (he) (hex (n)) and your o (log (n) ^ 2) corresponds to the requirement.
Comments
Post a Comment