|
| 1 | +import Comparator from '../../utils/comparator/Comparator'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Parent class for heaps |
| 5 | + * @class |
| 6 | + */ |
| 7 | +class Heap { |
| 8 | + /** |
| 9 | + * @constructs Heap |
| 10 | + * @param {Function} [comparatorFunction] |
| 11 | + */ |
| 12 | + constructor(comparatorFunction) { |
| 13 | + // Array representation of the heap. |
| 14 | + this.heapContainer = []; |
| 15 | + this.compare = new Comparator(comparatorFunction); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @param {number} parentIndex |
| 20 | + * @return {number} |
| 21 | + */ |
| 22 | + getLeftChildIndex(parentIndex) { |
| 23 | + return (2 * parentIndex) + 1; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @param {number} parentIndex |
| 28 | + * @return {number} |
| 29 | + */ |
| 30 | + getRightChildIndex(parentIndex) { |
| 31 | + return (2 * parentIndex) + 2; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param {number} childIndex |
| 36 | + * @return {number} |
| 37 | + */ |
| 38 | + getParentIndex(childIndex) { |
| 39 | + return Math.floor((childIndex - 1) / 2); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param {number} childIndex |
| 44 | + * @return {boolean} |
| 45 | + */ |
| 46 | + hasParent(childIndex) { |
| 47 | + return this.getParentIndex(childIndex) >= 0; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param {number} parentIndex |
| 52 | + * @return {boolean} |
| 53 | + */ |
| 54 | + hasLeftChild(parentIndex) { |
| 55 | + return this.getLeftChildIndex(parentIndex) < this.heapContainer.length; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @param {number} parentIndex |
| 60 | + * @return {boolean} |
| 61 | + */ |
| 62 | + hasRightChild(parentIndex) { |
| 63 | + return this.getRightChildIndex(parentIndex) < this.heapContainer.length; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param {number} parentIndex |
| 68 | + * @return {*} |
| 69 | + */ |
| 70 | + leftChild(parentIndex) { |
| 71 | + return this.heapContainer[this.getLeftChildIndex(parentIndex)]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param {number} parentIndex |
| 76 | + * @return {*} |
| 77 | + */ |
| 78 | + rightChild(parentIndex) { |
| 79 | + return this.heapContainer[this.getRightChildIndex(parentIndex)]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param {number} childIndex |
| 84 | + * @return {*} |
| 85 | + */ |
| 86 | + parent(childIndex) { |
| 87 | + return this.heapContainer[this.getParentIndex(childIndex)]; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param {number} indexOne |
| 92 | + * @param {number} indexTwo |
| 93 | + */ |
| 94 | + swap(indexOne, indexTwo) { |
| 95 | + const tmp = this.heapContainer[indexTwo]; |
| 96 | + this.heapContainer[indexTwo] = this.heapContainer[indexOne]; |
| 97 | + this.heapContainer[indexOne] = tmp; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return {*} |
| 102 | + */ |
| 103 | + peek() { |
| 104 | + if (this.heapContainer.length === 0) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + return this.heapContainer[0]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return {*} |
| 113 | + */ |
| 114 | + poll() { |
| 115 | + if (this.heapContainer.length === 0) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + if (this.heapContainer.length === 1) { |
| 120 | + return this.heapContainer.pop(); |
| 121 | + } |
| 122 | + |
| 123 | + const item = this.heapContainer[0]; |
| 124 | + |
| 125 | + // Move the last element from the end to the head. |
| 126 | + this.heapContainer[0] = this.heapContainer.pop(); |
| 127 | + this.heapifyDown(); |
| 128 | + |
| 129 | + return item; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param {*} item |
| 134 | + * @return {MinHeap} |
| 135 | + */ |
| 136 | + add(item) { |
| 137 | + this.heapContainer.push(item); |
| 138 | + this.heapifyUp(); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param {*} item |
| 144 | + * @param {Comparator} [customComparator] |
| 145 | + * @return {Number[]} |
| 146 | + */ |
| 147 | + find(item, customComparator) { |
| 148 | + const foundItemIndices = []; |
| 149 | + const comparator = customComparator || this.compare; |
| 150 | + |
| 151 | + for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) { |
| 152 | + if (comparator.equal(item, this.heapContainer[itemIndex])) { |
| 153 | + foundItemIndices.push(itemIndex); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return foundItemIndices; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return {boolean} |
| 162 | + */ |
| 163 | + isEmpty() { |
| 164 | + return !this.heapContainer.length; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * @return {string} |
| 169 | + */ |
| 170 | + toString() { |
| 171 | + return this.heapContainer.toString(); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +export default Heap; |
0 commit comments