如果我将 poll() 方法与 PriorityQueue 和类似接口一起使用,它会返回什么

发布于 2025-01-16 04:48:16 字数 2313 浏览 3 评论 0原文

我正在使用PriorityQueue并且我已经使用compareTo方法实现了类似的类,

现在我想知道我的队列是否已排序,如果我使用poll()方法将这返回最小costSum的队列?

类:State.java

public class State<N extends Comparable<N>> implements Comparable<State<N>> {

    private final ArrayList<Integer> board;
    private State<N> predecessor;
    private double totalCostFromStart; //g(x)
    private double minimumRemainingCostToTarget; //h(x)
    private double costSum; //f(x)
    private Move direction;
    public State(ArrayList<Integer> board,
                 State<N> predecessor,
                 double minimumRemainingCostToTarget,
                 Move direction) {
        this.board = board;
        this.predecessor = predecessor;
        this.totalCostFromStart = predecessor == null ? 0 : predecessor.totalCostFromStart + 1;
        this.minimumRemainingCostToTarget = minimumRemainingCostToTarget;
        this.direction=direction;
        calculateCostSum();
    }

   private void calculateCostSum() {
        this.costSum = this.totalCostFromStart + this.minimumRemainingCostToTarget;
    }

    @Override
    public int compareTo(State<N> nNode) {
        int compare = Double.compare(this.costSum, nNode.costSum);
        if (compare == 0) return 0;
        else return this.costSum>nNode.costSum ? 1:-1;
    }

类:AStar.java

    public State AStar(ArrayList<Integer> initialBoard,
                       State source,
                       ArrayList<Integer> target,
                       Heuristic heuristic){
        int minimumRemainingCostToTarget= heuristic.getRank(initialBoard, target);
        source = new State( initialBoard,null,0, minimumRemainingCostToTarget,null);
        PriorityQueue<State> open = new PriorityQueue<>();
        Set<ArrayList<Integer>> close = new HashSet<>(181440);

        //add initial state to ouverts, f(n) is an attribut in source.
        open.add(source);

        while(!close.isEmpty()){

            State currentState = open.poll();//<<<----------------------
        }
        return null;
    }

I'm using PriorityQueue and i've implemented comparable class, with compareTo method,

Now i want to know if my queue is sorted, if i use poll() method will this return the queue of the minimum costSum?

Class: State.java

public class State<N extends Comparable<N>> implements Comparable<State<N>> {

    private final ArrayList<Integer> board;
    private State<N> predecessor;
    private double totalCostFromStart; //g(x)
    private double minimumRemainingCostToTarget; //h(x)
    private double costSum; //f(x)
    private Move direction;
    public State(ArrayList<Integer> board,
                 State<N> predecessor,
                 double minimumRemainingCostToTarget,
                 Move direction) {
        this.board = board;
        this.predecessor = predecessor;
        this.totalCostFromStart = predecessor == null ? 0 : predecessor.totalCostFromStart + 1;
        this.minimumRemainingCostToTarget = minimumRemainingCostToTarget;
        this.direction=direction;
        calculateCostSum();
    }

   private void calculateCostSum() {
        this.costSum = this.totalCostFromStart + this.minimumRemainingCostToTarget;
    }

    @Override
    public int compareTo(State<N> nNode) {
        int compare = Double.compare(this.costSum, nNode.costSum);
        if (compare == 0) return 0;
        else return this.costSum>nNode.costSum ? 1:-1;
    }

Class : AStar.java

    public State AStar(ArrayList<Integer> initialBoard,
                       State source,
                       ArrayList<Integer> target,
                       Heuristic heuristic){
        int minimumRemainingCostToTarget= heuristic.getRank(initialBoard, target);
        source = new State( initialBoard,null,0, minimumRemainingCostToTarget,null);
        PriorityQueue<State> open = new PriorityQueue<>();
        Set<ArrayList<Integer>> close = new HashSet<>(181440);

        //add initial state to ouverts, f(n) is an attribut in source.
        open.add(source);

        while(!close.isEmpty()){

            State currentState = open.poll();//<<<----------------------
        }
        return null;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-23 04:48:16

现在我想知道我的队列是否已排序,如果我使用 poll() 方法,这会返回最小 costSum 的队列吗?

Javadoc 描述了这一点:

此队列的头部是相对于指定顺序的最小元素。 ...队列检索操作 poll、remove、peek 和 element 访问队列头部的元素。

所以,是的,它是最小元素。

但请注意,队列内部排序:如果打印优先级队列,您可能会注意到它们不是按升序显示的。元素只是按照堆属性的顺序存储,一旦最小元素被删除,就可以有效地更新数据结构。

Now i want to know if my queue is sorted, if i use poll() method will this return the queue of the minimum costSum?

The Javadoc describes this:

The head of this queue is the least element with respect to the specified ordering. ... The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

So, yes, it is the minimum element.

Note, however, that the queue isn't internally sorted: if you print a priority queue, you may note that they do not appear in ascending order. The elements are simply stored in an order with the heap property, which allows efficient updating of the data structure once the minimum element is removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文