Socket
Socket
Sign inDemoInstall

@datastructures-js/priority-queue

Package Overview
Dependencies
1
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @datastructures-js/priority-queue

priority queue implementation in javascript using a Min Heap


Version published
Maintainers
1
Install size
33.8 kB
Created

Changelog

Source

[3.0.1] - 2020-04-11

Fixed

  • jsdoc

Readme

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';

Construction

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.

params
nametype
elementobject
prioritynumber
runtime
O(log(n))
Example
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.

returndescription
objectobject literal with "priority" and "element" props
runtime
O(1)
Example
console.log(priorityQueue.front()); // { priority: 1, element: '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.

returndescription
objectobject literal with "priority" and "element" props
runtime
O(1)
Example
priorityQueue.enqueue('patient m', 4); // lowest priority
priorityQueue.enqueue('patient c', 4); // lowest priority
console.log(priorityQueue.back()); // { priority: 4, element: 'patient w' }

.dequeue()

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

returndescription
objectobject literal with "priority" and "element" props
runtime
O(log(n))
Example
console.log(priorityQueue.dequeue()); // { priority: 1, element: 'patient y' }
console.log(priorityQueue.front()); // { priority: 2, element: 'patient x' }

.isEmpty()

checks if the queue is empty.

return
boolean
runtime
O(1)
Example
console.log(priorityQueue.isEmpty()); // false

.size()

returns the number of elements in the queue.

return
number
runtime
O(1)
Example
console.log(priorityQueue.size()); // 5

.toArray()

returns a sorted array of elements by their priorities from highest to lowest.

returndescription
arrayan array of object literals with "priority" & "element" props
runtime
O(n*log(n))
Example
console.log(priorityQueue.toArray());
/*
[
  { priority: 2, element: 'patient x' },
  { priority: 3, element: 'patient z' },
  { priority: 4, element: 'patient c' },
  { priority: 4, element: 'patient w' },
  { priority: 4, element: 'patient m' }
]
*/

.clear()

clears all elements in the queue.

runtime
O(1)
Example
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

FAQs

Last updated on 11 Apr 2020

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc