@datastructures-js/heap
a complete javascript implementation for the Min/Max Heap data structures & Heap Sort algorithm.
Min Heap Max Heap
Table of Contents
installnpm install --save @datastructures-js/heap API requireconst { MinHeap, MaxHeap } = require ( '@datastructures-js/heap' ) ; importimport { MinHeap, MaxHeap } from '@datastructures-js/heap' ; Construction using "new"creates an empty heap.
Exampleconst minHeap = new MinHeap ( ) ;
const maxHeap = new MaxHeap ( ) ; using ".heapify(list)"converts an existing array to a heap.
params
name type item type
list array number , string or object literal with key/value props
return
MinHeap or MaxHeap
Exampleconst numList = [
50 ,
80 ,
{ key : 30 , value : 'something' } ,
90 ,
{ key : 60 , value : null } ,
40 ,
{ key : 20 , value : { name : 'test' } }
] ;
const strList = [
'm' ,
'x' ,
{ key : 'f' , value : 'something' } ,
'b' ,
{ key : 'z' , value : null } ,
'k' ,
{ key : 'c' , value : { name : 'test' } }
] ;
const minHeap = MinHeap. heapify ( numList) ;
const maxHeap = MaxHeap. heapify ( strList) ; .insert(key, value)insert a node into the heap.
params
name type
key number or string
value object
Exampleconst minHeap = new MinHeap ( ) ;
const maxHeap = new MaxHeap ( ) ;
minHeap. insert ( 50 ) ;
minHeap. insert ( 80 ) ;
minHeap. insert ( 30 , 'something' ) ;
minHeap. insert ( 90 ) ;
minHeap. insert ( 60 , null ) ;
minHeap. insert ( 40 ) ;
minHeap. insert ( 20 , { name : 'test' } ) ;
maxHeap. insert ( 'm' ) ;
maxHeap. insert ( 'x' ) ;
maxHeap. insert ( 'f' , 'something' ) ;
maxHeap. insert ( 'b' ) ;
maxHeap. insert ( 'z' , null ) ;
maxHeap. insert ( 'k' ) ;
maxHeap. insert ( 'c' , { name : 'test' } ) ; .root()returns the root without removing it.
Exampleconst min = minHeap. root ( ) ;
console. log ( min. getKey ( ) ) ;
console. log ( min. getValue ( ) ) ;
const max = maxHeap. root ( ) ;
console. log ( max. getKey ( ) ) ;
console. log ( max. getValue ( ) ) ; .leaf()returns the node with the max key in MinHeap, or with the min key in MaxHeap.
Exampleconst maxLeaf = minHeap. leaf ( ) ;
console. log ( maxLeaf. getKey ( ) ) ;
const minLeaf = maxHeap. leaf ( ) ;
console. log ( minLeaf. getKey ( ) ) ; removes and returns the root node in the heap.
Exampleconst min = minHeap. extractRoot ( ) ;
console. log ( min. getKey ( ) ) ;
console. log ( min. getValue ( ) ) ;
console. log ( minHeap. root ( ) . getKey ( ) ) ;
const max = maxHeap. extractRoot ( ) ;
console. log ( max. getKey ( ) ) ;
console. log ( max. getValue ( ) ) ;
console. log ( maxHeap. root ( ) . getKey ( ) ) ; .size()returns the number of nodes in the heap.
Exampleconsole. log ( minHeap. size ( ) ) ;
console. log ( maxHeap. size ( ) ) ; .clone()creates a shallow copy of a heap by slicing the nodes array and passing it to a new heap instance.
return
MinHeap or MaxHeap
Exampleconst minHeapClone = minHeap. clone ( ) ;
minHeapClone. extractRoot ( ) ;
console. log ( minHeapClone. root ( ) . getKey ( ) ) ;
console. log ( minHeap. root ( ) . getKey ( ) ) ; .sort()implements Heap Sort and sorts a Max Heap in ascneding order or a Min Heap in descending order .
return description
array
a sorted list by key of HeapNode
note : calling .sort() directly on a heap will mutate its nodes location. If you want to avoid that, you can sort a shallow copy of the heap.
Exampleconsole. log ( maxHeap. clone ( ) . sort ( ) ) ;
console. log ( maxHeap. root ( ) ) ;
console. log ( minHeap. clone ( ) . sort ( ) ) ;
console. log ( minHeap. root ( ) ) ; To sort a list of elements directtly using Heap Sort, it can be done like:
const unsortedList = [ 3 , 7 , 2 , 10 , 4 , 9 , 8 , 5 , 1 , 6 ] ;
const ascSorted = MaxHeap. heapify ( unsortedList) . sort ( ) . map ( ( node ) => node. getKey ( ) ) ;
const descSorted = MinHeap. heapify ( unsortedList) . sort ( ) . map ( ( node ) => node. getKey ( ) ) ;
.clear()clears the nodes in the heap.
ExampleminHeap. clear ( ) ;
maxHeap. clear ( ) ;
console. log ( minHeap. size ( ) ) ;
console. log ( minHeap. root ( ) ) ;
console. log ( maxHeap. size ( ) ) ;
console. log ( maxHeap. root ( ) ) ; HeapNode .getKey()returns the node's key.
.getValue()returns the node's associated value.
Buildlint + tests
grunt build LicenseThe MIT License. Full License is here