0.0.1 • Published 8 years ago
min-max-heap v0.0.1
min-max-heap
JavaScript implementations for a MinHeap and MaxHeap.
Test
yarn
yarn testInstallation
yarn add min-max-heapUsage
You can import MinHeap and/or MaxHeap:
import { MinHeap, MaxHeap } from 'min-max-heap';After that, you can initialise a heap and use the add, peek and pop commands to manage its state:
// Initialise heap
const heap = new MinHeap();
// Add new value to the heap
heap.add(1);
heap.add(2);
// Get root value of the heap without removing it
heap.peek() // 2
heap.peek() // 2
// Get root value of the heap and remove it
heap.pop() // 2
heap.pop() // 1Examples
MinHeap example:
import { MinHeap } from 'min-max-heap';
const heap = new MinHeap();
heap.add(1);
heap.add(2);
heap.add(3);
heap.add(4);
heap.add(5);
heap.peek() // 5
heap.pop() // 5
heap.pop() // 4
heap.peek() // 3
heap.pop() // 3MaxHeap example:
import { MaxHeap } from 'min-max-heap';
const heap = new MinHeap();
heap.add(1);
heap.add(2);
heap.add(3);
heap.add(4);
heap.add(5);
heap.peek() // 1
heap.pop() // 1
heap.pop() // 2
heap.peek() // 3
heap.pop() // 3