|
| 1 | +export default class MinHeap { |
| 2 | + constructor() { |
| 3 | + // Array representation of the heap. |
| 4 | + this.heapContainer = []; |
| 5 | + } |
| 6 | + |
| 7 | + static getLeftChildIndex(parentIndex) { |
| 8 | + return (2 * parentIndex) + 1; |
| 9 | + } |
| 10 | + |
| 11 | + static getRightChildIndex(parentIndex) { |
| 12 | + return (2 * parentIndex) + 2; |
| 13 | + } |
| 14 | + |
| 15 | + static getParentIndex(childIndex) { |
| 16 | + return Math.floor((childIndex - 1) / 2); |
| 17 | + } |
| 18 | + |
| 19 | + static hasParent(childIndex) { |
| 20 | + return this.getParentIndex(childIndex) >= 0; |
| 21 | + } |
| 22 | + |
| 23 | + hasLeftChild(parentIndex) { |
| 24 | + return MinHeap.getLeftChildIndex(parentIndex) < this.heapContainer.length; |
| 25 | + } |
| 26 | + |
| 27 | + hasRightChild(parentIndex) { |
| 28 | + return MinHeap.getRightChildIndex(parentIndex) < this.heapContainer.length; |
| 29 | + } |
| 30 | + |
| 31 | + leftChild(parentIndex) { |
| 32 | + return this.heapContainer[MinHeap.getLeftChildIndex(parentIndex)]; |
| 33 | + } |
| 34 | + |
| 35 | + rightChild(parentIndex) { |
| 36 | + return this.heapContainer[MinHeap.getRightChildIndex(parentIndex)]; |
| 37 | + } |
| 38 | + |
| 39 | + parent(childIndex) { |
| 40 | + return this.heapContainer[MinHeap.getParentIndex(childIndex)]; |
| 41 | + } |
| 42 | + |
| 43 | + swap(indexOne, indexTwo) { |
| 44 | + const tmp = this.heapContainer[indexTwo]; |
| 45 | + this.heapContainer[indexTwo] = this.heapContainer[indexOne]; |
| 46 | + this.heapContainer[indexOne] = tmp; |
| 47 | + } |
| 48 | + |
| 49 | + peek() { |
| 50 | + if (this.heapContainer.length === 0) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + return this.heapContainer[0]; |
| 55 | + } |
| 56 | + |
| 57 | + poll() { |
| 58 | + if (this.heapContainer.length === 0) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + if (this.heapContainer.length === 1) { |
| 63 | + return this.heapContainer.pop(); |
| 64 | + } |
| 65 | + |
| 66 | + const item = this.heapContainer[0]; |
| 67 | + |
| 68 | + // Move the last element from the end to the head. |
| 69 | + this.heapContainer[0] = this.heapContainer.pop(); |
| 70 | + this.heapifyDown(); |
| 71 | + |
| 72 | + return item; |
| 73 | + } |
| 74 | + |
| 75 | + add(item) { |
| 76 | + this.heapContainer.push(item); |
| 77 | + this.heapifyUp(); |
| 78 | + } |
| 79 | + |
| 80 | + heapifyUp() { |
| 81 | + let currentIndex = this.heapContainer.length - 1; |
| 82 | + |
| 83 | + while ( |
| 84 | + MinHeap.hasParent(currentIndex) && |
| 85 | + this.parent(currentIndex) > this.heapContainer[currentIndex] |
| 86 | + ) { |
| 87 | + this.swap(currentIndex, MinHeap.getParentIndex(currentIndex)); |
| 88 | + currentIndex = MinHeap.getParentIndex(currentIndex); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + heapifyDown() { |
| 93 | + let currentIndex = 0; |
| 94 | + let nextIndex = 0; |
| 95 | + |
| 96 | + while (this.hasLeftChild(currentIndex)) { |
| 97 | + if ( |
| 98 | + this.hasRightChild(currentIndex) && |
| 99 | + this.leftChild(currentIndex) > this.rightChild(currentIndex) |
| 100 | + ) { |
| 101 | + nextIndex = MinHeap.getRightChildIndex(currentIndex); |
| 102 | + } else { |
| 103 | + nextIndex = MinHeap.getLeftChildIndex(currentIndex); |
| 104 | + } |
| 105 | + |
| 106 | + if (this.heapContainer[currentIndex] < this.heapContainer[nextIndex]) { |
| 107 | + break; |
| 108 | + } |
| 109 | + |
| 110 | + this.swap(currentIndex, nextIndex); |
| 111 | + currentIndex = nextIndex; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + toString() { |
| 116 | + return this.heapContainer.toString(); |
| 117 | + } |
| 118 | +} |
0 commit comments