向已包含 n 个元素的二叉堆插入 n 个元素的渐近时间复杂度
假设我们有一个包含 n 个元素的二叉堆,并且希望再插入 n 个元素(不一定是一个接一个)。总共需要多少时间?
我认为它是 theta (n logn),因为一次插入需要 logn。
Suppose we have a binary heap of n elements and wish to insert n more elements(not necessarily one after other). What would be the total time required for this?
I think it's theta (n logn) as one insertion takes logn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给定:n 个元素的堆以及要插入的 n 个元素。所以最终会有2*n个元素。因为堆可以通过两种方式创建:1.连续插入和2.构建堆方法。在这些构建堆方法中,构建堆需要 O(n) 时间,这在
构建堆的时间复杂度如何达到 O(n) ?。所以所需的总时间是 O(2*n) ,与 O(n) 相同
given : heap of n elements and n more elements to be inserted. So in the end there will be 2*n elements. since heap can be created in 2 ways 1. Successive insertion and 2. Build heap method. Amoung these build heap method takes O(n) time to construct heap which is explained in
How can building a heap be O(n) time complexity?. so total time required is O(2*n) which is same as O(n)
假设我们给出:
我们有以下插入属性:
因此,对于每种情况,我们有
最坏的情况是,我们插入新的最小值,因此向上堆必须遍历整个分支。
最佳情况是,对于最小堆(顶部最小的堆),我们插入 BIG(更新分支上最大的)值(因此向上堆立即停止)。
您询问了对已经包含 n 个元素的堆进行的一系列 n 操作,
它的大小将渐进
地增长......
这简化了我们的方程。 (我们不必担心 n 的增长,因为它的增长是按常数因子计算的)。
因此,我们有“n 次插入”操作:
PS 要显示 Theta θ 、 Omega Ω 符号,您需要安装 UTF-8/兼容。
Assuming we are given:
We have following insertion properties:
So for every case, we have
WorstCase is when, we insert new minimal value, so up-heap has to travel whole branch.
BestCase is when, for minimal-heap (heap with minimal on top) we insert BIG (biggest on updated branch) value (so up-heap stops immediately).
You've asked about series of n operations on heap containing already n elements,
it's size will grow
what asymptotically is ...
What simplifies our equations. (We don't have to worry about growth of n , as it's growth is by constant factor).
So, we have "for n insertions" of operation:
P.S. For displaying Theta Θ , Omega Ω symbols, you need to have UTF-8 installed/be compatible.
它不是 theeta(nlogn)...它的 order(nlogn) 因为某些插入可能需要比精确的 logn 时间更短的时间...因此对于 n 次插入,它将花费时间 <=nlogn
=>时间复杂度=O(nlogn)
its not theeta(nlogn)... its order(nlogn) since some of the insertions can take less then exact logn time... therefore for n insertions it will take time <=nlogn
=> time complexity=O(nlogn)