|
| 1 | +import { Compare, defaultCompare, ICompareFunction, reverseCompare, swap } from '../util'; |
| 2 | + |
| 3 | +export class MinHeap<T> { |
| 4 | + protected heap: T[] = []; |
| 5 | + |
| 6 | + constructor(protected compareFn: ICompareFunction<T> = defaultCompare) {} |
| 7 | + |
| 8 | + private getLeftIndex(index: number) { |
| 9 | + return 2 * index + 1; |
| 10 | + } |
| 11 | + |
| 12 | + private getRightIndex(index: number) { |
| 13 | + return 2 * index + 2; |
| 14 | + } |
| 15 | + |
| 16 | + private getParentIndex(index: number) { |
| 17 | + if (index === 0) { |
| 18 | + return undefined; |
| 19 | + } |
| 20 | + return Math.floor((index - 1) / 2); |
| 21 | + } |
| 22 | + |
| 23 | + size() { |
| 24 | + return this.heap.length; |
| 25 | + } |
| 26 | + |
| 27 | + isEmpty() { |
| 28 | + return this.size() <= 0; |
| 29 | + } |
| 30 | + |
| 31 | + clear() { |
| 32 | + this.heap = []; |
| 33 | + } |
| 34 | + |
| 35 | + findMinimum() { |
| 36 | + return this.isEmpty() ? undefined : this.heap[0]; |
| 37 | + } |
| 38 | + |
| 39 | + insert(value: T) { |
| 40 | + if (value != null) { |
| 41 | + const index = this.heap.length; |
| 42 | + this.heap.push(value); |
| 43 | + this.siftUp(index); |
| 44 | + return true; |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + private siftDown(index: number) { |
| 50 | + let element = index; |
| 51 | + const left = this.getLeftIndex(index); |
| 52 | + const right = this.getRightIndex(index); |
| 53 | + const size = this.size(); |
| 54 | + |
| 55 | + if (left < size && this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN) { |
| 56 | + element = left; |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + right < size && |
| 61 | + this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN |
| 62 | + ) { |
| 63 | + element = right; |
| 64 | + } |
| 65 | + |
| 66 | + if (index !== element) { |
| 67 | + swap(this.heap, index, element); |
| 68 | + this.siftDown(element); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private siftUp(index: number): void { |
| 73 | + let parent = this.getParentIndex(index); |
| 74 | + while (index > 0 && this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN) { |
| 75 | + swap(this.heap, parent, index); |
| 76 | + index = parent; |
| 77 | + parent = this.getParentIndex(index); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + extract() { |
| 82 | + if (this.isEmpty()) { |
| 83 | + return undefined; |
| 84 | + } |
| 85 | + if (this.size() === 1) { |
| 86 | + return this.heap.shift(); |
| 87 | + } |
| 88 | + const removedValue = this.heap.shift(); |
| 89 | + this.siftDown(0); |
| 90 | + return removedValue; |
| 91 | + } |
| 92 | + |
| 93 | + heapify(array: T[]) { |
| 94 | + if (array) { |
| 95 | + this.heap = array; |
| 96 | + } |
| 97 | + |
| 98 | + const maxIndex = Math.floor(this.size() / 2) - 1; |
| 99 | + |
| 100 | + for (let i = 0; i <= maxIndex; i++) { |
| 101 | + this.siftDown(i); |
| 102 | + } |
| 103 | + |
| 104 | + return this.heap; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +export class MaxHeap<T> extends MinHeap<T> { |
| 109 | + constructor(protected compareFn: ICompareFunction<T> = defaultCompare) { |
| 110 | + super(compareFn); |
| 111 | + this.compareFn = reverseCompare(compareFn); |
| 112 | + } |
| 113 | +} |
0 commit comments