npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

binaryheap-resizable

v1.0.0

Published

binary heap implemented over a resizable array with multiple ways of handling predicates

Downloads

889

Readme

npm status build status dependency status experimental

Resizable Binary Heap


Installation

npm install binaryheap-resizable

Testing

npm test

Simple usage

var maxHeap = BinaryHeap([5, 3, 2]).maxHeap();
    or
var maxHeap = BinaryHeap([5, 3, 2]).predicate().greater();
// set it up as a simple max-heap
// its initial length is 3 and its initial data is the maxHeapified
// version of the passed array

var minObjectHeap = BinaryHeap(4).minHeap('priority');
    or
var minObjectHeap = BinaryHeap(4).predicate().lesser('priority');
// set it up as a object min-heap of initial length equals to 4
// compare objects from the heap by their priority property
// when inserted/pulled, throw an error if property does not exist

API

Constructors
  1. constructor(initial-length) - build an empty heap with initial-length size.
  2. constructor(array) - heapify array. initial length is set to array.length.
Predicates
  1. predicate().greater([ property [, default]]) - set heap as max-heap. if property is set, elements property values will be used when comparing. if this value is not accessible and default is set, default will be used instead.
  2. predicate().lesser([ property [, default]]) - same as above, but it will set heap as min-heap.
  3. predicate(fn).value([ property [, default]]) - same as above, but it will use fn as the predicate
  4. maxHeap() - alias for predicate().greater()
  5. minHeap() - alias for predicate().lesser()
Data management methods
  1. insert(elements..), insert(array-of-elements) - insert all passed elements in the heap, doing reallocations if necessary. returns new heap size. alias push().
  2. peek() - return the root element of the heap or null if heap is empty.
  3. pull() - peek and remove the peeked element from the heap if its not null. alias pop()
  4. resizeFactor([factor]) - get/set how much the heap buffer will expand/shrink when needed. default is 2.
  5. resize(n) - manually resize the heap buffer to n.
  6. toArray() - return an array representation of the heap
Properties
  1. size - store how many elements are on the heap.
  2. length - get the length of the heap buffer.

What package is that?

Exactly what the package name says it is. It's a resizable binary heap.

What do you exactly mean by resizable?

With binaryheap-resizable you can pre-allocate an array space for your binary heap with no worrying about when it's gonna be fulfilled. It expands and shrink itself automatically as elements are inserted to/pulled from the heap. You can even resize it by yourself using the resize() member method!

But what? I don't want my application to be re-allocating memory for a heap all the time!

That's why binaryheap-resizable got a resizeFactor property. Every time the heap data array is expanded, it's expanded to BinaryHeap.length * resizaFactor. You specify how much your array will be expanded/shrinked, so depending on your array initial size and on your demands you can control how often the application will be re-allocating memory to the heap. resizeFactor default value is 2.

Ok. Since it's a binary heap, how can I control the ordering criteria?

The heap constructor has an argument called predicate. You can set the binary heap predicate by passing a function fn(a, b) as that argument, where a is the parent element and b is its child. By default binaryheap-resizable has a predicate set to a max-heap predicate.

var predicate = function(a, b){
    return a > b;
}

So we have a pseudo-code which:

predicate(top-element, child-n) is truthy for n 1..top-element-child-count

So in version 0.0.7 we introduced new ways of handling predicates. We will be discussing about them right below.

Multiple ways of handling predicates

>= 0.0.7

In older versions we had only the basic way of dealing with predicates. It was really hard to deal with errors and to keep things going smooth when going through non-existent objects. Version 0.0.7 introduced major changes related to the way predicates were set up. It's important to say that projects that were built on top of older versions are still perfectly compatible with this new version.

Default method, in constructor

The old version way is still available but we highly recommend you to avoid it for non-number objects. Here's the min-heap predicate example.

var predicate = function(a, b){
    return a < b;
}

var heap = new BinaryHeap(4, predicate);
// create a min-heap with initial capacity of 4

This method can not handle object properties very well.

Deep finding way

In 0.0.7 the functional way of handling predicates was introduced. It's now simple to define your predicate and control the behavior of your application when dealing with deep objects. Let's say you want to order a max-heap by their priority properties. In the old fashioned way you would do something like this:

// old deprecated way
var predicate = function(a, b){
    return a.priority > b.priority;
}

var heap = new BinaryHeap(4, predicate);
// you will probably have problems dealing with non-existent objects or null properties

You could avoid the problems by rewriting your predicate code to check for these problems before doing the real job. But it's just awful. Then we introduce you a clean way of dealing with these issues.

// a and b are now real values
var predicate = function(a, b){
    return a < b;
}

// BinaryHeap(capacity).predicate(predicate).value([path [, default-value]])
// value() function returns a BinaryHeap object reference that can be used

var heap1 = BinaryHeap(4).predicate(predicate).value('priority');
var heap2 = BinaryHeap(4).predicate(predicate).value('priority', 1);
// heap1 inserts and pulls may throw an error when priority property does not exist
// heap2 inserts and pulls will set a default value to be compared when priority property does not exist

This code does basically what the deprecated code does, but you can define a solid behavior when dealing with properties passing a path argument to deep find that property, or even pass a default-value argument to be set as default value when a property does not exist, avoiding access errors.

When comparing it does something like this: (pseudo-code):

// heap1
a.priority < b.priority or throw an error if property priority does not exist
// heap2
a.priority < b.priority if property priority exists in both objects
    or
1 < b.priority if property priority exists only in b object
    or
a.priority < 1 if property priority exists only in a object
    or
1 < 1 if property priority does not exist in both objects
Preset-based way

The preset-based way is just what we described above, passing no arguments to predicate and using greater() and lesser() in place of value(). Using this we can quickly build max/min-heaps from raw numbers or numbers from deep objects properties.

var heap1 = BinaryHeap(4).predicate().greater('priority', 1);
// max-heap comparing (a.priority or 1) > (b.priority or 1)
var heap2 = BinaryHeap(4).predicate().lesser('info.relevance', 2);
// min-heap comparing (a.info.relevance or 2) < (b.info.relevance or 2)

We can also use the aliases to predicate().greater() and predicate().lesser()

var heap1 = BinaryHeap(4).maxHeap('priority', 1);
// max-heap comparing (a.priority or 1) > (b.priority or 1)
var heap2 = BinaryHeap(4).minHeap('info.relevance', 2);
// min-heap comparing (a.info.relevance or 2) < (b.info.relevance or 2)