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

后端 未结 5 1721
予麋鹿
予麋鹿 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:54

    Worst case complexity in case of binary tree will be O(n) when binary tree converges to an array while in heap it remains O(log(n)). you can use balanced binary trees like red black or AVl but then it wud become more complex and would require more memory.

提交回复
热议问题