Search code examples
javapriority-queuetreemap

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


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?


Solution

  • 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.