Skip to content

Commit 87682a6

Browse files
author
tianqing.liang
committed
优先队列
1 parent 81609dd commit 87682a6

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

13-Priority-Queue/MaxHeap.dart

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* 最大堆
3+
*/
4+
class MaxHeap<E extends Comparable<E>>{
5+
6+
List? data ;
7+
8+
MaxHeap.withCapacity(int capacity){
9+
data = List.filled(capacity, E, growable: true);
10+
}
11+
12+
MaxHeap.withEmpty(){
13+
data = List.empty(growable: true);
14+
}
15+
16+
// 返回堆中的元素个数
17+
int? size(){
18+
return data!.length;
19+
}
20+
21+
// 返回一个布尔值, 表示堆中是否为空
22+
bool isEmpty(){
23+
return data!.isEmpty;
24+
}
25+
26+
int _parent(int index){
27+
if(index == 0){
28+
throw Exception("index-0 doesn't have parent.");
29+
}
30+
return ((index - 1) / 2).toInt();
31+
}
32+
33+
// 返回完全二叉树的数组表示中,一个索引所表示的元素的左孩子节点的索引
34+
int _leftChild(int index){
35+
return index * 2 + 1;
36+
}
37+
38+
// 返回完全二叉树的数组表示中,一个索引所表示的元素的右孩子节点的索引
39+
int _rightChild(int index){
40+
return index * 2 + 2;
41+
}
42+
43+
// 向堆中添加元素
44+
add(E e){
45+
data!.add(e);
46+
siftUp(data!.length - 1);
47+
}
48+
49+
siftUp(int k){
50+
while(k > 0 && data![_parent(k)].compareTo(data![k]) < 0 ){
51+
_swap(k, _parent(k));
52+
k = _parent(k);
53+
}
54+
}
55+
56+
// 看堆中的最大元素
57+
E findMax(){
58+
if(data!.length == 0)
59+
throw new Exception("Can not findMax when heap is empty.");
60+
return data![0];
61+
}
62+
63+
// 取出堆中最大元素
64+
E extractMax(){
65+
E ret = findMax();
66+
_swap(0, data!.length - 1);
67+
data!.removeLast();
68+
_siftDown(0);
69+
70+
return ret;
71+
}
72+
73+
_siftDown(int k){
74+
while(_leftChild(k) < data!.length){
75+
int j = _leftChild(k); // 在此轮循环中,data[k]和data[j]交换位置
76+
if( j + 1 < data!.length &&
77+
data![j+1].compareTo(data![j]) > 0 )
78+
j ++;
79+
// data[j] 是 leftChild 和 rightChild 中的最大值
80+
if(data![k].compareTo(data![j]) >= 0 )
81+
break;
82+
_swap(k, j);
83+
k = j;
84+
}
85+
}
86+
87+
// 取出堆中的最大元素,并且替换成元素e
88+
E replace(E e){
89+
90+
E ret = findMax();
91+
data![0] =e;;
92+
_siftDown(0);
93+
return ret;
94+
}
95+
96+
_swap(int i, int j){
97+
if(i < 0 || i >= data!.length || j < 0 || j >= data!.length)
98+
throw new Exception("Index is illegal.");
99+
100+
E t = data![i];
101+
data![i] = data![j];
102+
data![j] = t;
103+
}
104+
105+
}

13-Priority-Queue/PriorityQueue.dart

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'Queue.dart';
2+
import 'MaxHeap.dart';
3+
4+
class PriorityQueue<E extends Comparable<E>> implements Queue<E> {
5+
6+
MaxHeap<E>? maxHeap;
7+
8+
PriorityQueue(){
9+
maxHeap = new MaxHeap.withEmpty();
10+
}
11+
12+
@override
13+
int? getSize(){
14+
return maxHeap!.size();
15+
}
16+
17+
@override
18+
bool isEmpty(){
19+
return maxHeap!.isEmpty();
20+
}
21+
22+
@override
23+
E getFront(){
24+
return maxHeap!.findMax();
25+
}
26+
27+
@override
28+
enqueue(E e){
29+
maxHeap!.add(e);
30+
}
31+
32+
@override
33+
dequeue(){
34+
return maxHeap!.extractMax();
35+
}
36+
}

13-Priority-Queue/Queue.dart

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
abstract class Queue<T>{
2+
int? getSize();
3+
bool isEmpty();
4+
void enqueue(T e);
5+
T dequeue();
6+
T getFront();
7+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
10. 二分搜索树,二分搜索树搜索、移除最大最小值
1515
11. 集合 和 映射
1616
12.
17+
13. 优先队列
18+
14.
1719

1820
#### SDK版本
1921
1. 版本:2.12.3

0 commit comments

Comments
 (0)