min-max-heap
JavaScript implementations for a MinHeap and MaxHeap.
Test
yarn
yarn test
Installation
yarn add min-max-heap
Usage
You can import MinHeap
and/or MaxHeap
:
;
After that, you can initialise a heap and use the add
, peek
and pop
commands to manage its state:
// Initialise heapconst heap = ; // Add new value to the heapheap; heap; // Get root value of the heap without removing itheap // 2heap // 2 // Get root value of the heap and remove itheap // 2heap // 1
Examples
MinHeap example:
; const heap = ; heap;heap;heap;heap;heap; heap // 5heap // 5heap // 4heap // 3heap // 3
MaxHeap example:
; const heap = ; heap;heap;heap;heap;heap; heap // 1heap // 1heap // 2heap // 3heap // 3