1,662 views
14 votes
14 votes
Let $H_1$ and $H_2$ be two complete binary trees that are heaps as well. Assume $H_1$ and $H_2$ are max-heaps, each of size $n$. Design and analyze an efficient algorithm to merge $H_1$ and $H_2$ to a new max-heap $H$ of size $2n$.

2 Answers

Best answer
14 votes
14 votes

First copy the two arrays of $H_1$ and $H_2$ into a new array of size $2n$...then apply build heap operation to get $H$...Time complexity$=O(2n)=O(n)$

http://www.geeksforgeeks.org/merge-two-binary-max-heaps/

edited by
0 votes
0 votes
i think question is asking to build heap H such that the height of the combined heap is 2n. Then just merging is required and time complexity will be 0(n)

Related questions

2 votes
2 votes
1 answer
1
1 votes
1 votes
1 answer
2
go_editor asked May 31, 2016
704 views
Let $G = (V, E)$ be an undirected weighted graph with all edge weights being positive. Design an efficient algorithm to find the maximum spanning tree of $G$.