Skip to content

Instantly share code, notes, and snippets.

@linfongi
Last active June 20, 2021 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save linfongi/643681694d027f7afea689d864cdc242 to your computer and use it in GitHub Desktop.
Save linfongi/643681694d027f7afea689d864cdc242 to your computer and use it in GitHub Desktop.
function Heap(n=((n,t)=>n-t)){const t=[];return{push:function(e){t.push(e),function(){let e=t.length-1,o=e-1>>1;for(;0!==e&&n(t[e],t[o])<0;)[t[e],t[o]]=[t[o],t[e]],o=(e=o)-1>>1}()},pop:function(){const o=e(),u=t.pop();t.length>0&&(t[0]=u,function(){let e=0;for(;;){let o=e;for(const u of[2*e+1,2*e+2])u<t.length&&n(t[u],t[o])<0&&(o=u);if(o===e)return;[t[e],t[o]]=[t[o],t[e]],e=o}}());return o},peek:e,size:function(){return t.length}};function e(){return t[0]}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment