简体   繁体   English

将指定的比较器添加到 java 优先级队列

[英]adding a specified comparator to java priorityqueue

Im having a hard time understanding how to priorityqueue uses the compareTo method to sort its content.我很难理解如何优先排队使用 compareTo 方法对其内容进行排序。

I have on class called Node.我有一个名为 Node.js 的课程。 and its has 4 fields.它有4个字段。

private char character;
private int frequency;
private Node left_child;
private Node right_child;

Then in my other class called Huffmantree I have a priorityqueue.然后在我的另一个名为 Huffmantree 的班级中,我有一个优先队列。

My problem:我的问题:

I want to put object nodes in the queue so that when dequeueing it depends on the (int)frequency of the node.我想将对象节点放入队列中,以便在出队时它取决于节点的(int)频率。

Now my Node class look like this:现在我的 Node 类看起来像这样:

/**
 * 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 +"]";

    }

Here I have tried to implement the Comparable interface and I have defined a CompareTo method, comparing Nodes to their frequency value.在这里,我尝试实现 Comparable 接口,并定义了一个 CompareTo 方法,将节点与其频率值进行比较。

In my HuffmanTree class I have tried to make a priorityqueue like this:在我的 HuffmanTree 类中,我尝试创建一个像这样的优先队列:

PriorityQueue<Node> pq = new PriorityQueue<>(new Comparator<Node>()

but i dont know if this is how u would do it.但我不知道这是否是你的方式。 I´m stuck and I haven't found a good example of this.我被卡住了,我还没有找到一个很好的例子。

Since your Node class already implements the Comparable interface, there is no need to define a Comparator<Node> and pass it to your queue object, just use the no-argument constructor :由于您的Node类已经实现了Comparable接口,因此无需定义Comparator<Node>并将其传递给您的队列对象,只需使用无参数构造函数:

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

According to the official documentation :根据官方文档

 public PriorityQueue()

Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.创建一个具有默认初始容量 (11) 的 PriorityQueue,根据元素的自然顺序对其进行排序。

In this case, you have defined a natural ordering by implementing the compareTo method for your Node objects, so you're pretty much done.在本例中,您已经通过为Node对象实现compareTo方法来定义自然排序,因此您已经大功告成了。 The other constructor which takes a comparator must only be used if the elements in your queue are not Comparable s, or you wish to use a different ordering :只有当队列中的元素不是Comparable s,或者您希望使用不同的 ordering 时,才必须使用采用比较器的另一个构造函数:

// 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);
    }
});

The compareTo() is used to compare two objects. compareTo() 用于比较两个对象。

So if you want to compare based on the frequency then you should write:所以如果你想根据频率进行比较,那么你应该写:

public int compareTo(Node other) {    
        if(frequency>other.frequency)
            return 1;
        else if(frequency==other.frequency) 
             return 0;
         return -1;
    }

Here we are comparing the current frequency with the parametrized object.在这里,我们将当前频率与参数化对象进行比较。

You can get a detail description from here您可以从这里获得详细说明

And you dont need this:你不需要这个:

PriorityQueue pq = new PriorityQueue<>(new Comparator()) PriorityQueue pq = new PriorityQueue<>(new Comparator())

You can just write this:你可以这样写:

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

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

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