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

后端 未结 5 1448
鱼传尺愫
鱼传尺愫 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:53

    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.

提交回复
热议问题