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

iceburn picture iceburn · Aug 19, 2010 · Viewed 18k times · Source

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 advantage of using one over the other, not just for this example, but generally speaking?

Answer

erickson picture erickson · Aug 19, 2010

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.