|
| 1 | +/** |
| 2 | + * The minimalistic (ad hoc) version of a MinHeap data structure that doesn't have |
| 3 | + * external dependencies and that is easy to copy-paste and use during the |
| 4 | + * coding interview if allowed by the interviewer (since many data |
| 5 | + * structures in JS are missing). |
| 6 | + */ |
| 7 | +class MinHeapAdhoc { |
| 8 | + constructor(heap = []) { |
| 9 | + this.heap = []; |
| 10 | + heap.forEach(this.add); |
| 11 | + } |
| 12 | + |
| 13 | + add(num) { |
| 14 | + this.heap.push(num); |
| 15 | + this.heapifyUp(); |
| 16 | + } |
| 17 | + |
| 18 | + peek() { |
| 19 | + return this.heap[0]; |
| 20 | + } |
| 21 | + |
| 22 | + poll() { |
| 23 | + if (this.heap.length === 0) return undefined; |
| 24 | + const top = this.heap[0]; |
| 25 | + this.heap[0] = this.heap[this.heap.length - 1]; |
| 26 | + this.heap.pop(); |
| 27 | + this.heapifyDown(); |
| 28 | + return top; |
| 29 | + } |
| 30 | + |
| 31 | + isEmpty() { |
| 32 | + return this.heap.length === 0; |
| 33 | + } |
| 34 | + |
| 35 | + toString() { |
| 36 | + return this.heap.join(','); |
| 37 | + } |
| 38 | + |
| 39 | + heapifyUp() { |
| 40 | + let nodeIndex = this.heap.length - 1; |
| 41 | + while (nodeIndex > 0) { |
| 42 | + const parentIndex = this.getParentIndex(nodeIndex); |
| 43 | + if (this.heap[parentIndex] <= this.heap[nodeIndex]) break; |
| 44 | + this.swap(parentIndex, nodeIndex); |
| 45 | + nodeIndex = parentIndex; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + heapifyDown() { |
| 50 | + let nodeIndex = 0; |
| 51 | + |
| 52 | + while ( |
| 53 | + ( |
| 54 | + this.hasLeftChild(nodeIndex) |
| 55 | + && this.heap[nodeIndex] > this.leftChild(nodeIndex) |
| 56 | + ) |
| 57 | + || ( |
| 58 | + this.hasRightChild(nodeIndex) |
| 59 | + && this.heap[nodeIndex] > this.rightChild(nodeIndex) |
| 60 | + ) |
| 61 | + ) { |
| 62 | + const leftIndex = this.getLeftChildIndex(nodeIndex); |
| 63 | + const rightIndex = this.getRightChildIndex(nodeIndex); |
| 64 | + const left = this.leftChild(nodeIndex); |
| 65 | + const right = this.rightChild(nodeIndex); |
| 66 | + |
| 67 | + if (this.hasLeftChild(nodeIndex) && this.hasRightChild(nodeIndex)) { |
| 68 | + if (left <= right) { |
| 69 | + this.swap(leftIndex, nodeIndex); |
| 70 | + nodeIndex = leftIndex; |
| 71 | + } else { |
| 72 | + this.swap(rightIndex, nodeIndex); |
| 73 | + nodeIndex = rightIndex; |
| 74 | + } |
| 75 | + } else if (this.hasLeftChild(nodeIndex)) { |
| 76 | + this.swap(leftIndex, nodeIndex); |
| 77 | + nodeIndex = leftIndex; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + getLeftChildIndex(parentIndex) { |
| 83 | + return 2 * parentIndex + 1; |
| 84 | + } |
| 85 | + |
| 86 | + getRightChildIndex(parentIndex) { |
| 87 | + return 2 * parentIndex + 2; |
| 88 | + } |
| 89 | + |
| 90 | + getParentIndex(childIndex) { |
| 91 | + return Math.floor((childIndex - 1) / 2); |
| 92 | + } |
| 93 | + |
| 94 | + hasLeftChild(parentIndex) { |
| 95 | + return this.getLeftChildIndex(parentIndex) < this.heap.length; |
| 96 | + } |
| 97 | + |
| 98 | + hasRightChild(parentIndex) { |
| 99 | + return this.getRightChildIndex(parentIndex) < this.heap.length; |
| 100 | + } |
| 101 | + |
| 102 | + leftChild(parentIndex) { |
| 103 | + return this.heap[this.getLeftChildIndex(parentIndex)]; |
| 104 | + } |
| 105 | + |
| 106 | + rightChild(parentIndex) { |
| 107 | + return this.heap[this.getRightChildIndex(parentIndex)]; |
| 108 | + } |
| 109 | + |
| 110 | + swap(indexOne, indexTwo) { |
| 111 | + const tmp = this.heap[indexTwo]; |
| 112 | + this.heap[indexTwo] = this.heap[indexOne]; |
| 113 | + this.heap[indexOne] = tmp; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export default MinHeapAdhoc; |
0 commit comments