🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@datastructures-js/priority-queue

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@datastructures-js/priority-queue

priority queue implementation in javascript using a Min Heap

2.0.0
Source
npm
Version published
Weekly downloads
186K
22.99%
Maintainers
1
Weekly downloads
 
Created
Source

@datastructures-js/priority-queue

build:? npm npm npm

A highly performant priority queue implementation using a Min Heap data structure.

Table of Contents

Install

npm install --save @datastructures-js/priority-queue

API

require

const PriorityQueue = require('@datastructures-js/priority-queue');

import

import PriorityQueue from '@datastructures-js/priority-queue';

Create a Priority Queue

const priorityQueue = new PriorityQueue();

.enqueue(element, priority)

adds an element with a priority (number) to the queue. The smaller the number, the higher the priority.

runtimeparams
O(log(n)) element: object

priority: number
priorityQueue.enqueue('patient y', 1); // highest priority
priorityQueue.enqueue('patient z', 3);
priorityQueue.enqueue('patient w', 4); // lowest priority
priorityQueue.enqueue('patient x', 2);

.front()

returns the element with highest priority in the queue.

runtimereturn
O(1)object
console.log(priorityQueue.front()); // patient y

.back()

returns an element with lowest priority in the queue. If multiple elements exist at the lowest priority, the one that was inserted first will be returned.

runtimereturn
O(1)object
priorityQueue.enqueue('patient m', 4); // lowest priority
priorityQueue.enqueue('patient c', 4); // lowest priority
console.log(priorityQueue.back()); // patient w

.dequeue()

removes and returns the element with highest priority in the queue.

runtimereturn
O(log(n))object
console.log(priorityQueue.dequeue()); // patient y
console.log(priorityQueue.front()); // patient x

.isEmpty()

checks if the queue is empty.

runtimereturn
O(1){boolean}
console.log(priorityQueue.isEmpty()); // false

.size()

returns the number of elements in the queue.

runtimereturn
O(1)number
console.log(priorityQueue.size()); // 5

.toArray()

returns an sorted array of elements from highest priority to lowest.

runtimereturn
O(n*log(n))array
console.log(priorityQueue.toArray()); // ['patient x', 'patient z', 'patient c', 'patient w', 'patient m']

.clear()

clears all elements in the queue.

runtime
O(1)
priorityQueue.clear();
console.log(priorityQueue.size()); // 0
console.log(priorityQueue.front()); // null
console.log(priorityQueue.dequeue()); // null

Build

grunt build

License

The MIT License. Full License is here

Keywords

queue

FAQs

Package last updated on 09 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts