Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68af4fc

Browse files
committed
Clean up Heaps.
1 parent bc50fff commit 68af4fc

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

‎src/data-structures/heap/Heap.js‎

Lines changed: 10 additions & 9 deletions
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 childIndex> 0;
50+
return this.getParentIndex(childIndex)>= 0;
5151
}
5252

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

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

154154
for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) {
155155
// We need to find item index to remove each time after removal since
156156
// indices are being changed after each heapify process.
157-
const indexToRemove = this.find(item, customComparator).pop();
157+
const indexToRemove = this.find(item, comparator).pop();
158158

159159
// If we need to remove last child in the heap then just remove it.
160160
// There is no need to heapify the heap afterwards.
@@ -164,14 +164,15 @@ export default class Heap {
164164
// Move last element in heap to the vacant (removed) position.
165165
this.heapContainer[indexToRemove] = this.heapContainer.pop();
166166

167+
// Get parent.
167168
const parentItem = this.parent(indexToRemove);
168169

169170
// If there is no parent or parent is in correct order with the node
170171
// we're going to delete then heapify down. Otherwise heapify up.
171172
if (
172173
this.hasLeftChild(indexToRemove)
173174
&& (
174-
parentItem==null
175+
!parentItem
175176
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
176177
)
177178
) {
@@ -187,14 +188,14 @@ export default class Heap {
187188

188189
/**
189190
* @param {*} item
190-
* @param {Comparator} [customComparator]
191+
* @param {Comparator} [comparator]
191192
* @return {Number[]}
192193
*/
193-
find(item, customComparator = this.compare) {
194+
find(item, comparator = this.compare) {
194195
const foundItemIndices = [];
195196

196197
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
197-
if (customComparator.equal(item, this.heapContainer[itemIndex])) {
198+
if (comparator.equal(item, this.heapContainer[itemIndex])) {
198199
foundItemIndices.push(itemIndex);
199200
}
200201
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /