trekhleb/javascript-algorithms · error · Error
You have to implement heap pair comparison method
Error message
You have to implement heap pair comparison method
for ${firstElement} and ${secondElement} values.
What it means
Heap is an abstract base class: pairIsInCorrectOrder(firstElement, secondElement) is the ordering hook that heapifyUp, heapifyDown and remove consult to decide whether a parent/child pair may stay in place, and the base implementation deliberately throws because no default ordering exists. Concrete subclasses define it — MinHeap returns compare.lessThanOrEqual(first, second), MaxHeap returns compare.greaterThanOrEqual(first, second). Seeing this error means the raw Heap class (or an incomplete subclass) is being used instead of a concrete heap.
Source
Thrown at src/data-structures/heap/Heap.js:281
}
this.swap(currentIndex, nextIndex);
currentIndex = nextIndex;
}
}
/**
* Checks if pair of heap elements is in correct order.
* For MinHeap the first element must be always smaller or equal.
* For MaxHeap the first element must be always bigger or equal.
*
* @param {*} firstElement
* @param {*} secondElement
* @return {boolean}
*/
/* istanbul ignore next */
pairIsInCorrectOrder(firstElement, secondElement) {
throw new Error(`
You have to implement heap pair comparison method
for ${firstElement} and ${secondElement} values.
`);
}
}
View on GitHub (pinned to 85293e3e2b)
Solutions
- If you need a standard heap, instantiate MinHeap or MaxHeap instead of Heap.
- If you subclass Heap, implement pairIsInCorrectOrder(firstElement, secondElement) returning true when the pair is correctly ordered (e.g. firstElement.priority <= secondElement.priority for a min-style heap).
- Check the import path — it must point at src/data-structures/heap/MinHeap.js or MaxHeap.js, not Heap.js.
- If you only need custom ordering of primitives, pass a custom Comparator to MinHeap/MaxHeap instead of subclassing Heap.
Example fix
// before
import Heap from './data-structures/heap/Heap';
const heap = new Heap(comparator);
heap.add(3); // throws: heap pair comparison not implemented
// after (option 1: use a concrete heap)
import MinHeap from './data-structures/heap/MinHeap';
const heap = new MinHeap();
heap.add(3);
// after (option 2: subclass and implement the hook)
class MinPriorityHeap extends Heap {
pairIsInCorrectOrder(a, b) {
return a.priority <= b.priority;
}
} Defensive patterns
Strategy: type-guard
Validate before calling
import Heap from './data-structures/heap/Heap';
import MinHeap from './data-structures/heap/MinHeap';
import MaxHeap from './data-structures/heap/MaxHeap';
const isConcreteHeap = (heap) =>
heap instanceof MinHeap ||
heap instanceof MaxHeap ||
(heap instanceof Heap && heap.pairIsInCorrectOrder !== Heap.prototype.pairIsInCorrectOrder);
if (!isConcreteHeap(heap)) {
throw new TypeError('Heap is abstract: use MinHeap/MaxHeap or override pairIsInCorrectOrder');
} Type guard
const isConcreteHeap = (heap) => heap instanceof MinHeap || heap instanceof MaxHeap || (heap instanceof Heap && heap.pairIsInCorrectOrder !== Heap.prototype.pairIsInCorrectOrder);
Prevention
- Never instantiate Heap directly; treat it as an abstract class and import MinHeap or MaxHeap.
- When subclassing Heap, write pairIsInCorrectOrder first and assert it in a unit test before adding heap operations.
- Add a lint rule or code review check that greps for 'new Heap(' in application code.
- Encapsulate heap creation in one factory function so only it decides which heap class is used.
When it happens
Trigger: Instantiating Heap directly and calling add(), remove(), or anything that triggers heapifyUp/heapifyDown (e.g. new Heap(); heap.add(3)). Writing a custom class that extends Heap (for example to order objects by a priority field) without overriding pairIsInCorrectOrder. Importing Heap via editor autocomplete instead of MinHeap/MaxHeap, or copying Heap into a new structure and dropping the subclass.
Common situations: Building a priority queue directly on the raw Heap class; adapting the heap to custom objects by only overriding the compare function instead of the pair method; porting example code that starts from Heap; refactors where the concrete subclass import is accidentally replaced with the base class.
Related errors
- Cannot construct Heap instance directly
- sort method must be implemented
- Item not found in the tree
- Position is out of allowed range
- Left index can not be greater than right one
AI-assisted analysis of trekhleb/javascript-algorithms@85293e3e2b (2026-08-24).
Data as JSON: /api/errors/7ecf6e167b3a39aa.
Report an issue: GitHub.