1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Leto [7]
3 years ago
9

Suppose that you have two binary heaps H1 and H2, each with exactly 2k-1 elements. The heaps are represented with explicit binar

y trees; they are not encoded into arrays. Describe (in precise English, not with code) an O(k) algorithm that will combine H1 and H2 into a single binary heap with 2k 1- 2 elements.
Computers and Technology
1 answer:
motikmotik3 years ago
3 0

Answer:

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.

You might be interested in
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the elemen
Verdich [7]

Answer:

Replace <STUDENT CODE> with

for (i = 0; i < SCORES_SIZE; ++i) {

       if(lowerScores.at(i)<=0){

           lowerScores.at(i) = 0;

       }

       else{

           lowerScores.at(i) = lowerScores.at(i) - 1;

       }  

   }

Explanation:

To do this, we simply iterate through the vector.

For each item in the vector, we run a check if it is less than 1 (i.e. 0 or negative).

If yes, the vector item is set to 0

If otherwise, 1 is subtracted from that vector item

This line iterates through the vector

for (i = 0; i < SCORES_SIZE; ++i) {

This checks if vector item is less than 1

       if(lowerScores.at(i)<1){

If yes, the vector item is set to 0

           lowerScores.at(i) = 0;

       }

       else{

If otherwise, 1 is subtracted from the vector item

           lowerScores.at(i) = lowerScores.at(i) - 1;

       }  

   }

Also, include the following at the beginning of the program:

<em>#include <vector></em>

8 0
3 years ago
A single instruction carried out by a computer is called a what?
pochemuha

Answer:

in computer science, an instruction is a single operation of a processor defined by the instruction set

Explanation:

if you need anymore help let me know :)

6 0
3 years ago
the impact of the global digital divide. According to the authors, countries that lag behind the rest of the world’s ICT capabil
kvasek [131]

Answer:

hi

Explanation:

5 0
3 years ago
As text is typed in the _____ text box, a drop-down list displays options for completing the action, displaying information on t
pickupchik [31]

Answer:

Excel Help

Explanation:

Press F1 key to open the excel help window

Or you can also click the excel help button to launch the help window.

Type for whatever you are seeking help for and you will notice suggestions as you type something. These suggestions include online and offline results. If there are too many results then try searching offline it will narrow down the results.

After typing the text and hitting enter, the excel help returns several related topics on the searched query. You can browse through these topics and get help from excel.

5 0
3 years ago
In which job role would a course in 3D modeling help with professional career prospects?please help
lana66690 [7]
Arcitechture, video game design, etc.
6 0
4 years ago
Read 2 more answers
Other questions:
  • To what extent do you think st. cyprian was effective in his efforts to bring inherited christian teachings to bear on the unpre
    10·1 answer
  • You can apply bold, italic, and underline styles to text. But why is it a good idea to use underline sparingly?
    13·2 answers
  • Which protocol do many browsers assume when you enter a URL in the address field?
    9·1 answer
  • What are the best data structures to create the following items? And why?
    13·1 answer
  • Which of these is a requirement for developing game applications on devices running Apple’s iOS operating system?
    6·2 answers
  • Which of these is NOT one of the main parts of an email?
    14·2 answers
  • Networking and telecommunications technologies, along with computer hardware, software, data management technology, and the peop
    5·1 answer
  • A small company has hired you to take over its IT needs. The company currently has seven on-site employees and 12 remote employe
    12·1 answer
  • What is 16/17 simplified?
    14·2 answers
  • Osing Commands
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!