向java priorityqueue添加指定的比较器
我很难理解如何优先队列使用compareTo方法对其内容进行排序 我有一个名为Node的类。 它有4个字段向java priorityqueue添加指定的比较器,java,priority-queue,Java,Priority Queue,我很难理解如何优先队列使用compareTo方法对其内容进行排序 我有一个名为Node的类。 它有4个字段 private char character; private int frequency; private Node left_child; private Node right_child; 然后在我的另一个叫做Huffmantree的课上,我有一个优先队列 我的问题是: 我想把对象节点放在队列中,这样当退出队列时,它取决于节点的(int)频率 现在,我的节点类如下所示: /**
private char character;
private int frequency;
private Node left_child;
private Node right_child;
然后在我的另一个叫做Huffmantree的课上,我有一个优先队列
我的问题是:
我想把对象节点放在队列中,这样当退出队列时,它取决于节点的(int)频率
现在,我的节点类如下所示:
/**
* Class for creating nodes with a specified character, frequency,
* left- and right child.
* implements comparable to be able to compare 2 nodes based on their
* frequency.
*/
public class Node implements Comparable<Node>{
private char character;
private int frequency;
private Node left_child;
private Node right_child;
public Node(char character, int frequency, Node left_child, Node
right_child){
this.character = character;
this.frequency = frequency;
this.left_child = left_child;
this.right_child = right_child;
}
//Checks if two nodes have equal frequency.
private boolean equals(Node n){
return this.frequency == n.frequency;
}
@Override
public int compareTo(Node other) {
if(this.equals(other)){
return 0;
}else if(this.frequency > other.frequency){
return 1;
}else return -1;
}
//Print out current node
@Override
public String toString(){
return "[" +this.character+"," + this.frequency +"]";
}
PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>()
/**
*用于创建具有指定字符、频率、,
*左边和右边的孩子。
*实现Compariable,以便能够根据两个节点的
*频率。
*/
公共类节点实现了可比较的{
私有字符;
专用整数频率;
私有节点左_子节点;
私用节点权限\子节点;
公共节点(字符、整数频率、节点左\子节点、节点
右(儿童){
这个。字符=字符;
这个频率=频率;
this.left_child=left_child;
this.right\u child=right\u child;
}
//检查两个节点的频率是否相等。
私有布尔等于(节点n){
返回this.frequency==n.frequency;
}
@凌驾
公共整数比较(节点其他){
如果(这个等于(其他)){
返回0;
}else if(this.frequency>other.frequency){
返回1;
}否则返回-1;
}
//打印当前节点
@凌驾
公共字符串toString(){
返回“[”+this.character+”,“+this.frequency+”];
}
在这里,我尝试实现类似的接口
我定义了一个CompareTo方法,将节点与其频率值进行比较
在我的HuffmanTree类中,我尝试创建如下优先级队列:
/**
* Class for creating nodes with a specified character, frequency,
* left- and right child.
* implements comparable to be able to compare 2 nodes based on their
* frequency.
*/
public class Node implements Comparable<Node>{
private char character;
private int frequency;
private Node left_child;
private Node right_child;
public Node(char character, int frequency, Node left_child, Node
right_child){
this.character = character;
this.frequency = frequency;
this.left_child = left_child;
this.right_child = right_child;
}
//Checks if two nodes have equal frequency.
private boolean equals(Node n){
return this.frequency == n.frequency;
}
@Override
public int compareTo(Node other) {
if(this.equals(other)){
return 0;
}else if(this.frequency > other.frequency){
return 1;
}else return -1;
}
//Print out current node
@Override
public String toString(){
return "[" +this.character+"," + this.frequency +"]";
}
PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>()
PriorityQueue pq=新的PriorityQueue(新的比较器()
但是我不知道你是否会这样做。我被卡住了,我还没有找到一个好的例子。compareTo()用于比较两个对象
因此,如果你想根据频率进行比较,那么你应该写:
public int compareTo(Node other) {
if(frequency>other.frequency)
return 1;
else if(frequency==other.frequency)
return 0;
return -1;
}
这里我们将当前频率与参数化对象进行比较
您可以从中获得详细说明
你不需要这个:
PriorityQueue pq=新的PriorityQueue(新的比较器())
你可以这样写:
PriorityQueue pq=新的PriorityQueue()
由于您的
节点
类已经实现了Comparable
接口,因此无需定义比较器
并将其传递给队列对象,只需使用无参数构造函数:
PriorityQueue<Node> pq = new PriorityQueue<>();
使用默认值创建PriorityQueue
初始容量(11),根据元件的特性对其元件进行排序
自然有序
在本例中,通过为节点
对象实现compareTo
方法,您已经定义了一个自然排序,因此您已经完成了大部分工作。只有在队列中的元素不可比较或者您希望使用不同的排序时,才必须使用另一个使用比较器的构造函数:
// This queue will hand out nodes in the inverse order of their frequency
PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() {
@Override
public int compare(Node a, Node b) {
return -a.compareTo(b);
}
});
//此队列将按与其频率相反的顺序分发节点
PriorityQueue队列=新的PriorityQueue(新的比较器(){
@凌驾
公共整数比较(节点a、节点b){
返回-a.与(b)比较;
}
});
你想比较什么?请指定比较的条件。如果它们相等,你不应该返回零吗?是的。它们应该返回零。谢谢你!这正是我想要的答案:)@Fanny很乐意帮忙,请务必接受我的答案,这样答案总是第一个出现(并为我赢得一些分数;))我不太习惯stackoverflow,但我按下了“check”符号,数组向上,希望是正确的:)