|
| 1 | +class BinaryHeap { |
| 2 | + constructor(comparator = (a, b) => { |
| 3 | + return (a < b) ? -1 : (a === b ? 0 : 1); |
| 4 | + }) { |
| 5 | + this.heap = []; |
| 6 | + this.comparator = comparator; |
| 7 | + } |
| 8 | + getLeftIndex(index) { |
| 9 | + return 2 * index + 1; |
| 10 | + } |
| 11 | + getRightIndex(index) { |
| 12 | + return 2 * index + 2; |
| 13 | + } |
| 14 | + getParentIndex(index) { |
| 15 | + return Math.floor((index - 1) / 2); |
| 16 | + } |
| 17 | + insert(data) { |
| 18 | + if (data === undefined || data === null) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + this.heap.push(data); |
| 22 | + this.bubbleUp(this.heap.length - 1); |
| 23 | + return true; |
| 24 | + } |
| 25 | + bubbleUp(index) { |
| 26 | + while (index > 0) { |
| 27 | + let curr = this.heap[index]; |
| 28 | + let parentIndex = this.getParentIndex(index); |
| 29 | + let parent = this.heap[parentIndex]; |
| 30 | + |
| 31 | + let compare = this.comparator(parent, curr); |
| 32 | + if (compare < 0 || compare === 0) { |
| 33 | + break; |
| 34 | + } |
| 35 | + |
| 36 | + this.swap(index, parentIndex); |
| 37 | + index = parentIndex; |
| 38 | + } |
| 39 | + } |
| 40 | + extract() { |
| 41 | + if (this.size() === 0) { |
| 42 | + return undefined; |
| 43 | + } |
| 44 | + |
| 45 | + if (this.size() === 1) { |
| 46 | + return this.heap.shift(); |
| 47 | + } |
| 48 | + |
| 49 | + const value = this.heap[0]; |
| 50 | + this.heap[0] = this.heap.pop(); |
| 51 | + this.sinkDown(0); |
| 52 | + return value; |
| 53 | + } |
| 54 | + sinkDown(currIndex) { |
| 55 | + let left = this.getLeftIndex(currIndex); |
| 56 | + let right = this.getRightIndex(currIndex); |
| 57 | + let parentIndex = currIndex; |
| 58 | + |
| 59 | + if (this.comparator(this.heap[left], this.heap[parentIndex]) < 0) { |
| 60 | + parentIndex = left; |
| 61 | + } |
| 62 | + |
| 63 | + if (this.comparator(this.heap[right], this.heap[parentIndex]) < 0) { |
| 64 | + parentIndex = right; |
| 65 | + } |
| 66 | + |
| 67 | + if (parentIndex !== currIndex) { |
| 68 | + this.swap(parentIndex, currIndex); |
| 69 | + this.sinkDown(parentIndex); |
| 70 | + } |
| 71 | + } |
| 72 | + peak() { |
| 73 | + return this.size() > 0 ? this.heap[0] : undefined; |
| 74 | + } |
| 75 | + swap(a, b) { |
| 76 | + [this.heap[a], this.heap[b]] = [this.heap[b], this.heap[a]]; |
| 77 | + } |
| 78 | + size() { |
| 79 | + return this.heap.length; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +const assert = require('chai').assert; |
| 84 | + |
| 85 | +describe('BinaryHeap', () => { |
| 86 | + let minHeap; |
| 87 | + beforeEach(() => { |
| 88 | + minHeap = new BinaryHeap(); |
| 89 | + }); |
| 90 | + it('should defined', () => { |
| 91 | + assert.equal(typeof BinaryHeap, 'function'); |
| 92 | + }); |
| 93 | + describe('#insert', () => { |
| 94 | + it('should defined', () => { |
| 95 | + assert.equal(typeof minHeap.insert, 'function'); |
| 96 | + }); |
| 97 | + it('should insert value to the heap', () => { |
| 98 | + minHeap.insert(2); |
| 99 | + minHeap.insert(5); |
| 100 | + assert.deepEqual(minHeap.heap, [2, 5]); |
| 101 | + }); |
| 102 | + }); |
| 103 | + describe('#size', () => { |
| 104 | + it('should defined', () => { |
| 105 | + assert.equal(typeof minHeap.size, 'function'); |
| 106 | + }); |
| 107 | + it('should return the length of the heap', () => { |
| 108 | + assert.equal(minHeap.size(), 0); |
| 109 | + minHeap.insert(2); |
| 110 | + assert.equal(minHeap.size(), 1); |
| 111 | + }); |
| 112 | + }); |
| 113 | + describe('#peak', () => { |
| 114 | + it('should defined', () => { |
| 115 | + assert.equal(typeof minHeap.peak, 'function'); |
| 116 | + }); |
| 117 | + it('should return the minimum value', () => { |
| 118 | + minHeap.insert(4); |
| 119 | + minHeap.insert(7); |
| 120 | + minHeap.insert(1); |
| 121 | + minHeap.insert(9); |
| 122 | + assert.equal(minHeap.peak(), 1); |
| 123 | + }); |
| 124 | + it('should return the minimum value without deleting', () => { |
| 125 | + minHeap.insert(4); |
| 126 | + minHeap.insert(7); |
| 127 | + minHeap.insert(1); |
| 128 | + minHeap.insert(9); |
| 129 | + assert.equal(minHeap.size(), 4); |
| 130 | + minHeap.peak(); |
| 131 | + assert.equal(minHeap.size(), 4); |
| 132 | + }); |
| 133 | + }); |
| 134 | + describe('#extract', () => { |
| 135 | + it('should defined', () => { |
| 136 | + assert.equal(typeof minHeap.extract, 'function'); |
| 137 | + }); |
| 138 | + it('should return the minimum value', () => { |
| 139 | + minHeap.insert(4); |
| 140 | + minHeap.insert(7); |
| 141 | + minHeap.insert(1); |
| 142 | + minHeap.insert(9); |
| 143 | + assert.equal(minHeap.extract(), 1); |
| 144 | + }); |
| 145 | + it('should remove from the heap', () => { |
| 146 | + minHeap.insert(4); |
| 147 | + minHeap.insert(7); |
| 148 | + minHeap.insert(1); |
| 149 | + minHeap.insert(9); |
| 150 | + assert.equal(minHeap.size(), 4); |
| 151 | + minHeap.extract(); |
| 152 | + assert.equal(minHeap.size(), 3); |
| 153 | + }); |
| 154 | + it('should rearrange the heap', () => { |
| 155 | + minHeap.insert(4); |
| 156 | + minHeap.insert(7); |
| 157 | + minHeap.insert(1); |
| 158 | + minHeap.insert(9); |
| 159 | + assert.deepEqual(minHeap.heap, [1, 7, 4, 9]); |
| 160 | + minHeap.extract(); |
| 161 | + assert.deepEqual(minHeap.heap, [4, 7, 9]); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('MaxHeap', () => { |
| 166 | + it('should customize to max heap', () => { |
| 167 | + const max = (a, b) => b - a; |
| 168 | + const heap = new BinaryHeap(max); |
| 169 | + heap.insert(9); |
| 170 | + heap.insert(5); |
| 171 | + heap.insert(10); |
| 172 | + heap.insert(20); |
| 173 | + heap.insert(7); |
| 174 | + heap.insert(30); |
| 175 | + heap.insert(90); |
| 176 | + heap.insert(49); |
| 177 | + |
| 178 | + const sorted = [90, 49, 30, 20, 10, 9, 7, 5]; |
| 179 | + const output = []; |
| 180 | + |
| 181 | + while (heap.size()) { |
| 182 | + output.push(heap.extract()); |
| 183 | + } |
| 184 | + assert.deepEqual(output, sorted); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
| 188 | + |
0 commit comments