public class PriorityQueue<E> extends AbstractQueue<E>
implements java.io.Serializable {
/**
* 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.
*/
transient Object[] queue; // non-private to simplify nested class access
/**
* The number of elements in the priority queue.
* -- 当前队列中的元素个数
*/
private int size = 0;
/**
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/
private final Comparator<? super E> comparator;
/**
* The number of times this priority queue has been
* <i>structurally modified</i>. See AbstractList for gory details.
*/
transient int modCount = 0; // non-private to simplify nested class access
}
public boolean add(E e) {
return offer(e);
}
public boolean offer(E e) {
if (e == null) // -- 不允许放入空元素
throw new NullPointerException();
modCount++;
int i = size;
if (i >= queue.length) // -- 若队列中元素已满,则扩容
grow(i + 1);
size = i + 1;
if (i == 0) // -- 若队列为空,则插入为第一个元素
queue[0] = e;
else
siftUp(i, e);
return true;
}
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// -- 若size小于64,则增大100%, 否则增大50%
// Double size if small; else grow by 50%
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
(oldCapacity + 2) :
(oldCapacity >> 1));
// overflow-conscious code
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// -- 简单地把原数组的内容完全拷过去
queue = Arrays.copyOf(queue, newCapacity);
}
// -- k: 准备插入的位置, x: 插入的元素
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
@SuppressWarnings("unchecked")
private void siftUpComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>) x;
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (key.compareTo((E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = key;
}
@SuppressWarnings("unchecked")
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = x;
}
siftUpUsingComparator
或siftUpComparable
,这两个函数内部逻辑基本相同(k-1)/2
)parent, 比较父亲节点parent
和准备插入点x
的权值parent小于x
,则当前 k 为合适的插入位置,退出循环public E peek() {
return (size == 0) ? null : (E) queue[0];
}
public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
E result = (E) queue[0]; // -- 队尾元素
E x = (E) queue[s]; // -- 队首元素
queue[s] = null; // -- 删除队尾元素
if (s != 0) // -- 若队列不为空
siftDown(0, x); // -- 将队尾元素插入到队首,并调整堆的结构
return result;
}
从 k 指定的位置开始,将 x 逐层向下与当前点的左右孩子中较小的那个交换,直到 x 小于或等于左右孩子中的任何一个为止
private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
@SuppressWarnings("unchecked")
private void siftDownComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>)x;
int half = size >>> 1; // loop while a non-leaf
while (k < half) { // -- 若大于half则为叶子节点,没有sift down的必要
int child = (k << 1) + 1; // assume left child is least
Object c = queue[child];
int right = child + 1;
if (right < size && // -- 若右儿子存在且小于左儿子,则将child赋值为右儿子
((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
c = queue[child = right];
if (key.compareTo((E) c) <= 0) // 若待插入元素小于左右两个儿子节点,退出循环
break;
queue[k] = c; // 将较小的儿子节点上提
k = child; // 待插入位置修改
}
queue[k] = key;
}
@SuppressWarnings("unchecked")
private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = queue[child];
int right = child + 1;
if (right < size &&
comparator.compare((E) c, (E) queue[right]) > 0)
c = queue[child = right];
if (comparator.compare(x, (E) c) <= 0)
break;
queue[k] = c;
k = child;
}
queue[k] = x;
}
siftDownComparable
或siftDownUsingComparator
,这两个函数内部逻辑基本相同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); // -- 将最后一个元素插入到被删除的位置,试着做siftDown
if (queue[i] == moved) { // -- 若它没有被siftDown,则可能是需要做siftUp
siftUp(i, moved);
if (queue[i] != moved)
return moved;
}
}
return null;
}