When should I use a TreeMap over a PriorityQueue and vice versa?

后端 未结 8 2165
南旧
南旧 2020-12-23 11:46

Seems they both let you retrieve the minimum, which is what I need for Prim\'s algorithm, and force me to remove and reinsert a key to update its value. Is there any advanta

8条回答
  •  醉酒成梦
    2020-12-23 12:10

    Generally speaking, it is less work to track only the minimum element, using a heap.

    A tree is more organized, and it requires more computation to maintain that organization. But if you need to access any key, and not just the minimum, a heap will not suffice, and the extra overhead of the tree is justified.

提交回复
热议问题