Skip to content

Commit 4c8e31e

Browse files
priorityQueue
1 parent 033a088 commit 4c8e31e

2 files changed

Lines changed: 116 additions & 1 deletion

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# 优先级队列
2+
3+
## 1. Priority Queue
4+
5+
>优先级队列(Priority Queue)也是队列
6+
>
7+
>- 普通队列按照`FIFO`原则,也就是先进先出
8+
>- 优先级队列按照`优先级高低`进行出队,比如将`优先级最高`的元素作为`队头`优先出队
9+
10+
基本接口和队列保持一样
11+
12+
```java
13+
int size(); // 元素的数量
14+
boolean isEmpty(); // 是否为空
15+
void enQueue(E element); // 入队
16+
E deQueue(); // 出队
17+
E front(); // 获取队列的头元素
18+
void clear(); // 清空
19+
```
20+
21+
## 2. 使用二叉堆实现优先级队列
22+
23+
实现比较简单
24+
25+
```java
26+
/**
27+
* 使用堆来实现优先级队列
28+
*
29+
* @author 梁峰源 <fengyuan-liang@foxmail.com>
30+
* @since 2023/6/13 12:40
31+
*/
32+
public class PriorityQueue<E> implements Queue<E> {
33+
34+
private final BinaryHeap<E> binaryHeap;
35+
36+
public PriorityQueue() {
37+
binaryHeap = new BinaryHeap<>();
38+
}
39+
40+
public PriorityQueue(Comparator<E> comparator) {
41+
binaryHeap = new BinaryHeap<>(comparator);
42+
}
43+
44+
45+
@Override
46+
public int size() {
47+
return binaryHeap.size();
48+
}
49+
50+
@Override
51+
public boolean isEmpty() {
52+
return size() == 0;
53+
}
54+
55+
@Override
56+
public void enQueue(E element) {
57+
binaryHeap.add(element);
58+
}
59+
60+
@Override
61+
public E deQueue() {
62+
return binaryHeap.remove();
63+
}
64+
65+
@Override
66+
public E front() {
67+
return binaryHeap.get();
68+
}
69+
70+
@Override
71+
public void clear() {
72+
binaryHeap.clear();
73+
}
74+
}
75+
76+
```
77+
78+
测试
79+
80+
```java
81+
@Test
82+
public void test01() {
83+
PriorityQueue<Person> priorityQueue = new PriorityQueue<>((p1, p2) -> p1.age - p2.age);
84+
priorityQueue.enQueue(new Person(22, "张三"));
85+
priorityQueue.enQueue(new Person(18, "李四"));
86+
priorityQueue.enQueue(new Person(25, "王五"));
87+
while (!priorityQueue.isEmpty()) {
88+
System.out.println(priorityQueue.deQueue());
89+
}
90+
}
91+
// 输出
92+
Person{age=25, name='王五'}
93+
Person{age=22, name='张三'}
94+
Person{age=18, name='李四'}
95+
```
96+
97+
## 3. jdk优先级队列源码分析
98+
99+
`java.util`包下也有一个优先级队列`PriorityQueue`,API如下:
100+
101+
![image-20230613212055889](https://cdn.fengxianhub.top/resources-master/202306132120166.png)
102+
103+
### 3.1offer方法源码分析
104+
105+
我们来看下如何给优先级队列添加元素
106+
107+
![image-20230613212250517](https://cdn.fengxianhub.top/resources-master/202306132122598.png)
108+
109+
可以看到里面最重要的方法就是我们堆操作的`siftUp`,在点进去看就是和我们一样的堆操作的逻辑了,jdk的优先级队列底层就是`二叉堆`
110+
111+
### 3.2 批量建堆源码
112+
113+
可以看到批量建堆的源码跟我们一样采用的也是`自下而上的下滤`
114+
115+
![image-20230613214737806](https://cdn.fengxianhub.top/resources-master/202306132147053.png)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120

121121
****
122122

123-
[二叉堆](/AlgorithmAndDataStructure/13-二叉堆.md)
123+
[二叉堆](/AlgorithmAndDataStructure/13-二叉堆.md) | [优先级队列](/AlgorithmAndDataStructure/14-优先级队列.md)
124124

125125
## 🎈 大数据
126126

0 commit comments

Comments
 (0)