Java 是否优先队列';s remove方法重新排列堆?

Java 是否优先队列';s remove方法重新排列堆?,java,sorting,queue,compare,heap,Java,Sorting,Queue,Compare,Heap,在java中,当对PriorityQueue对象调用remove方法时,堆头被移除。要将新的最小元素放在头部,是否对堆的其余部分执行了任何排序操作?例如,调用remove时是否调用了compareTo方法 抱歉,如果这在文档中,我在任何地方都找不到。提前感谢。优先级队列实现为一个平衡的二进制堆,实现为一个数组。删除元素后,堆必须重新排序以保持堆的顺序 证据在评论中 /** * Priority queue represented as a balanced binary heap: the t

在java中,当对
PriorityQueue
对象调用
remove
方法时,堆头被移除。要将新的最小元素放在头部,是否对堆的其余部分执行了任何排序操作?例如,调用
remove
时是否调用了
compareTo
方法


抱歉,如果这在文档中,我在任何地方都找不到。提前感谢。

优先级队列实现为一个平衡的二进制堆,实现为一个数组。删除元素后,堆必须重新排序以保持堆的顺序

证据在评论中

/**
 * Priority queue represented as a balanced binary heap: the two
 * children of queue[n] are queue[2*n+1] and queue[2*(n+1)].  The
 * priority queue is ordered by comparator, or by the elements'
 * natural ordering, if comparator is null: For each node n in the
 * heap and each descendant d of n, n <= d.  The element with the
 * lowest value is in queue[0], assuming the queue is nonempty.
 */
private transient Object[] queue;
/**
*表示为平衡二进制堆的优先级队列:两个
*队列[n]的子级是队列[2*n+1]和队列[2*(n+1)]。这个
*优先级队列由比较器或元素的优先级排序
*自然排序,如果比较器为空:对于

*堆和n的每个后代d,n这取决于。如果要
删除
数组中支持
优先级队列的最后一个元素,则不会执行重新排序。如果您
删除
任何其他元素,它将对其元素重新排序(
siftUp
siftDown
):

公共布尔删除(对象o){
int i=indexOf(o);
如果(i==-1)
返回false;
否则{
removeAt(i);
返回true;
}
}
私人电子搬迁(国际一){
断言i>=0&&i
我只是查看了文档,没有对此进行任何说明。我很想听到答案!谢谢,很高兴知道我没有遗漏任何明显的东西!虽然我同意这是需要发生的事情,但文档中是否有任何东西可以证明这是实际发生的事情?@templatetypedef当它声明它是作为堆实现的时候,必须进行重新排序。否则,它不是堆。您可以浏览其余的源代码以了解实现细节。@templatetypedef是
remove(Object o)
的源代码,它实际上调用
indexOf(o)
removeAt(i)
,前者执行o(n)搜索,后者执行o(log(n))搜索筛选操作。@lcn-一个特定的
remove
实现不一定能保证所需的行为,但感谢您的链接!
public boolean remove(Object o) {
    int i = indexOf(o);
    if (i == -1)
        return false;
    else {
        removeAt(i);
        return true;
    }
}

private E removeAt(int i) {
    assert i >= 0 && i < size;
    modCount++;
    int s = --size;
    if (s == i) // removed last element
        queue[i] = null;
    else {
        E moved = (E) queue[s];
        queue[s] = null;
        siftDown(i, moved);
        if (queue[i] == moved) {
            siftUp(i, moved);
            if (queue[i] != moved)
                return moved;
        }
    }
    return null;
}