|
1 | 1 | # Priority Queue |
2 | 2 |
|
3 | | -[](https://codecov.io/gh/davidnguyen179/priority-queue) [](https://travis-ci.org/davidnguyen179/priority-queue) [](https://github.com/davidnguyen179/priority-queue/pulls) [](https://github.com/davidnguyen179/priority-queue/blob/master/LICENSE) |
| 3 | +A priority queue is a collection in which items can be added at any time, but the only item that can be removed is the one with the highest priority. |
| 4 | + |
| 5 | +For more information, please check [wiki](https://en.wikipedia.org/wiki/Priority_queue). |
| 6 | + |
| 7 | +[](https://codecov.io/gh/davidnguyen179/priority-queue) [](https://travis-ci.org/davidnguyen179/priority-queue) [](https://github.com/davidnguyen179/priority-queue/pulls) [](https://github.com/davidnguyen179/priority-queue/blob/master/LICENSE) |
| 8 | + |
| 9 | +## Motivation |
| 10 | + |
| 11 | +During practising some challenges in `leetcode`. I found this [problem](https://leetcode.com/problems/last-stone-weight/) requires `priority queue`. So I decided to research some documentations online and try to implement by myself this lib. The result beats 97% in `leetcode`. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +The `priority queue` lib uses max heap as a default way to build a queue. |
| 16 | + |
| 17 | +**Max priority queue** |
| 18 | + |
| 19 | +_with array of numbers_ |
| 20 | + |
| 21 | +```ts |
| 22 | +const p = new PriorityQueue(); |
| 23 | +p.push(2); |
| 24 | +p.push(7); |
| 25 | +p.push(4); |
| 26 | +p.push(1); |
| 27 | +p.push(8); |
| 28 | +p.push(1); |
| 29 | + |
| 30 | +// The queue: [8, 7, 4, 1, 2, 1] |
| 31 | +``` |
| 32 | + |
| 33 | +_with array of objects_ |
| 34 | + |
| 35 | +```ts |
| 36 | +const p = new PriorityQueue(function (a, b) { |
| 37 | + return a.value < b.value; |
| 38 | +}); |
| 39 | + |
| 40 | +p.push({ text: "a", value: 2 }); |
| 41 | +p.push({ text: "b", value: 7 }); |
| 42 | +p.push({ text: "c", value: 4 }); |
| 43 | +p.push({ text: "d", value: 1 }); |
| 44 | +p.push({ text: "e", value: 8 }); |
| 45 | +p.push({ text: "f", value: 1 }); |
| 46 | + |
| 47 | +/** The queue |
| 48 | +[ |
| 49 | + { text: 'e', value: 8 }, |
| 50 | + { text: 'b', value: 7 }, |
| 51 | + { text: 'c', value: 4 }, |
| 52 | + { text: 'd', value: 1 }, |
| 53 | + { text: 'a', value: 2 }, |
| 54 | + { text: 'f', value: 1 }, |
| 55 | +] |
| 56 | + */ |
| 57 | +``` |
| 58 | + |
| 59 | +If you want to support min `priority queue`. The lib allows providing the customized `comparator`. |
| 60 | + |
| 61 | +**Min priority queue** |
| 62 | + |
| 63 | +_with array of numbers_ |
| 64 | + |
| 65 | +```ts |
| 66 | +const p = new PriorityQueue(function (a, b) { |
| 67 | + return a > b; |
| 68 | +}); |
| 69 | +p.push(2); |
| 70 | +p.push(7); |
| 71 | +p.push(4); |
| 72 | +p.push(1); |
| 73 | +p.push(8); |
| 74 | +p.push(1); |
| 75 | + |
| 76 | +// The queue: [1, 2, 1, 7, 8, 4] |
| 77 | +``` |
| 78 | + |
| 79 | +_with array of objects_ |
| 80 | + |
| 81 | +```ts |
| 82 | +const p = new PriorityQueue(function (a, b) { |
| 83 | + return a.value > b.value; |
| 84 | +}); |
| 85 | + |
| 86 | +p.push({ text: "a", value: 2 }); |
| 87 | +p.push({ text: "b", value: 7 }); |
| 88 | +p.push({ text: "c", value: 4 }); |
| 89 | +p.push({ text: "d", value: 1 }); |
| 90 | +p.push({ text: "e", value: 8 }); |
| 91 | +p.push({ text: "f", value: 1 }); |
| 92 | + |
| 93 | +/** The queue |
| 94 | +[ |
| 95 | + { text: 'd', value: 1 }, |
| 96 | + { text: 'a', value: 2 }, |
| 97 | + { text: 'f', value: 1 }, |
| 98 | + { text: 'b', value: 7 }, |
| 99 | + { text: 'e', value: 8 }, |
| 100 | + { text: 'c', value: 4 } |
| 101 | +] |
| 102 | + */ |
| 103 | +``` |
| 104 | + |
| 105 | +## API |
| 106 | + |
| 107 | +### push(value) |
| 108 | + |
| 109 | +Add elements to queue |
| 110 | + |
| 111 | +```ts |
| 112 | +const p = new PriorityQueue(); |
| 113 | +p.push(1); // adding "1" to queue |
| 114 | +p.push(2); // adding "2" to queue |
| 115 | +p.push(3); // adding "3" to queue |
| 116 | + |
| 117 | +// The queue: [3, 1, 2] |
| 118 | +``` |
| 119 | + |
| 120 | +### pop |
| 121 | + |
| 122 | +Extract the largest or smallest element from the queue |
| 123 | + |
| 124 | +```ts |
| 125 | +const p = new PriorityQueue(); |
| 126 | +p.push(1); // adding "1" to queue |
| 127 | +p.push(2); // adding "2" to queue |
| 128 | +p.push(3); // adding "3" to queue |
| 129 | + |
| 130 | +const elmenet = p.pop(); // Output: 3 |
| 131 | +``` |
| 132 | + |
| 133 | +The queue looks like this `[2, 1]` |
| 134 | + |
| 135 | +### top |
| 136 | + |
| 137 | +Peek the element (get the largest or smallest element without removing it from queue) |
| 138 | + |
| 139 | +```ts |
| 140 | +const p = new PriorityQueue(); |
| 141 | +p.push(1); // adding "1" to queue |
| 142 | +p.push(2); // adding "2" to queue |
| 143 | +p.push(3); // adding "3" to queue |
| 144 | + |
| 145 | +const elmenet = p.pop(); // Output: 3 |
| 146 | + |
| 147 | +// The queue is remained the same |
| 148 | +``` |
| 149 | + |
| 150 | +### size |
| 151 | + |
| 152 | +Get the size of the queue |
| 153 | + |
| 154 | +```ts |
| 155 | +const p = new PriorityQueue(); |
| 156 | +p.push(1); // adding "1" to queue |
| 157 | +p.push(2); // adding "2" to queue |
| 158 | +p.push(3); // adding "3" to queue |
| 159 | +p.push(4); // adding "4" to queue |
| 160 | + |
| 161 | +const length = p.size(); // Output: 4 |
| 162 | +``` |
| 163 | + |
| 164 | +### empty |
| 165 | + |
| 166 | +Check whether the queue is empty or not. |
| 167 | + |
| 168 | +- true: if the queue is empty |
| 169 | +- false: if the queue has data |
| 170 | + |
| 171 | +### toArray |
| 172 | + |
| 173 | +Extract queue to array |
| 174 | + |
| 175 | +```ts |
| 176 | +const p = new PriorityQueue(); |
| 177 | +p.push(1); // adding "1" to queue |
| 178 | +p.push(2); // adding "2" to queue |
| 179 | +p.push(3); // adding "3" to queue |
| 180 | + |
| 181 | +const array = p.toArray(); // Output: [3, 1, 2] |
| 182 | +``` |
| 183 | + |
| 184 | +## Running time |
| 185 | + |
| 186 | +| Operation | Binary heap | |
| 187 | +| --------- | ----------- | |
| 188 | +| push | O(lg n) | |
| 189 | +| top | O(1) | |
| 190 | +| pop | O(lg n) | |
0 commit comments