Socket
Socket
Sign inDemoInstall

binaryheap-array

Package Overview
Dependencies
145
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    binaryheap-array

Binary Heap Data Structure using an array like implementation, super fast and easy to use


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
9.24 MB
Created
Weekly downloads
 

Readme

Source

BinaryHeap

Binary Heap Data Structure using an array implementation

Istanbul Coverage Commitizen friendly Version License Downloads

Example

Import via NPM
var BinaryHeap = require("binaryheap-array");
|| Single element
var ch = new BinaryHeap();
ch.insert('T');
ch.insert('S');

// You can also chain inserts :)
ch.insert('R').insert('P');

// Removes the largest element first
ch.remove(); // 'T'

// You can also peak before you remove
ch.peak();    // 'S'
ch.remove();  // 'S'
|| Object
// Use the 'comparable' property to choose what you need to compare ahead of time
// In our example we want to compare the age
var list = new BinaryHeap({ comparable: function(elm) { return elm.age; } });
list.insert({ 'name': 'John', 'age': 25 });
list.insert({ 'name': 'Mike', 'age': 21 });
list.insert({ 'name': 'Aisha', 'age': 33 });
list.insert({ 'name': 'Sarah', 'age': 20 });

list.remove(); // { name: 'Aisha', age: 33 }
list.size(); // 3
list.remove(); // { name: 'John', age: 25 }
|| Priority Queue

Create a maximum or minimum priority queue on the fly

// Choose the order of the binaryheap ascending, or descending
var maximumPQ = new BinaryHeap({ order:'asc' });
var minimumPQ = new BinaryHeap({ order:'dec' });

API

MethodReturns TypeDescription
sizenumberThe size of the heap
insertobjectAdds an element to the heap
removeobjectRemoves the root element (could be the max or min based on the configuration setting)
printundefinedPrints the tree structure of the binary heap
peakobjectPeak on the root element, or the element that will get remove first

Setting

ObjectTypeDescription
orderstringThe order of the BinaryHeap either 'ascending' or 'descending'
comparablefunctionChoose what you need to compare, default to the inserted value see object example
dataarrayPass in the data as an array ahead of time and we will handle the insertion for you

O(n)

TypeWorstAverage
insertO(log n)O(log n)
removeO(log n)O(log n)
peakO(1)O(1)

Graph

This graph is a representation of the algorithm used in this module


               *-( (2 * i) + 1 )-˅
               *-( 2 * i )-˅     ˅
[ 'ø',  'T',  'S',  'R',  'P',  'N',  'O',  'A', ...]
  Empty  *------^     ^
         (2 * i)      ^
         *------------^
         (2 * i) + 1

Reach out

Feel free to reach out with feedback via github: issue, feature, bug, or enhancement inputs are greatly appreciated


↳ Links: NPM | GitHub

© MIT

Keywords

FAQs

Last updated on 02 Nov 2019

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