尝试按存储节点的值之一对优先级队列进行排序 [英] Trying to sort a priority queue by one of the stored nodes' values

查看:72
本文介绍了尝试按存储节点的值之一对优先级队列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是优先队列的新手。在我要实现的算法中,我想根据存储的节点的functionValue对优先级队列进行排序。我不了解优先级队列如何知道根据该值对节点进行排序,而不是我的节点对象的其他八个实例变量之一。我很确定我定义了一个Comparator对象来定义比较/排序规则,但是我不能为Oracle Comparator的Oracle类库做头或尾。

So I'm new to priority queues. In an algorithm I'm trying to implement, I want to sort a priority queue according to the stored nodes' functionValue. I don't understand how the priority queue will know to sort the nodes by that value, as opposed to one of the other eight instance variables of my node objects. I'm pretty sure I define a Comparator object to define the comparison/ sorting rules, but I can't make heads or tails of the Oracle class library for Comparator.

这是我的节点类的属性

public class Node{

public char label;      //Holds char for the Move used; U, D, L, R
public boolean visited = false; 
public State nodeState; //Each node holds a State object 
int depth;
int heuristicCount;
int functionCount;   <--- This is the property I want the priority queue to sort by.
.
.
.


推荐答案

比较器是一个非常简单的界面。您遇到什么困难?关键位是 比较方法。您只需实现它即可比较您的类的两个实例。

Comparator is a pretty simple interface. What's your difficulty with it? The key bit is the compare method. You simply implement that to compare two instances of your class. Something like:

public class NodeComparator implements Comparator<Node> {
    public int compare(Node a, Node b) {
        Integer aCount = a.getFunctionCount();
        Integer bCount = b.getFunctionCount();
        return a.compareTo(b);
    }
}

这篇关于尝试按存储节点的值之一对优先级队列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆