简体   繁体   English

使用最低密钥从优先级队列中删除节点

[英]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. 我有getCost()和getTax()方法,但是我不知道这里是否需要它们。

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. 将自动删除具有最低键的那个。

A PriorityQueue needs to be populated with Comparable elements, or else be supplied with a Comparator when constructed. PriorityQueue需要用Comparable元素填充,或者在构造时随Comparator提供。 So your Node needs to be comparable based on the total = cost + tax. 因此,您的Node需要基于总数=成本+税金进行比较。 If you do that correctly, the queue will put the smallest element at the top. 如果正确执行此操作,则队列会将最小的元素放在顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM