Concepts
Binary min-heap
Implicit complete binary tree satisfying parent <= child. Append and sift up; remove the root and sift down. Heap order supports a priority queue.
Formal Syntax & Structure
javascript syntaxjavascript
parent = Math.floor((i - 1)/2); left = 2*i + 1; right = left + 1Working Example
Example Snippetjavascript
const h=[];function push(x){h.push(x);let i=h.length-1; while(i>0){const p=Math.floor((i-1)/2);if(h[p]<=h[i])break;[h[p],h[i]]=[h[i],h[p]];i=p;}}for(const n of [8,3,5,1])push(n);console.log(JSON.stringify(h),h[0]);