Skip to content

ngryman/ds-binary-heap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ds-binary-heap

A binary heap data structure in JavaScript.

Build Status Coverage


Big-O

    | Access | Search | Insertion  | Deletion

------- | ------ | ------ | ---------- | -------- Average | Θ(1) | Θ(n) | Θ(1) | Θ(log n) Worst | O(1) | O(n) | O(log n) | O(log n)

Install

yarn add ds-binary-heap

API

constructor

Create a new binary heap.

Parameters

  • scorer [Function] Function used to score an item.
  • comparer [Function] Function used to compare two items scores.

push

Push an item to the heap.

Parameters

  • item Any

Examples

heap.push(1)
heap.push('foo')
heap.push({ foo: 'bar' })

pop

Return the root of the heap and remove it.

Examples

heap.pop() // Hip-Hop! Ok...
// => 1

Returns Any item

peek

Return the root of the heap.

Examples

heap.peek()
// => 1

Returns Any item

clear

Remove all items form the heap.

Examples

heap.clear()

entries

Return an array containing all the items.

Examples

heap.entries()
// => [ 1, 'foo', { foo: 'bar' }]

Returns Array

inspect

Return a string representation of the list.

Parameters

Examples

list.inspect()
// => [ 1, 'foo', { foo: 'bar' }]

Returns String

length

Return the number of items in the list.

Examples

list.length()
// => 3

Returns Number

iterator

Iterate over the list.

Examples

for (let item of list) {
  console.log(item)
}
// => 1
// => 'foo'
// => { foo: 'bar' }

License

MIT © Nicolas Gryman

About

A binary heap data structure in JavaScript.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published