Skip to content

Instantly share code, notes, and snippets.

@trykhov
Created June 21, 2020 18:25
Show Gist options
  • Save trykhov/d3bcbcad14866b3efb151d2db44ddaf0 to your computer and use it in GitHub Desktop.
Save trykhov/d3bcbcad14866b3efb151d2db44ddaf0 to your computer and use it in GitHub Desktop.
// Assume heap is represented as an array list: heap = [...]
// Assume min-heap
function siftDown(idx) {
let leftChildIdx = (2 * idx) + 1;
let rightChildIdx = (2 * idx) + 2;
// base case
// make sure the heap has children
// it has to at least have a left child
if(leftChildIdx >= heap.length) return;
// start at the current node and find the new min-value among the three nodes (parent, left child, right child)
let node = heap[idx];
let minValueIdx = idx;
// check the left child
if(heap[leftChildIdx] !== null && node > heap[leftChildIdx]) {
node = heap[leftChildIdx];
minValueIdx = leftChildIdx;
}
// now check the right child
if(heap[rightChildIdx] !== null && node > heap[rightChildIdx]) {
node = heap[rightChildIdx];
minValueIdx = rightChildIdx;
}
// if the min-value is not the initial node, do the swap and then recursion
if(minValueIdx !== idx) {
let temp = heap[idx];
heap[idx] = heap[minValueIdx];
heap[minValueIdx] = temp;
siftDown(minValueIdx);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment