Skip to content

Commit bc50fff

Browse files
appleJaxtrekhleb
authored andcommitted
clean up Heap implementation (trekhleb#184)
1 parent a8d7435 commit bc50fff

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/data-structures/heap/Heap.js

+18-21
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class Heap {
4747
* @return {boolean}
4848
*/
4949
hasParent(childIndex) {
50-
return this.getParentIndex(childIndex) >= 0;
50+
return childIndex > 0;
5151
}
5252

5353
/**
@@ -144,17 +144,16 @@ export default class Heap {
144144

145145
/**
146146
* @param {*} item
147-
* @param {Comparator} [customFindingComparator]
147+
* @param {Comparator} [customComparator]
148148
* @return {Heap}
149149
*/
150-
remove(item, customFindingComparator) {
150+
remove(item, customComparator = this.compare) {
151151
// Find number of items to remove.
152-
const customComparator = customFindingComparator || this.compare;
153152
const numberOfItemsToRemove = this.find(item, customComparator).length;
154153

155154
for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) {
156155
// We need to find item index to remove each time after removal since
157-
// indices are being change after each heapify process.
156+
// indices are being changed after each heapify process.
158157
const indexToRemove = this.find(item, customComparator).pop();
159158

160159
// If we need to remove last child in the heap then just remove it.
@@ -165,16 +164,14 @@ export default class Heap {
165164
// Move last element in heap to the vacant (removed) position.
166165
this.heapContainer[indexToRemove] = this.heapContainer.pop();
167166

168-
// Get parent.
169-
const parentItem = this.hasParent(indexToRemove) ? this.parent(indexToRemove) : null;
170-
const leftChild = this.hasLeftChild(indexToRemove) ? this.leftChild(indexToRemove) : null;
167+
const parentItem = this.parent(indexToRemove);
171168

172-
// If there is no parent or parent is in incorrect order with the node
169+
// If there is no parent or parent is in correct order with the node
173170
// we're going to delete then heapify down. Otherwise heapify up.
174171
if (
175-
leftChild !== null
172+
this.hasLeftChild(indexToRemove)
176173
&& (
177-
parentItem === null
174+
parentItem == null
178175
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
179176
)
180177
) {
@@ -193,12 +190,11 @@ export default class Heap {
193190
* @param {Comparator} [customComparator]
194191
* @return {Number[]}
195192
*/
196-
find(item, customComparator) {
193+
find(item, customComparator = this.compare) {
197194
const foundItemIndices = [];
198-
const comparator = customComparator || this.compare;
199195

200196
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
201-
if (comparator.equal(item, this.heapContainer[itemIndex])) {
197+
if (customComparator.equal(item, this.heapContainer[itemIndex])) {
202198
foundItemIndices.push(itemIndex);
203199
}
204200
}
@@ -224,9 +220,9 @@ export default class Heap {
224220
* @param {number} [customStartIndex]
225221
*/
226222
heapifyUp(customStartIndex) {
227-
// Take last element (last in array or the bottom left in a tree) in
228-
// a heap container and lift him up until we find the parent element
229-
// that is less then the current new one.
223+
// Take the last element (last in array or the bottom left in a tree)
224+
// in the heap container and lift it up until it is in the correct
225+
// order with respect to its parent element.
230226
let currentIndex = customStartIndex || this.heapContainer.length - 1;
231227

232228
while (
@@ -241,10 +237,11 @@ export default class Heap {
241237
/**
242238
* @param {number} [customStartIndex]
243239
*/
244-
heapifyDown(customStartIndex) {
245-
// Compare the root element to its children and swap root with the smallest
246-
// of children. Do the same for next children after swap.
247-
let currentIndex = customStartIndex || 0;
240+
heapifyDown(customStartIndex = 0) {
241+
// Compare the parent element to its children and swap parent with the appropriate
242+
// child (smallest child for MinHeap, largest child for MaxHeap).
243+
// Do the same for next children after swap.
244+
let currentIndex = customStartIndex;
248245
let nextIndex = null;
249246

250247
while (this.hasLeftChild(currentIndex)) {

0 commit comments

Comments
 (0)