UNPKG

@andrewrubin/min-heap

Version:

Min Heap data structure in JavaScript

4 lines (3 loc) 2.25 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t||self).minHeap=e()}(this,function(){var t=0;function e(e){return"__private_"+t+++"_"+e}function i(t,e){if(!Object.prototype.hasOwnProperty.call(t,e))throw new TypeError("attempted to use private field on non-instance");return t}var n=function(t){this.value=t},r=/*#__PURE__*/e("list"),o=/*#__PURE__*/e("compare"),s=/*#__PURE__*/e("parent"),h=/*#__PURE__*/e("leftChild"),f=/*#__PURE__*/e("rightChild"),u=/*#__PURE__*/e("heapifyUp"),a=/*#__PURE__*/e("heapifyDown");function p(t){return Math.floor(.5*(t-1))}function l(t){var e=2*t+1;return i(this,r)[r][e]&&e}function c(t){var e=2*t+2;return i(this,r)[r][e]&&e}function v(t){if(!(i(this,r)[r].length<2))for(;i(this,s)[s](t)>=0;){var e=i(this,s)[s](t),n=i(this,r)[r][t],h=i(this,r)[r][e];if(i(this,o)[o](h,n))break;var f=[n,h];h=f[0],n=f[1],i(this,r)[r][e]=h,i(this,r)[r][t]=n,t=e}}function d(t){if(!(i(this,r)[r].length<2))for(;i(this,h)[h](t);){var e=i(this,h)[h](t),n=i(this,f)[f](t),s=n&&i(this,o)[o](i(this,r)[r][n],i(this,r)[r][e])?n:e,u=i(this,r)[r][t],a=i(this,r)[r][s];if(i(this,o)[o](u,a))break;var p=[a,u];u=p[0],a=p[1],i(this,r)[r][t]=u,i(this,r)[r][s]=a,t=s}}/*#__PURE__*/ return function(){function t(t){Object.defineProperty(this,a,{value:d}),Object.defineProperty(this,u,{value:v}),Object.defineProperty(this,f,{value:c}),Object.defineProperty(this,h,{value:l}),Object.defineProperty(this,s,{value:p}),Object.defineProperty(this,r,{writable:!0,value:[]}),Object.defineProperty(this,o,{writable:!0,value:function(t,e){if(!t instanceof n||!e instanceof n)throw"Comparison argument must be an instance of HeapNode.";return e.value>t.value}}),t&&"function"==typeof t&&(i(this,o)[o]=t)}var e=t.prototype;return e.peek=function(){return i(this,r)[r][0].value},e.pull=function(){if(0!==i(this,r)[r].length){var t=i(this,r)[r][0],e=i(this,r)[r].pop();return 0!==i(this,r)[r].length&&(i(this,r)[r][0]=e,i(this,a)[a](0)),t.value}},e.insert=function(t){var e=i(this,r)[r].push(new n(t));return i(this,u)[u](e-1),t},e.getList=function(){return i(this,r)[r]},e.isEmpty=function(){return 0===i(this,r)[r].length},t}()}); //# sourceMappingURL=min-heap.umd.js.map