|
1 |
| -# heap |
2 |
| -A binary heap implementation in JavaScript ES6. |
| 1 | +# Heap |
| 2 | +A binary heap implementation in JavaScript ES6. |
| 3 | +It supports the basic heap operations and the creation of a heap from an existing array. |
| 4 | +This heap implementation uses a 0-based indexing. |
| 5 | + |
| 6 | +## Installation |
| 7 | +`$ npm install @gaetancovelli/heap` |
| 8 | + |
| 9 | +## Usage |
| 10 | +```javascript |
| 11 | +var Heap = require('@gaetancovelli/heap'); |
| 12 | +``` |
| 13 | + |
| 14 | +### Creation |
| 15 | +Creates an empty min heap |
| 16 | +```javascript |
| 17 | +var heap = new Heap(); |
| 18 | +``` |
| 19 | +Creates an empty max heap |
| 20 | +```javascript |
| 21 | +var heap = new Heap(Heap.MAX_PROPERTY); |
| 22 | +``` |
| 23 | +Creates a min heap from an existing array that will get heapified |
| 24 | +```javascript |
| 25 | +var array = [5, 4, 3, 2, 1]; |
| 26 | +var heap = new Heap(Heap.MIN_PROPERTY, array); |
| 27 | +``` |
| 28 | +Creates a max heap from an existing array that will get heapified |
| 29 | +```javascript |
| 30 | +var array = [1, 2, 3, 4, 5]; |
| 31 | +var heap = new Heap(Heap.MAX_PROPERTY, array); |
| 32 | +``` |
| 33 | + |
| 34 | +### Operations |
| 35 | +Push - Pushes a number to the heap. |
| 36 | +It can be a number or an array whose first element is the priority value and second element is the record we want to track. It has to be consistent for every elements of the heap. |
| 37 | +```javascript |
| 38 | +heap.push(1); // A value OR |
| 39 | +heap.push([1, 'My task with priority value 1']); // A record with a string OR |
| 40 | +heap.push([1, {firstname: 'Gaetan', lastname: 'Covelli'}]); // A record with an object |
| 41 | +``` |
| 42 | + |
| 43 | +Peek - Returns the top element of the heap |
| 44 | +```javascript |
| 45 | +heap.peek(); |
| 46 | +``` |
| 47 | + |
| 48 | +Pop - Pops and return the top element of the heap |
| 49 | +```javascript |
| 50 | +heap.pop() |
| 51 | +``` |
| 52 | + |
| 53 | +## Tests |
| 54 | +Tests are performed with mocha. |
| 55 | +To run the tests: |
| 56 | +`$ npm test` |
| 57 | + |
| 58 | +## Build |
| 59 | +The ES6 code is transpiled with Babel to dist/Heap.js |
| 60 | +To run babel and build the dist: |
| 61 | +`$ gulp babel` |
| 62 | + |
| 63 | +You can transpile automatically everytime the source file is saved by: |
| 64 | +`$ gulp dev` |
| 65 | + |
| 66 | +## License |
| 67 | +Copyright (c) 2017 Gaëtan Covelli. |
| 68 | +Released under the [MIT License](https://github.com/colbat/heap/blob/master/LICENSE) |
0 commit comments