Java中带出队列的优先级队列问题

Java中带出队列的优先级队列问题,java,heap,priority-queue,min,Java,Heap,Priority Queue,Min,嗨,所以我现在正在做一个项目,我的最小值的出列似乎有一个问题。它似乎排得很好。在20,15,11,29中打孔,它与11,20,15,29正确对应。这些数字与要达到的最后期限相对应。现在,当我想将最大值出列时,它将抓取11,对其执行所需的操作,丢弃截止日期为11的任务。然后,当我获取下一个最大值时,它返回20,而不是15。我不知道为什么?任何帮助都将不胜感激 import java.util.Comparator; public class treeCompare implements Com

嗨,所以我现在正在做一个项目,我的最小值的出列似乎有一个问题。它似乎排得很好。在20,15,11,29中打孔,它与11,20,15,29正确对应。这些数字与要达到的最后期限相对应。现在,当我想将最大值出列时,它将抓取11,对其执行所需的操作,丢弃截止日期为11的任务。然后,当我获取下一个最大值时,它返回20,而不是15。我不知道为什么?任何帮助都将不胜感激

import java.util.Comparator;


public class treeCompare implements Comparator<Task> {

    public int compare(Task t1, Task t2) {

        int dead1 = t1.getDeadline();
        int dead2 = t2.getDeadline();

        return dead1 - dead2;

    }


}

    import java.util.ArrayList;
import java.util.Comparator;


public class PriorityQueue<E> implements QueueADT<E> {

    private ArrayList<E> items;
    private int max;
    private Comparator<E> comp;

    public PriorityQueue(Comparator<E> comp, int maxCapacity){
        this.comp = comp;
        this.max = maxCapacity;
        items = new ArrayList<E>();
    }



    public boolean isFull() {
        if(size() == max) return true;
        else return false;
    }


     private void siftUp() {
            int k = items.size() - 1;
            while (k > 0) {
                int p = (k-1)/2;
                E item = items.get(k);
                E parent = items.get(p);

                     if (comp.compare(items.get(k), items.get(p)) < 0) {
                        // swap
                        items.set(k, parent);
                        items.set(p, item);

                        // move up one level
                        k = p;

                } else {
                    break;
                }
            }
        }

        public void enqueue(E item)throws FullQueueException {
            if(isFull()) throw new FullQueueException();
            items.add(item);
            siftUp();
        }


    /**
     * Returns the front item of the queue without removing it.
     * @return the front item of the queue
     * @throws EmptyQueueException
     */
    public E peek() throws EmptyQueueException {
        if(isEmpty()) throw new EmptyQueueException();
        else{
            E item = items.get(0);
            return item;
        }
    }

    private void siftDown() {
        int k = 0;
        int l = 2*k+1;
        while (l < items.size()) {
            int max=l, r=l+1;
            if (r < items.size()) { // there is a right child
                if (comp.compare(items.get(r), items.get(l)) > 0) {
                    max++;
                }
            }
            if (comp.compare(items.get(k), items.get(max)) > 0) {
                    // switch
                    E temp = items.get(k);
                    items.set(k, items.get(max));
                    items.set(max, temp);
                    k = max;
                    l = 2*k+1;
            } else {
                break;
            }
        }
    }

    public E dequeue() throws EmptyQueueException {
        if (items.size() == 0) {
            throw new EmptyQueueException();
        }
        if (items.size() == 1) {
            return items.remove(0);
        }
        E hold = items.get(0);
        items.set(0, items.remove(items.size()-1));
        siftDown();
        return hold;
    }

    public int size() {
        return items.size();
    }

    public boolean isEmpty() {
        return items.isEmpty();

    }



   public String toString() {
       return items.toString();
   }


    public int capacity() {
        return max;
    }

}
import java.util.Comparator;
公共类treeCompare实现了Comparator{
公共整数比较(任务t1、任务t2){
int dead1=t1.getDeadline();
int dead2=t2.getDeadline();
返回死区1-死区2;
}
}
导入java.util.ArrayList;
导入java.util.Comparator;
公共类PriorityQueue实现QueueADT{
私有ArrayList项;
私人int max;
私人比较公司;
公共优先级队列(比较器comp,int maxCapacity){
this.comp=comp;
this.max=最大容量;
items=newarraylist();
}
公共布尔值isFull(){
如果(size()==max)返回true;
否则返回false;
}
私有void siftUp(){
int k=items.size()-1;
而(k>0){
int p=(k-1)/2;
E item=items.get(k);
E父项=项。获取(p);
if(comp.compare(items.get(k)、items.get(p))<0){
//交换
项目集(k,父项);
项目集(p,项目);
//升一级
k=p;
}否则{
打破
}
}
}
公共无效排队(E项)引发FullQueueException{
如果(isFull())抛出新的FullQueueException();
项目。添加(项目);
siftUp();
}
/**
*返回队列的前端项,但不删除它。
*@返回队列的前面项目
*@throws-EmptyQueueException
*/
public E peek()引发EmptyQueueException{
如果(isEmpty())抛出新的EmptyQueueException();
否则{
E item=items.get(0);
退货项目;
}
}
私人空间{
int k=0;
int l=2*k+1;
而(l0){
max++;
}
}
如果(comp.compare(items.get(k)、items.get(max))大于0){
//开关
E温度=项目。获取(k);
items.set(k,items.get(max));
项目设置(最大值、温度);
k=最大值;
l=2*k+1;
}否则{
打破
}
}
}
public E dequeue()引发EmptyQueueException{
如果(items.size()==0){
抛出新的EmptyQueueException();
}
如果(items.size()==1){
返回项目。删除(0);
}
E hold=items.get(0);
items.set(0,items.remove(items.size()-1));
siftDown();
返回保持;
}
公共整数大小(){
返回items.size();
}
公共布尔值为空(){
return items.isEmpty();
}
公共字符串toString(){
return items.toString();
}
公共交通容量(){
返回最大值;
}
}

shiftDown()
中的比较
comp.compare(items.get(r)、items.get(l))>0

将其反转(并为清晰起见重命名变量max)该算法似乎有效(就我测试的结果而言)

private void siftDown(){
int k=0;
int l=2*k+1;
而(l0){
//开关
E温度=项目。获取(k);
items.set(k,items.get(min));
项目设置(最小值、温度);
k=最小值;
l=2*k+1;
}否则{
打破
}
}
}
如果我插入[20,15,11,1,29,10,7,28,3,54,40,34,58],我将检索[1,3,7,10,11,15,20,28,29,34,40,54,58],看起来很不错。

希望有帮助

您为什么不使用JDK的
PriorityQueue
?赋值的目的之一是对我们自己的优先级队列进行编码在您的问题中,您声明,值的正确顺序对应于11、20、15、29,那么如果11之后得到20,问题出在哪里?那么20是一个大于15的值,因此当我们排队时,20岁可能会被加到左边的孩子身上,但当它真正的离开队列和重新安置的时候,15岁需要占据首位谢谢,新鲜的眼睛肯定会有帮助,不是吗?谢谢你的提示!
    private void siftDown() {
        int k = 0;
        int l = 2*k+1;
        while (l < items.size()) {
            int min=l, r=l+1;
            if (r < items.size()) { // there is a right child
                if (comp.compare(items.get(r), items.get(l)) < 0) {
                    min = r;
                }
            }
            if (comp.compare(items.get(k), items.get(min)) > 0) {
                    // switch
                    E temp = items.get(k);
                    items.set(k, items.get(min));
                    items.set(min, temp);
                    k = min;
                    l = 2*k+1;
            } else {
                break;
            }
        }
    }