Created
April 24, 2021 13:59
-
-
Save hawaijar/1560fc1676cd6fef3382496515f68efc to your computer and use it in GitHub Desktop.
Implementing min-heap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Heap { | |
constructor() { | |
this.container = []; | |
} | |
getLeftChildIndex(parentIndex) { | |
return 2 * parentIndex + 1; | |
} | |
getRightChildIndex(parentIndex) { | |
return 2 * parentIndex + 2; | |
} | |
getParentIndex(childIndex) { | |
return Math.floor((childIndex - 1) / 2); | |
} | |
hasParent(childIndex) { | |
return this.getParentIndex(childIndex) >= 0; | |
} | |
hasLeftChild(parentIndex) { | |
return this.getLeftChildIndex(parentIndex) < this.container.length; | |
} | |
hasRightChild(parentIndex) { | |
return this.getRightChildIndex(parentIndex) < this.container.length; | |
} | |
hasChild(parentIndex) { | |
return this.hasLeftChild(parentIndex) || this.hasRightChild(parentIndex); | |
} | |
leftChild(parentIndex) { | |
return this.container[this.getLeftChildIndex(parentIndex)]; | |
} | |
rightChild(parentIndex) { | |
return this.container[this.getRightChildIndex(parentIndex)]; | |
} | |
bubbleUp(index = this.container.length - 1) { | |
let current = index; | |
while (this.hasParent(current)) { | |
const parentIndex = this.getParentIndex(current); | |
if (this.container[parentIndex] > this.container[current]) { | |
// swap the parent and child keys | |
const temp = this.container[parentIndex]; | |
this.container[parentIndex] = this.container[current]; | |
this.container[current] = temp; | |
current = parentIndex; | |
} else { | |
break; | |
} | |
} | |
} | |
bubbleDown(index = 0) { | |
let current = index; | |
while (this.hasChild(current)) { | |
let left = this.hasLeftChild(current) | |
? this.leftChild(current) | |
: Infinity; | |
let right = this.hasRightChild(current) | |
? this.rightChild(current) | |
: Infinity; | |
let smallest; | |
let isLeft = true; | |
if (left < right) { | |
smallest = left; | |
} else { | |
smallest = right; | |
isLeft = false; | |
} | |
if (this.container[current] > smallest) { | |
if (isLeft) { | |
let temp = this.container[current]; | |
this.container[current] = this.container[ | |
this.getLeftChildIndex(current) | |
]; | |
this.container[this.getLeftChildIndex(current)] = temp; | |
current = this.getLeftChildIndex(current); | |
} else { | |
let temp = this.container[current]; | |
this.container[current] = this.container[ | |
this.getRightChildIndex(current) | |
]; | |
this.container[this.getRightChildIndex(current)] = temp; | |
current = this.getRightChildIndex(current); | |
} | |
} else { | |
break; | |
} | |
} | |
} | |
insert(element) { | |
this.container.push(element); | |
this.bubbleUp(); | |
} | |
removeMin() { | |
if (this.container.length > 0) { | |
const min = this.container[0]; | |
this.container[0] = this.container[this.container.length - 1]; | |
this.container.length -= 1; // remove the last element | |
this.bubbleDown(); | |
return min; | |
} | |
return null; | |
} | |
getMin() { | |
if (this.container.length > 0) { | |
return this.container[0]; | |
} | |
return null; | |
} | |
build(array = []) { | |
for (let element of array) { | |
this.insert(element); | |
} | |
} | |
toString() { | |
return this.container; | |
} | |
} | |
const h = new Heap(); | |
const a = [1, 2, -3, 4, -5]; | |
h.build(a); | |
while (h.container.length) { | |
console.log(h.removeMin()); | |
} | |
// Output: [-5, -3, 1, 2, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment