0

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.

2
  • See this. Commented Feb 24, 2019 at 21:03
  • @ChiefTwoPencils Okay so remove() removes the head of the queue. But is the head the one with the lowest key value? Commented Feb 24, 2019 at 21:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.