mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-06 09:23:18 +08:00
Use Comparator in heap.
This commit is contained in:
@ -1,7 +1,10 @@
|
|||||||
|
import Comparator from '../../utils/comparator/Comparator';
|
||||||
|
|
||||||
export default class MinHeap {
|
export default class MinHeap {
|
||||||
constructor() {
|
constructor() {
|
||||||
// Array representation of the heap.
|
// Array representation of the heap.
|
||||||
this.heapContainer = [];
|
this.heapContainer = [];
|
||||||
|
this.compare = new Comparator();
|
||||||
}
|
}
|
||||||
|
|
||||||
static getLeftChildIndex(parentIndex) {
|
static getLeftChildIndex(parentIndex) {
|
||||||
@ -85,7 +88,7 @@ export default class MinHeap {
|
|||||||
|
|
||||||
while (
|
while (
|
||||||
MinHeap.hasParent(currentIndex) &&
|
MinHeap.hasParent(currentIndex) &&
|
||||||
this.lessThen(this.heapContainer[currentIndex], this.parent(currentIndex))
|
this.compare.lessThen(this.heapContainer[currentIndex], this.parent(currentIndex))
|
||||||
) {
|
) {
|
||||||
this.swap(currentIndex, MinHeap.getParentIndex(currentIndex));
|
this.swap(currentIndex, MinHeap.getParentIndex(currentIndex));
|
||||||
currentIndex = MinHeap.getParentIndex(currentIndex);
|
currentIndex = MinHeap.getParentIndex(currentIndex);
|
||||||
@ -101,14 +104,14 @@ export default class MinHeap {
|
|||||||
while (this.hasLeftChild(currentIndex)) {
|
while (this.hasLeftChild(currentIndex)) {
|
||||||
if (
|
if (
|
||||||
this.hasRightChild(currentIndex) &&
|
this.hasRightChild(currentIndex) &&
|
||||||
this.lessThen(this.rightChild(currentIndex), this.leftChild(currentIndex))
|
this.compare.lessThen(this.rightChild(currentIndex), this.leftChild(currentIndex))
|
||||||
) {
|
) {
|
||||||
nextIndex = MinHeap.getRightChildIndex(currentIndex);
|
nextIndex = MinHeap.getRightChildIndex(currentIndex);
|
||||||
} else {
|
} else {
|
||||||
nextIndex = MinHeap.getLeftChildIndex(currentIndex);
|
nextIndex = MinHeap.getLeftChildIndex(currentIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.lessThen(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
|
if (this.compare.lessThen(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,18 +123,4 @@ export default class MinHeap {
|
|||||||
toString() {
|
toString() {
|
||||||
return this.heapContainer.toString();
|
return this.heapContainer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
compare(a, b) {
|
|
||||||
if (a === b) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Min heap may be converted to max heap by simply changing this line to:
|
|
||||||
// a > b ? -1 : 1
|
|
||||||
return a < b ? -1 : 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
lessThen(a, b) {
|
|
||||||
return this.compare(a, b) === -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import MinHeap from '../heap/MinHeap';
|
import MinHeap from '../heap/MinHeap';
|
||||||
|
import Comparator from '../../utils/comparator/Comparator';
|
||||||
|
|
||||||
// It is the same as min heap except that when comparing to elements
|
// 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.
|
// we take into account not element's value but rather its priority.
|
||||||
@ -6,6 +7,7 @@ export default class PriorityQueue extends MinHeap {
|
|||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.priorities = {};
|
this.priorities = {};
|
||||||
|
this.compare = new Comparator(this.comparePriority.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
add(item, priority = 0) {
|
add(item, priority = 0) {
|
||||||
@ -13,7 +15,7 @@ export default class PriorityQueue extends MinHeap {
|
|||||||
super.add(item);
|
super.add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
compare(a, b) {
|
comparePriority(a, b) {
|
||||||
if (this.priorities[a] === this.priorities[b]) {
|
if (this.priorities[a] === this.priorities[b]) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user