|
| 1 | +export abstract class Heap<T> { |
| 2 | + protected heap: T[]; |
| 3 | + |
| 4 | + constructor(elements: T[] = []) { |
| 5 | + this.heap = []; |
| 6 | + for (let element of elements) { |
| 7 | + this.insert(element); |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + /** |
| 12 | + * Compares the value at parentIndex with the value at childIndex |
| 13 | + * In a maxHeap the value at parentIndex should be larger than the value at childIndex |
| 14 | + * In a minHeap the value at parentIndex should be smaller than the value at childIndex |
| 15 | + * |
| 16 | + */ |
| 17 | + protected abstract isRightlyPlaced( |
| 18 | + childIndex: number, |
| 19 | + parentIndex: number |
| 20 | + ): boolean; |
| 21 | + |
| 22 | + /** |
| 23 | + * In a maxHeap the the index with the larger value in returned |
| 24 | + * In a maxHeap the the index with the larger value in returned |
| 25 | + */ |
| 26 | + protected abstract getChildIndexToSwap( |
| 27 | + leftChildIndex: number, |
| 28 | + rightChildIndex: number |
| 29 | + ): number; |
| 30 | + |
| 31 | + public insert(value: T): void { |
| 32 | + this.heap.push(value); |
| 33 | + this.bubbleUp(); |
| 34 | + } |
| 35 | + |
| 36 | + public extract(): T { |
| 37 | + let maxElement = this.heap[0]; |
| 38 | + this.heap[0] = this.heap[this.size() - 1]; |
| 39 | + this.heap.pop(); |
| 40 | + this.sinkDown(); |
| 41 | + return maxElement; |
| 42 | + } |
| 43 | + |
| 44 | + public size(): number { |
| 45 | + return this.heap.length; |
| 46 | + } |
| 47 | + |
| 48 | + public isEmpty(): boolean { |
| 49 | + return this.size() === 0; |
| 50 | + } |
| 51 | + |
| 52 | + protected bubbleUp(): void { |
| 53 | + let index = this.size() - 1; |
| 54 | + let parentIndex; |
| 55 | + |
| 56 | + while (index > 0) { |
| 57 | + parentIndex = Math.floor((index - 1) / 2); |
| 58 | + if (this.isRightlyPlaced(index, parentIndex)) break; |
| 59 | + [this.heap[parentIndex], this.heap[index]] = [ |
| 60 | + this.heap[index], |
| 61 | + this.heap[parentIndex], |
| 62 | + ]; |
| 63 | + index = parentIndex; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected sinkDown(): void { |
| 68 | + let index = 0; |
| 69 | + let leftChildIndex = this.getLeftChildIndex(index); |
| 70 | + let rightChildIndex = this.getRightChildIndex(index); |
| 71 | + let childIndexToSwap; |
| 72 | + |
| 73 | + while (this.heap[leftChildIndex] || this.heap[rightChildIndex]) { |
| 74 | + childIndexToSwap = this.getChildIndexToSwap( |
| 75 | + leftChildIndex, |
| 76 | + rightChildIndex |
| 77 | + ); |
| 78 | + if (this.isRightlyPlaced(childIndexToSwap, index)) break; |
| 79 | + [this.heap[childIndexToSwap], this.heap[index]] = [ |
| 80 | + this.heap[index], |
| 81 | + this.heap[childIndexToSwap], |
| 82 | + ]; |
| 83 | + index = childIndexToSwap; |
| 84 | + leftChildIndex = this.getLeftChildIndex(index); |
| 85 | + rightChildIndex = this.getRightChildIndex(index); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected getLeftChildIndex(index: number): number { |
| 90 | + return index * 2 + 1; |
| 91 | + } |
| 92 | + |
| 93 | + protected getRightChildIndex(index: number): number { |
| 94 | + return index * 2 + 2; |
| 95 | + } |
| 96 | + |
| 97 | + public check(): void { |
| 98 | + return this._check(); |
| 99 | + } |
| 100 | + |
| 101 | + protected _check(index: number = 0): void { |
| 102 | + if (!this.heap[index]) return; |
| 103 | + const leftChildIndex = this.getLeftChildIndex(index); |
| 104 | + const rightChildIndex = this.getRightChildIndex(index); |
| 105 | + |
| 106 | + if ( |
| 107 | + this.heap[leftChildIndex] && |
| 108 | + !this.isRightlyPlaced(leftChildIndex, index) |
| 109 | + ) |
| 110 | + throw new Error("Heap does not adhere to heap invariant"); |
| 111 | + |
| 112 | + if ( |
| 113 | + this.heap[rightChildIndex] && |
| 114 | + !this.isRightlyPlaced(rightChildIndex, index) |
| 115 | + ) |
| 116 | + throw new Error("Heap does not adhere to heap invariant"); |
| 117 | + |
| 118 | + this._check(leftChildIndex); |
| 119 | + this._check(rightChildIndex); |
| 120 | + } |
| 121 | +} |
0 commit comments