Giter Site home page Giter Site logo

ignlg / heap-js Goto Github PK

View Code? Open in Web Editor NEW
113.0 1.0 8.0 2.21 MB

Efficient Binary heap (priority queue, binary tree) data structure for JavaScript / TypeScript. Includes JavaScript methods, Python's heapq module methods, and Java's PriorityQueue methods.

License: BSD 3-Clause "New" or "Revised" License

JavaScript 5.36% TypeScript 94.64%
javascript heap typescript nodejs data-structures priority-queue binary-heap binary-trees array-heap

heap-js's Introduction

Hi there ๐Ÿ‘‹

GitHub Stats

GitHub stats Top languages

Repositories

heap-js's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar ignlg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

heap-js's Issues

When the priorities are equal, shouldn't the FIFO principle be followed, but it's not

const Heap = require('heap-js').default

const comparator = (a, b) => {
    if (a.ttl > b.ttl) {
        return 1
    }
    if (a.ttl < b.ttl) {
        return -1
    }
    return 0
}

const equalSamples = [{ id: 1, ttl: 0 }, { id: 2, ttl: 0 }, { id: 3, ttl: 0 }, { id: 4, ttl: 0 }]
it('FIFO when equal compare', () => {
    const heap = new Heap(comparator)
    heap.push(...equalSamples)
    expect([heap.pop(), heap.pop(), heap.pop(), heap.pop()]).toEqual(equalSamples)
})

failed

Consider a sequence that is constantly push() and pop(). If FIFO is not followed, some tasks will never be pop() as long as the sequence is constantly push().

Fix and enhance CI

error [email protected]: The engine "node" is incompatible with this module. Expected version ">=6 <7 || >=8".

I think Node 7 can be removed and how about adding Node 10?

Documentation for `limit`

The documentation for limit is sparse, and I for some reason thought it would enforce the heap to keep the top N values. Instead, I'm not actually quite sure what it's doing, or what some practical purposes are for it? Some clarity here would be great, as sometimes my test results with limit returned "correctly" (but it was purely by chance).

simplify default compare functions

you can use (a, b) => a-b, (a, b) => b-a for the default minComparator and maxComparator function respectively.
Should be a bit faster than the if -> else if -> else

Issue popping when using custom comparator

Hi,

Thanks for the library! I've had great use of it in a lot of online coding interviews. :)

Here's a quick reproduction of a bug I found when using custom comparators:

const heap = new Heap((a, b) => a[1] - b[1]);
heap.push([1, 2]);
heap.push([6, 0]);
heap.push([2, 3]);
heap.pop();
// => TypeError: Cannot read property '1' of undefined

After some digging, I found that inside getPotentialParent sometimes calls this.compare with an undefined value. Adding a quick typeof this.heapArray[j] !== "undefined" && to the function solves the issue but I am not sure if this is the best way to handle it. I'd be happy to send a PR with tests if you think this is a good solution to the problem.

Documentation page shows 404 for Heap class documentation

Generated documentation generates a wrong link to Heap class documentation.

Current behavior

Heap menu item references /classes/Heap.html.

Expected behavior

Heap menu item must reference the generated file.

Proposed solution

Two options:

  • Heap menu item must reference /classes/heap.html.
  • Generated file must be renamed to /classes/Heap.html.

Iterator should not modify the heap (it should use nlargest(length) instead of pop)

Currently, this library implements the heap with the pop() method, which causes the heap to be emptied on a single iteration. For example:

import { Heap } from 'heap-js'

const heap = new Heap()
heap.push(10)
heap.push(5)
heap.push(15)
heap.push(3)

// prints 3, 5, 10, 15
for (const item of heap) {
  console.log(item)
}

// does not print anything
for (const item of heap) {
  console.log(item)
}

As it is not a standard behaviour of iterators to modify the underlying data structure, the sorted(nlargest(length)) method should be used instead of pop().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.