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

后端 未结 5 1705
予麋鹿
予麋鹿 2021-02-15 23:21

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:44

    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?

提交回复
热议问题