Socket
Socket
Sign inDemoInstall

binaryheaps

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    binaryheaps

binary heap with fast increaseKey and decreaseKey operations


Version published
Maintainers
1
Install size
12.2 kB
Created

Readme

Source

A binary heap, suppose heap has n elements

isEmpty - O(1)
decreaseKey - O(log(n))
increaseKey - O(log(n))
insert - O(log(n))
delete - O(log(n))
from - O(n)
extract - O(log(n))
peek - O(1)

The heap is implemented internally as a binary tree represented by an array, and a map we maintain allows us O(1) lookup of elements. As such it has a greater memory overhead, and slightly lengthier insert, extract, etc. with the trade off of O(log(n)) delete, increase and decrease operations.

You can use it like so

const PriorityQueue = require("binaryheaps");

const arr = [2, 3, 1, 5, 6];
const cmp = (a, b) => a >= b ? 1 : -1;

const p = PriorityQueue.from(arr, cmp);

const largest = p.extract();

console.log(largest) // 6

In general you can either call new PriorityQueue(cmp) where cmp is a function (a: T, b: T) => number which returns 1 if a > b, 0 if a === b, else -1, where T is the type of the elements inserted into your queue.

Alternatively you can use the static from method as above which takes as its first argument an iterable.

Keywords

FAQs

Last updated on 08 Oct 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