Build a binary min-heap priority queue
Quick answer
A binary min-heap is a complete binary tree with every parent no larger than its children. Its root is a minimum, but the entire array is not sorted.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
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]);Expected console output:
[1,3,5,8] 1The newest small value rises to the root.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const h=[1,3,5,8,4];const min=h[0],last=h.pop();h[0]=last;let i=0;while(true){let k=i,l=2*i+1,r=l+1; if(l<h.length&&h[l]<h[k])k=l;if(r<h.length&&h[r]<h[k])k=r; if(k===i)break;[h[i],h[k]]=[h[k],h[i]];i=k;}console.log(min,JSON.stringify(h));Expected console output:
1 [3,4,5,8]The last value replaces the root, then swaps with the smaller child.
How it works and cost model
In a zero-based array, parent of i>0 is floor((i-1)/2); children are 2i+1 and 2i+2 when in bounds. Insert by appending then swapping upward while the parent is larger. Remove the minimum by saving the root, moving the last item to the root if nonempty, and sifting down toward the smaller child until the invariant holds. With the dynamic-array cost model, insertion and removal take O(log n) comparisons and swaps, peek O(1), storage O(n). Building bottom-up by sifting internal nodes down takes O(n) comparisons in the usual model. Equal priorities need an explicit tie-breaker if FIFO among ties matters.
Common mistakes
Compare both children during sift-down; moving toward the larger child can break heap order. Handle empty and singleton removal explicitly.
Practice
Insert priorities 8,3,5,1 into a min-heap and trace each swap. Repeatedly remove minimum, including an empty removal.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build a binary min-heap priority queue — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
Which property must hold at every parent of a min-heap?
Why select the smaller child during min-heap sift-down?
What can be read directly from a nonempty min-heap in constant indexed work?
What is the exact console output in the Binary min-heap example 1?
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]);What is the exact console output in the Binary min-heap example 2?
const h=[1,3,5,8,4];const min=h[0],last=h.pop();h[0]=last;let i=0;while(true){let k=i,l=2*i+1,r=l+1; if(l<h.length&&h[l]<h[k])k=l;if(r<h.length&&h[r]<h[k])k=r; if(k===i)break;[h[i],h[k]]=[h[k],h[i]];i=k;}console.log(min,JSON.stringify(h));