1 | # 优先队列 PriorityQueue\<T>
|
2 |
|
3 | 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。
|
4 |
|
5 | ## 基本操作的API及示例
|
6 |
|
7 | ### 查看头部 peek
|
8 | ##### peek(): T
|
9 |
|
10 | ```text
|
11 | 示例:
|
12 | const queue = new PriorityQueue();
|
13 | const head = queue.peek();
|
14 | ```
|
15 |
|
16 | ### 入队 enqueue
|
17 | ##### enqueue(value: T,prioriry: number): PriorityQueue
|
18 |
|
19 | ```text
|
20 | 示例:
|
21 | const queue = new PriorityQueue();
|
22 | queue.enqueue(1,1);
|
23 | queue.enqueue(2,3);
|
24 | ```
|
25 |
|
26 | ### 出队 dequeue
|
27 | ##### dequeue(): PriorityQueueNode\<T>
|
28 |
|
29 | ```text
|
30 | 示例:
|
31 | const queue = new PriorityQueue();
|
32 | queue.enqueue(1,1);
|
33 | queue.enqueue(2,3);
|
34 | const node = queue.dequeue();
|
35 | console.log(node.value);
|
36 | // 2
|
37 | ```
|
38 |
|
39 | ### 调整优先级 changePriority
|
40 | ##### changePriority(value: T, newPriority: number): void
|
41 |
|
42 | ```text
|
43 | 示例:
|
44 | let queue = new PriorityQueue();
|
45 | queue.enqueue(1,1);
|
46 | queue.enqueue(2,2);
|
47 | queue.changePriority(1,3);
|
48 | ```
|
49 |
|
50 | ### 清空队列 clear
|
51 | ##### clear(): void
|
52 |
|
53 | ```text
|
54 | 示例:
|
55 | let queue = new PriorityQueue();
|
56 | queue.enqueue(1,1);
|
57 | queue.enqueue(2,2);
|
58 | queue.clear();
|
59 | console.log(queue.isEmpty())
|
60 | // true
|
61 | ```
|
62 |
|
63 | ### 是否为空 isEmpty
|
64 | ##### isEmpty(): boolean
|
65 |
|
66 | ```text
|
67 | isEmpty()
|
68 | 示例:
|
69 | const queue = new PriorityQueue();
|
70 | queue.enqueue(1,1);
|
71 | queue.enqueue(2,2);
|
72 | let isEmpty = queue.isEmpty();
|
73 | console.log(isEmpty);
|
74 | // false
|
75 | queue.clear();
|
76 | isEmpty = queue.isEmpty();
|
77 | console.log(isEmpty);
|
78 | // true
|
79 | ```
|
80 |
|
81 | ### 是否存在 has
|
82 | ##### has(value: T): boolean
|
83 |
|
84 | ```text
|
85 | 示例:
|
86 | const queue = new PriorityQueue();
|
87 | queue.enqueue(1,1);
|
88 | queue.enqueue(2,2);
|
89 | let exist = queue.has(2);
|
90 | console.log(true);
|
91 | // true
|
92 | exist = queue.has(3);
|
93 | console.log(exist);
|
94 | // false
|
95 | ``` |
\ | No newline at end of file |