Why use heap instead of binary tree when implementing priority queue?

后端 未结 5 1437
鱼传尺愫
鱼传尺愫 2021-02-15 23:31

It seems to me that the only advantage of heap over binary tree is to find the smallest item in the heap in complexity of O(1) instead of O(log(2)n) in binary tree.

When

5条回答
  •  无人共我
    2021-02-15 23:55

    Heaps are usually simpler to implement than properly balanced binary trees. Additionally, they require less memory overhead (elements can be stored directly in an array, without having to allocate tree nodes and pointers and everything), potentially speedier performance (largely due to the memory locality of using a single contiguous array)...why wouldn't you use them?

提交回复
热议问题