Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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