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

binary-heap.js

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binary-heap.js

A package for binary heap (heap tree) data structure

3.3.0
latest
Source
npm
Version published
Weekly downloads
24
500%
Maintainers
1
Weekly downloads
 
Created
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

data

FAQs

Package last updated on 09 Mar 2021

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