PriorityQueue

PriorityQueue是一个基于小顶堆的无界队列,非线程安全,内部元素无序,只有顶部存储着最小的元素;入队是堆的插入元素实现,出队是堆的删除元素实现。

主要属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class PriorityQueue<E> extends AbstractQueue<E>
implements java.io.Serializable {
private static final long serialVersionUID = -7720805057305804111L;
//默认容量
private static final int DEFAULT_INITIAL_CAPACITY = 11;
/**
* 存放数据的地方
* 优先队列由一个平衡二叉堆来实现:queue[n]的两个子节点分别为queue[2*n+1]和queue[2*n+2]
* 其队列中元素的顺序根据构造器传入的comparator保证或者元素的自然顺序
*/
transient Object[] queue; // non-private to simplify nested class access
/**
* 优先队列中元素个数
*/
private int size = 0;
/**
* 比较器 如果为空 则使用元素的自然顺序
*/
private final Comparator<? super E> comparator;
/**
* 修改次数
*/
transient int modCount = 0; // non-private to simplify nested class access

入队

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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;
}
/**
* 扩容操作
* 如果旧容量<64 则扩容翻倍
* 如果旧容量>=64 则扩容原来容量的一半
*/
private void grow(int minCapacity) {
int oldCapacity = queue.length;
// 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);
}
private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}
private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
//找到节点k的父节点
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;//找到最终的位置 放入数据
}

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;
}

出队

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}

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;
}

private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}
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) {
int child = (k << 1) + 1; // assume left child is least
Object c = queue[child];
int right = child + 1;
if (right < size &&
((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;
}
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;
}
坚持原创技术分享,您的支持将鼓励我继续创作!