项目作者: davidnguyen179

项目描述 :
Priority Queue in Typescript & Javascript
高级语言: TypeScript
项目地址: git://github.com/davidnguyen179/p-queue-ts.git
创建时间: 2020-04-14T02:41:23Z
项目社区:https://github.com/davidnguyen179/p-queue-ts

开源协议:MIT License

下载


Priority Queue

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.

For more information, please check wiki.

codecov Build Status PRs Welcome License: MIT

Motivation

During practising some challenges in leetcode. I found this problem 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.

Installation

npm

  1. npm i p-queue-ts

yarn

  1. yarn add p-queue-ts

Usage

The priority queue lib uses max heap as a default way to build a queue.

  1. import { PriorityQueue } from 'p-queue-ts';

or

  1. const { PriorityQueue } = require('p-queue-ts');

Max priority queue

with array of numbers

  1. const p = new PriorityQueue();
  2. p.push(2);
  3. p.push(7);
  4. p.push(4);
  5. p.push(1);
  6. p.push(8);
  7. p.push(1);
  8. // The queue: [8, 7, 4, 1, 2, 1]

with array of objects

  1. const p = new PriorityQueue(function (a, b) {
  2. return a.value < b.value;
  3. });
  4. p.push({ text: 'a', value: 2 });
  5. p.push({ text: 'b', value: 7 });
  6. p.push({ text: 'c', value: 4 });
  7. p.push({ text: 'd', value: 1 });
  8. p.push({ text: 'e', value: 8 });
  9. p.push({ text: 'f', value: 1 });
  10. /** The queue
  11. [
  12. { text: 'e', value: 8 },
  13. { text: 'b', value: 7 },
  14. { text: 'c', value: 4 },
  15. { text: 'd', value: 1 },
  16. { text: 'a', value: 2 },
  17. { text: 'f', value: 1 },
  18. ]
  19. */

If you want to support min priority queue. The lib allows providing the customized comparator.

Min priority queue

with array of numbers

  1. const p = new PriorityQueue(function (a, b) {
  2. return a > b;
  3. });
  4. p.push(2);
  5. p.push(7);
  6. p.push(4);
  7. p.push(1);
  8. p.push(8);
  9. p.push(1);
  10. // The queue: [1, 2, 1, 7, 8, 4]

with array of objects

  1. const p = new PriorityQueue(function (a, b) {
  2. return a.value > b.value;
  3. });
  4. p.push({ text: 'a', value: 2 });
  5. p.push({ text: 'b', value: 7 });
  6. p.push({ text: 'c', value: 4 });
  7. p.push({ text: 'd', value: 1 });
  8. p.push({ text: 'e', value: 8 });
  9. p.push({ text: 'f', value: 1 });
  10. /** The queue
  11. [
  12. { text: 'd', value: 1 },
  13. { text: 'a', value: 2 },
  14. { text: 'f', value: 1 },
  15. { text: 'b', value: 7 },
  16. { text: 'e', value: 8 },
  17. { text: 'c', value: 4 }
  18. ]
  19. */

API

push(value: T)

Add elements to queue

  1. const p = new PriorityQueue();
  2. p.push(1); // adding "1" to queue
  3. p.push(2); // adding "2" to queue
  4. p.push(3); // adding "3" to queue
  5. // The queue: [3, 1, 2]

pop()

Extract the largest or smallest element from the queue

  1. const p = new PriorityQueue();
  2. p.push(1); // adding "1" to queue
  3. p.push(2); // adding "2" to queue
  4. p.push(3); // adding "3" to queue
  5. const elmenet = p.pop(); // Output: 3

The queue looks like this [2, 1]

top()

Peek the element (get the largest or smallest element without removing it from queue)

  1. const p = new PriorityQueue();
  2. p.push(1); // adding "1" to queue
  3. p.push(2); // adding "2" to queue
  4. p.push(3); // adding "3" to queue
  5. const elmenet = p.pop(); // Output: 3
  6. // The queue is remained the same

size()

Get the size of the queue

  1. const p = new PriorityQueue();
  2. p.push(1); // adding "1" to queue
  3. p.push(2); // adding "2" to queue
  4. p.push(3); // adding "3" to queue
  5. p.push(4); // adding "4" to queue
  6. const length = p.size(); // Output: 4

empty()

Check whether the queue is empty or not.

  • true: if the queue is empty
  • false: if the queue has data

toArray()

Extract queue to array

  1. const p = new PriorityQueue();
  2. p.push(1); // adding "1" to queue
  3. p.push(2); // adding "2" to queue
  4. p.push(3); // adding "3" to queue
  5. const array = p.toArray(); // Output: [3, 1, 2]

clear()

Removes all of the elements from this priority queue.

contains(value: T, comparator?: (item: T) => boolean)

Returns true if this queue contains the specified element.

  • true: if the element exists in queue
  • false: if the element does not exist in queue

with array of numbers

  1. const p = new PriorityQueue();
  2. p.push(2);
  3. p.push(7);
  4. p.push(4);
  5. p.push(1);
  6. p.push(8);
  7. p.push(1);
  8. p.contains(8); // true
  9. p.contains(100); // false

with array of objects

  1. const p = new PriorityQueue(function (a, b) {
  2. return a.value > b.value;
  3. });
  4. p.push({ text: 'a', value: 2 });
  5. p.push({ text: 'b', value: 7 });
  6. p.push({ text: 'c', value: 4 });
  7. p.push({ text: 'd', value: 1 });
  8. p.push({ text: 'e', value: 8 });
  9. p.push({ text: 'f', value: 1 });
  10. function callback(item: any) {
  11. return item.value === element.value;
  12. }
  13. p.contains(8, callback); // true
  14. p.contains(100, callback); // false

Running time

You can check the performance of the package here: https://jsperf.com/p-queue-ts

Operation Binary heap
push O(lg n)
top O(1)
pop O(lg n)