user11110481
user11110481

Reputation:

Remove Node From Priority Queue With Lowest Key

I have a priority queue set up as the following:

PriorityQueue<Node> pq = new PriorityQueue<Node>(100);

my instructions say that the key for the priority queue will be total = cost + tax. I have getCost() and getTax() methods but I dont know if they are needed here.

I am trying to remove the Node with the lowest key value. I am not sure if I need to specify the key when initializing the priority queue, or if doing

pq.remove()

will automatically remove the one with the lowest key.

Upvotes: 0

Views: 200

Answers (1)

Josh
Josh

Reputation: 500

A PriorityQueue needs to be populated with Comparable elements, or else be supplied with a Comparator when constructed. So your Node needs to be comparable based on the total = cost + tax. If you do that correctly, the queue will put the smallest element at the top.

Upvotes: 1

Related Questions