Socket
Socket
Sign inDemoInstall

binary-heap.js

Package Overview
Dependencies
0
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    binary-heap.js

A package for binary heap (heap tree) data structure


Version published
Maintainers
1
Created

Changelog

Source

3.3.0 (2021-03-09)

Documentation Changes
  • heap: update jsdocs for public methods (5b61e765)

Readme

Source

Binary heap

A package for general purpose binary heap data structure that can contain any type of data

Installation

npm i binary-heap.js

Constructor

ParameterTypeRequiredDefaultDescription
extractorfunctionfalsefunction that extracts the key used for sorting elements in the heap (should be equal to identity for primitive data types)

Methods

NameDescriptionReturn
InsertInsert a single element into the heapvoid
InsertManyInserts Many elements into the heapvoid
popPops the top element (maximum or minimum depending on the type of the heap)type T (type of elements inserted)
isEmptyChecks if the heap is empty or notboolean

Usage

const maxHeap = new MaxHeap( (x) =>x );
maxHeap.insert(3);
maxHeap.insertMany([5,7].values())

maxHeap.pop() // 7
maxHeap.pop() // 5
maxHeap.pop() // 3
maxHeap.pop() // undefined

const minHeap = new MinHeap( (x) =>x );

maxHeap.insert(5);
maxHeap.insertMany([3,7].values())

maxHeap.pop() // 3
maxHeap.pop() // 5
maxHeap.pop() // 7
maxHeap.pop() // undefined

Works with any data structure that implements the iteratable interface to provide it's elements

// Works with arrays, maps, sets
const arr = [1,2,3];
const set = new Set([1,2,3])
const map = new Map();
map.set(1,1)
map.set(2,2)
map.set(3,3);

const maxHeapOne = new MaxHeap((x) =>x);
maxHeapOne.insertMany(arr.values());

const maxHeapTwo = new MaxHeap((x) =>x);
maxHeapTwo.insertMany(set.values());

const maxHeapThree = new MaxHeap((x) =>x);
maxHeapThree.insertMany(map.keys().values());

Works with complex objects as long as you provide the right extractor

const arr = [{id:1},{id:2},{id:3}];

const maxHeap = new MaxHeap((x) =>x.id);
maxHeap.insertMany(arr.values());

Keywords

FAQs

Last updated on 09 Mar 2021

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