Skip to content

Add support for complex data to PriorityQueue #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/data-structures/heap/Heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,40 @@ export default class Heap {
// We need to find item index to remove each time after removal since
// indices are being changed after each heapify process.
const indexToRemove = this.find(item, comparator).pop();
this.removeIndex(indexToRemove);
}

return this;
}

// If we need to remove last child in the heap then just remove it.
// There is no need to heapify the heap afterwards.
if (indexToRemove === (this.heapContainer.length - 1)) {
this.heapContainer.pop();
/**
* @param {number} indexToRemove
* @return {Heap}
*/
removeIndex(indexToRemove) {
// If we need to remove last child in the heap then just remove it.
// There is no need to heapify the heap afterwards.
if (indexToRemove === (this.heapContainer.length - 1)) {
this.heapContainer.pop();
} else {
// Move last element in heap to the vacant (removed) position.
this.heapContainer[indexToRemove] = this.heapContainer.pop();

// Get parent.
const parentItem = this.parent(indexToRemove);

// 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.
if (
this.hasLeftChild(indexToRemove)
&& (
!parentItem
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
)
) {
this.heapifyDown(indexToRemove);
} else {
// Move last element in heap to the vacant (removed) position.
this.heapContainer[indexToRemove] = this.heapContainer.pop();

// Get parent.
const parentItem = this.parent(indexToRemove);

// 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.
if (
this.hasLeftChild(indexToRemove)
&& (
!parentItem
|| this.pairIsInCorrectOrder(parentItem, this.heapContainer[indexToRemove])
)
) {
this.heapifyDown(indexToRemove);
} else {
this.heapifyUp(indexToRemove);
}
this.heapifyUp(indexToRemove);
}
}

Expand All @@ -203,6 +212,15 @@ export default class Heap {
return foundItemIndices;
}

/**
*
* @param {number} index
* @return {*}
*/
getElementAtIndex(index) {
return this.heapContainer[index];
}

/**
* @return {boolean}
*/
Expand Down
91 changes: 65 additions & 26 deletions src/data-structures/priority-queue/PriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,126 @@ import Comparator from '../../utils/comparator/Comparator';
// It is the same as min heap except that when comparing to elements
// we take into account not element's value but rather its priority.
export default class PriorityQueue extends MinHeap {
constructor() {
/**
* @constructor
* @param {function} [compareValueFunction]
*/
constructor(compareValueFunction) {
super();
this.priorities = {};
// Map data structure supports using any value for key type
// e.g. functions, objects, or primitives
this.priorities = new Map();
this.compare = new Comparator(this.comparePriority.bind(this));
this.compareValue = new Comparator(compareValueFunction);
}

/**
* @param {*} item
* @param {number} [priority]
* @param {number} [priority = 0]
* @return {PriorityQueue}
*/
add(item, priority = 0) {
this.priorities[item] = priority;
this.priorities.set(item, priority);
super.add(item);

return this;
}

/**
* @param {*} item
* @param {Comparator} [customFindingComparator]
* @param {Comparator|function} [maybeComparator]
* @return {PriorityQueue}
*/
remove(item, customFindingComparator) {
super.remove(item, customFindingComparator);
delete this.priorities[item];
remove(item, maybeComparator) {
const comparator = this.getValueComparator(maybeComparator);
super.remove(item, comparator);
this.priorities.delete(item);

return this;
}

/**
* @param {*} item
* @param {number} priority
* @param {Comparator|function} [maybeComparator]
* @return {PriorityQueue}
*/
changePriority(item, priority) {
this.remove(item, new Comparator(this.compareValue));
this.add(item, priority);
changePriority(item, priority, maybeComparator) {
const comparator = this.getValueComparator(maybeComparator);
const numberOfItemsToRemove = this.find(item, comparator).length;
const itemsToUpdate = [];

for (let iteration = 0; iteration < numberOfItemsToRemove; iteration += 1) {
// We need to find item index to remove each time after removal since
// indices are being changed after each heapify process.
const indexToRemove = this.find(item, comparator).pop();
const itemToUpdate = this.getElementAtIndex(indexToRemove);
itemsToUpdate.push(itemToUpdate);
this.priorities.delete(itemToUpdate);
this.removeIndex(indexToRemove);
}

itemsToUpdate.forEach((itemToUpdate) => {
this.add(itemToUpdate, priority);
});

return this;
}

/**
* @param {*} item
* @return {Number[]}
* @param {Comparator|function} [maybeComparator]
* @return {*[]}
*/
findByValue(item) {
return this.find(item, new Comparator(this.compareValue));
findByValue(item, maybeComparator) {
const comparator = this.getValueComparator(maybeComparator);
return this.find(item, comparator);
}

/**
* @param {*} item
* @param {Comparator|function} [maybeComparator]
* @return {boolean}
*/
hasValue(item) {
return this.findByValue(item).length > 0;
hasValue(item, maybeComparator) {
const comparator = this.getValueComparator(maybeComparator);
return this.findByValue(item, comparator).length > 0;
}

/**
* @param {*} a
* @param {*} b
* @return {number}
* @param {Comparator|function} [maybeComparator]
* @return {Comparator}
*/
comparePriority(a, b) {
if (this.priorities[a] === this.priorities[b]) {
return 0;
getValueComparator(maybeComparator) {
if (maybeComparator == null) {
return this.compareValue;
}

if (maybeComparator instanceof Comparator) {
return maybeComparator;
}

return this.priorities[a] < this.priorities[b] ? -1 : 1;
if (maybeComparator instanceof Function) {
return new Comparator(maybeComparator);
}

throw new TypeError(
'Invalid comparator type\n'
+ 'Must be one of: Comparator | Function | undefined\n'
+ `Given: ${typeof maybeComparator}`,
);
}

/**
* @param {*} a
* @param {*} b
* @return {number}
*/
compareValue(a, b) {
if (a === b) {
comparePriority(a, b) {
if (this.priorities.get(a) === this.priorities.get(b)) {
return 0;
}

return a < b ? -1 : 1;
return this.priorities.get(a) < this.priorities.get(b) ? -1 : 1;
}
}
Loading