Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/can-dy-jack/priority-queue
Priority queue implementation in JavaScript
https://github.com/can-dy-jack/priority-queue
Last synced: 12 days ago
JSON representation
Priority queue implementation in JavaScript
- Host: GitHub
- URL: https://github.com/can-dy-jack/priority-queue
- Owner: can-dy-jack
- License: mit
- Created: 2023-01-14T04:57:13.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-02-14T10:48:34.000Z (over 1 year ago)
- Last Synced: 2024-10-11T01:51:31.533Z (28 days ago)
- Language: JavaScript
- Size: 35.2 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# priority-queue ![npm version](https://img.shields.io/npm/v/@kartjim/priority-queue?style=flat-square) ![Node.js CI](https://github.com/can-dy-jack/priority-queue/actions/workflows/test.yml/badge.svg)
> Priority queue(优先队列) implementation in JavaScript## install
```sh
npm i @kartjim/priority-queue
```## import
```js
import PriorityQueue from '@kartjim/priority-queue';
```or use CDN:
```js```
## use
```js
const data = [];
for (let i = 0; i < 50; i++) {
data.push(~~(50 * Math.random()));
}const sorted = data.slice().sort((a, b) => a - b);
const heap = new PriorityQueue();
for (let i = 0; i < data.length; i++) {
heap.push(data[i]);
}heap.peek() === sorted[0] // true
let ans = [];
while (!heap.isEmpty()) {
ans.push(heap.pop());
}ans == sorted // deeply equal
```## API
- constructor
- `constructor(data?: T[],compare?: (a: T, b: T) => number);`
- `push(val: T) : void;`
- push an element into PriorityQueue
- `peek() : T | undefined;`
- get the top element
- `pop() : T | undefined;`
- pop an element, and return it
- `isEmpty() : boolean;`
- check if the PriorityQueue is empty