mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-06 09:23:18 +08:00
Clean up Heaps.
This commit is contained in:
@ -47,7 +47,7 @@ export default class Heap {
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
hasParent(childIndex) {
|
hasParent(childIndex) {
|
||||||
return childIndex > 0;
|
return this.getParentIndex(childIndex) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -144,17 +144,17 @@ export default class Heap {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {*} item
|
* @param {*} item
|
||||||
* @param {Comparator} [customComparator]
|
* @param {Comparator} [comparator]
|
||||||
* @return {Heap}
|
* @return {Heap}
|
||||||
*/
|
*/
|
||||||
remove(item, customComparator = this.compare) {
|
remove(item, comparator = this.compare) {
|
||||||
// Find number of items to remove.
|
// Find number of items to remove.
|
||||||
const numberOfItemsToRemove = this.find(item, customComparator).length;
|
const numberOfItemsToRemove = this.find(item, comparator).length;
|
||||||
|
|
||||||
for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) {
|
for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) {
|
||||||
// We need to find item index to remove each time after removal since
|
// We need to find item index to remove each time after removal since
|
||||||
// indices are being changed after each heapify process.
|
// indices are being changed after each heapify process.
|
||||||
const indexToRemove = this.find(item, customComparator).pop();
|
const indexToRemove = this.find(item, comparator).pop();
|
||||||
|
|
||||||
// If we need to remove last child in the heap then just remove it.
|
// If we need to remove last child in the heap then just remove it.
|
||||||
// There is no need to heapify the heap afterwards.
|
// There is no need to heapify the heap afterwards.
|
||||||
@ -164,6 +164,7 @@ export default class Heap {
|
|||||||
// Move last element in heap to the vacant (removed) position.
|
// Move last element in heap to the vacant (removed) position.
|
||||||
this.heapContainer[indexToRemove] = this.heapContainer.pop();
|
this.heapContainer[indexToRemove] = this.heapContainer.pop();
|
||||||
|
|
||||||
|
// Get parent.
|
||||||
const parentItem = this.parent(indexToRemove);
|
const parentItem = this.parent(indexToRemove);
|
||||||
|
|
||||||
// If there is no parent or parent is in correct order with the node
|
// If there is no parent or parent is in correct order with the node
|
||||||
@ -171,7 +172,7 @@ export default class Heap {
|
|||||||
if (
|
if (
|
||||||
this.hasLeftChild(indexToRemove)
|
this.hasLeftChild(indexToRemove)
|
||||||
&& (
|
&& (
|
||||||
parentItem == null
|
!parentItem
|
||||||
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
|
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
@ -187,14 +188,14 @@ export default class Heap {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {*} item
|
* @param {*} item
|
||||||
* @param {Comparator} [customComparator]
|
* @param {Comparator} [comparator]
|
||||||
* @return {Number[]}
|
* @return {Number[]}
|
||||||
*/
|
*/
|
||||||
find(item, customComparator = this.compare) {
|
find(item, comparator = this.compare) {
|
||||||
const foundItemIndices = [];
|
const foundItemIndices = [];
|
||||||
|
|
||||||
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
|
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
|
||||||
if (customComparator.equal(item, this.heapContainer[itemIndex])) {
|
if (comparator.equal(item, this.heapContainer[itemIndex])) {
|
||||||
foundItemIndices.push(itemIndex);
|
foundItemIndices.push(itemIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user