clean up Heap implementation (#184)

This commit is contained in:
Kevin Brewer
2018-08-30 00:11:14 -05:00
committed by Oleksii Trekhleb
parent a8d7435b8d
commit bc50fff2ee

View File

@ -47,7 +47,7 @@ export default class Heap {
* @return {boolean} * @return {boolean}
*/ */
hasParent(childIndex) { hasParent(childIndex) {
return this.getParentIndex(childIndex) >= 0; return childIndex > 0;
} }
/** /**
@ -144,17 +144,16 @@ export default class Heap {
/** /**
* @param {*} item * @param {*} item
* @param {Comparator} [customFindingComparator] * @param {Comparator} [customComparator]
* @return {Heap} * @return {Heap}
*/ */
remove(item, customFindingComparator) { remove(item, customComparator = this.compare) {
// Find number of items to remove. // Find number of items to remove.
const customComparator = customFindingComparator || this.compare;
const numberOfItemsToRemove = this.find(item, customComparator).length; const numberOfItemsToRemove = this.find(item, customComparator).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 change after each heapify process. // indices are being changed after each heapify process.
const indexToRemove = this.find(item, customComparator).pop(); const indexToRemove = this.find(item, customComparator).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.
@ -165,16 +164,14 @@ 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.hasParent(indexToRemove) ? this.parent(indexToRemove) : null;
const leftChild = this.hasLeftChild(indexToRemove) ? this.leftChild(indexToRemove) : null;
// If there is no parent or parent is in incorrect order with the node // If there is no parent or parent is in correct order with the node
// we're going to delete then heapify down. Otherwise heapify up. // we're going to delete then heapify down. Otherwise heapify up.
if ( if (
leftChild !== null this.hasLeftChild(indexToRemove)
&& ( && (
parentItem === null parentItem == null
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove]) || this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
) )
) { ) {
@ -193,12 +190,11 @@ export default class Heap {
* @param {Comparator} [customComparator] * @param {Comparator} [customComparator]
* @return {Number[]} * @return {Number[]}
*/ */
find(item, customComparator) { find(item, customComparator = this.compare) {
const foundItemIndices = []; const foundItemIndices = [];
const comparator = customComparator || this.compare;
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) { for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
if (comparator.equal(item, this.heapContainer[itemIndex])) { if (customComparator.equal(item, this.heapContainer[itemIndex])) {
foundItemIndices.push(itemIndex); foundItemIndices.push(itemIndex);
} }
} }
@ -224,9 +220,9 @@ export default class Heap {
* @param {number} [customStartIndex] * @param {number} [customStartIndex]
*/ */
heapifyUp(customStartIndex) { heapifyUp(customStartIndex) {
// Take last element (last in array or the bottom left in a tree) in // Take the last element (last in array or the bottom left in a tree)
// a heap container and lift him up until we find the parent element // in the heap container and lift it up until it is in the correct
// that is less then the current new one. // order with respect to its parent element.
let currentIndex = customStartIndex || this.heapContainer.length - 1; let currentIndex = customStartIndex || this.heapContainer.length - 1;
while ( while (
@ -241,10 +237,11 @@ export default class Heap {
/** /**
* @param {number} [customStartIndex] * @param {number} [customStartIndex]
*/ */
heapifyDown(customStartIndex) { heapifyDown(customStartIndex = 0) {
// Compare the root element to its children and swap root with the smallest // Compare the parent element to its children and swap parent with the appropriate
// of children. Do the same for next children after swap. // child (smallest child for MinHeap, largest child for MaxHeap).
let currentIndex = customStartIndex || 0; // Do the same for next children after swap.
let currentIndex = customStartIndex;
let nextIndex = null; let nextIndex = null;
while (this.hasLeftChild(currentIndex)) { while (this.hasLeftChild(currentIndex)) {