Skip to content

Instantly share code, notes, and snippets.

@tpae
Last active April 8, 2024 14:14
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tpae/54ec7371f947505967a2036b9c002428 to your computer and use it in GitHub Desktop.
Save tpae/54ec7371f947505967a2036b9c002428 to your computer and use it in GitHub Desktop.
JavaScript implementation of Min Heap Data Structure
// Implement a min heap:
// -> insert, extract_min
// property:
// - elements are in ascending order
// - complete binary tree (node is smaller than it’s children)
// - root is the most minimum
// - insert takes O(logn) time
// - insert to the bottom right
// - bubble up until it meets requirements
// - extract_min takes O(logn) time
// - replace min with bottom right
// - bubble down until it meets requirements
function MinHeap() {
this.data = [];
}
MinHeap.prototype.insert = function(val) {
this.data.push(val);
this.bubbleUp(this.data.length-1);
};
MinHeap.prototype.bubbleUp = function(index) {
while (index > 0) {
// get the parent
var parent = Math.floor((index + 1) / 2) - 1;
// if parent is greater than child
if (this.data[parent] > this.data[index]) {
// swap
var temp = this.data[parent];
this.data[parent] = this.data[index];
this.data[index] = temp;
}
index = parent;
}
};
MinHeap.prototype.extractMin = function() {
if (this.data.length < 2) return this.data.pop();
var min = this.data[0];
// set first element to last element
this.data[0] = this.data.pop();
// call bubble down
this.bubbleDown(0);
return min;
};
MinHeap.prototype.bubbleDown = function(index) {
while (true) {
var child = (index+1)*2;
var sibling = child - 1;
var toSwap = null;
// if current is greater than child
if (this.data[index] > this.data[child]) {
toSwap = child;
}
// if sibling is smaller than child, but also smaller than current
if (this.data[index] > this.data[sibling] && (this.data[child] == null || (this.data[child] !== null && this.data[sibling] < this.data[child]))) {
toSwap = sibling;
}
// if we don't need to swap, then break.
if (toSwap == null) {
break;
}
var temp = this.data[toSwap];
this.data[toSwap] = this.data[index];
this.data[index] = temp;
index = toSwap;
}
};
var heap = new MinHeap();
heap.insert(5);
heap.insert(4);
heap.insert(8);
heap.insert(6);
heap.insert(1);
heap.insert(14);
heap.insert(2);
heap.insert(7);
console.log(heap.extractMin());
console.log(heap.extractMin());
console.log(heap.extractMin());
console.log(heap.extractMin());
@BigaDev
Copy link

BigaDev commented Jun 29, 2017

Also, you must break in the bubbleDown when this.data[toSwap] == null.
test your code with this example:
10 20 30 60 50 40
here in the second time to get the minimum your code not valid.

@blaskovicz
Copy link

@KrypticCoder
Copy link

This code does not work.

@zeeshan1112
Copy link

Created a gist for Max Heap in Javascript .

@savoygu
Copy link

savoygu commented Feb 20, 2021

image
the extractMin has some error!

@apokaliptis
Copy link

apokaliptis commented Apr 11, 2023

Adding if (this.data.length < 2) { return this.data.pop(); } to the beginning of extractMin allows the heap to properly empty. Otherwise, the last heap element will never be removed.

@ljx213101212
Copy link

This code doesn't work, for those people who needs one can check this out

https://github.com/datastructures-js/priority-queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment