简体   繁体   中英

Construct a binary heap of size 2N from two binary heaps each of size N in linear time?

Constructing a binary heap of size N from scratch takes NlogN compares on an average and thus linearithmic time.

Given that two binary heaps of size N are already in place, how to construct a single binary heap containing all 2N keys, in linear time (uses linear number of compares)?

Constructing a binary heap of size N from scratch takes NlogN compares on an average and thus linearithmic time.

If you mean "constructing a binary heap of size N from an array of size N", then this is not necessarily true. You can do it in linear time. See Building a heap here .

So for your problem, if you have your two heaps in two arrays, concatenating the arrays and running the same algorithm on the resulting array will be linear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM