@heap-data-structure/binomial-heap
Version:
Binomial heap data structures for JavaScript
3 lines (2 loc) • 5 kB
JavaScript
function t(t){const n=function(t,n,e,l){let i=l-n.length;for(;i-- >0;)n.push(null);const o=n.length;for(i=l;i<o&&null!==n[i];++i)e=e.merge(t,n[i]),n[i]=null;i===o&&n.push(null),n[i]=e},e=function(t,n,e){if(0===e.length)return;let l=e.length-n.length;for(;l-- >0;)n.push(null);let i=null;const o=n.length;for(l=0;l<o;++l)l>=e.length||null===e[l]?null!==i&&(null===n[l]?(n[l]=i,i=null):(i=i.merge(t,n[l]),n[l]=null)):null!==i?i=i.merge(t,e[l]):null!==n[l]?(i=n[l].merge(t,e[l]),n[l]=null):n[l]=e[l];null!==i&&n.push(i)},l=function(t,n,e,l){for(;e<l-1&&null===n[e];++e);let i=e,o=n[e].value;for(++e;e<l;++e){const l=n[e];if(null!==l){const n=l.value;t(n,o)<0&&(i=e,o=n)}}return i},i=function(t,n,l,i){const o=n[l].children;n[l]=null,h(null,o),l===i-1&&n.pop(),e(t,n,o)},o=function(t,n){const e=n.length,o=l(t,n,0,e),h=n[o];return i(t,n,o,e),h},h=function(t,n){const e=n.length;for(let l=0;l<e;++l)n[l].setparent(t)},r=function(t,n){const e=n.children;n.children=t.children,t.children=e;const l=n.rank();return t.children[l]=n,t.parent=n.parent,h(t,t.children),h(n,n.children),t.parent},s=function(t,n,e){!function(t,n){let e=n.parent;if(null!==e){for(;e=r(n,e),null!==e;)e.children[n.rank()]=n;t[n.rank()]=n}}(n,e),i(t,n,e.rank(),n.length),e.detach()},u=function(t){this.compare=t,this.length=0,this.list=[]};return u.prototype.head=function(){if(0===this.length)return;const t=l(this.compare,this.list,0,this.list.length);return this.list[t].value},u.prototype.headreference=function(){if(0===this.length)return null;const t=l(this.compare,this.list,0,this.list.length);return this.list[t]},u.prototype.pop=function(){if(0!==this.length)return--this.length,o(this.compare,this.list).value},u.prototype.popreference=function(){return 0===this.length?null:(--this.length,o(this.compare,this.list).detach())},u.prototype.push=function(n){const e=new t(n,[]);return this.pushreference(e),e},u.prototype.pushreference=function(t){++this.length,n(this.compare,this.list,t,0)},u.prototype.merge=function(t){return e(this.compare,this.list,t.list),this.length+=t.length,this},u.prototype.update=function(t,n){const e=this.compare(n,t.value);e<0?this.decreasekey(t,n):e>0?this.increasekey(t,n):t.value=n},u.prototype.decreasekey=function(t,n){!function(t,n,e,l){e.value=l;let i=e.parent;if(null!==i){for(;;){if(t(l,i.value)>=0)return;if(i=r(e,i),null===i)break;i.children[e.rank()]=e}n[e.rank()]=e}}(this.compare,this.list,t,n)},u.prototype.increasekey=function(t,e){s(this.compare,this.list,t),t.value=e,n(this.compare,this.list,t,0)},u.prototype.delete=function(t){--this.length,s(this.compare,this.list,t)},u}function n(t,n){this.value=t,this.children=n}function e(t,n){this.value=t,this.children=n,this.parent=null}function l(t,n){this.value=t,this.next=n}function i(){this.top=null,this.bottom=null}function o(t){const n=function(t,n,e){if(0===e.length)return;let l=e.length-n.length;for(;l-- >0;)n.push(null);let i=null;const o=n.length;for(l=0;l<o;++l)l>=e.length||null===e[l]?null!==i&&(null===n[l]?(n[l]=i,i=null):(i=i.merge(t,n[l]),n[l]=null)):null!==i?i=i.merge(t,e[l]):null!==n[l]?(i=n[l].merge(t,e[l]),n[l]=null):n[l]=e[l];null!==i&&n.push(i)},e=function(t){this.compare=t,this.length=0,this.list=[],this.lazy=new i};return e.prototype.pop=function(){if(0!==this.length)return--this.length,function(t,e,l){for(;!l.empty();)n(t,e,l.pop());const i=e.length;let o=0;for(;o<i-1&&null===e[o];++o);let h=o,r=e[o].value;for(++o;o<i;++o){const n=e[o];if(null!==n){const e=n.value;t(e,r)<0&&(h=o,r=e)}}const s=e[h].children;return e[h]=null,h===i-1&&e.pop(),l.push(s),r}(this.compare,this.list,this.lazy)},e.prototype.push=function(n){return++this.length,function(t,n,e){const l=[];let i=0;for(;i--;)l.push(null);l.push(n),t.push(l)}(this.lazy,new t(n,[]))},e.prototype.merge=function(t){return this.lazy.meld(t.lazy),this.length+=t.length,this},e}n.prototype.merge=function(t,n){return t(this.value,n.value)<=0?(this.children=this.children.concat(n),this):(n.children=n.children.concat(this),n)},n.prototype.detach=function(){return this.children.splice(0),this},n.prototype.setparent=function(t){},e.prototype.rank=function(){return this.children.length},e.prototype.merge=function(t,n){return t(this.value,n.value)<=0?(this.children=this.children.concat(n),n.parent=this,this):(n.children=n.children.concat(this),this.parent=n,n)},e.prototype.detach=function(){return this.children.splice(0),this.parent=null,this},e.prototype.setparent=function(t){this.parent=t},i.prototype.empty=function(){return null===this.top},i.prototype.push=function(t){this.top=new l(t,this.top),null===this.bottom&&(this.bottom=this.top)},i.prototype.pop=function(){const t=this.top.value;return this.top=this.top.next,null===this.top&&(this.bottom=null),t},i.prototype.meld=function(t){null===this.bottom?this.top=t.top:this.bottom.next=t.top,this.bottom=t.bottom};export{t as BinomialHeap,n as BinomialTree,e as BinomialTreeWithParent,o as LazyBinomialHeap,l as LazyNode,i as LazyStack};
//# sourceMappingURL=index.modern.js.map