{"record":{"id":"7ecf6e167b3a39aa","repo":"trekhleb/javascript-algorithms","slug":"you-have-to-implement-heap-pair-comparison","errorCode":null,"errorMessage":"\n      You have to implement heap pair comparison method\n      for ${firstElement} and ${secondElement} values.\n    ","messagePattern":"\n      You have to implement heap pair comparison method\n      for (.+?) and (.+?) values\\.\n    ","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/data-structures/heap/Heap.js","lineNumber":281,"sourceCode":"      }\n\n      this.swap(currentIndex, nextIndex);\n      currentIndex = nextIndex;\n    }\n  }\n\n  /**\n   * Checks if pair of heap elements is in correct order.\n   * For MinHeap the first element must be always smaller or equal.\n   * For MaxHeap the first element must be always bigger or equal.\n   *\n   * @param {*} firstElement\n   * @param {*} secondElement\n   * @return {boolean}\n   */\n  /* istanbul ignore next */\n  pairIsInCorrectOrder(firstElement, secondElement) {\n    throw new Error(`\n      You have to implement heap pair comparison method\n      for ${firstElement} and ${secondElement} values.\n    `);\n  }\n}\n","sourceCodeStart":263,"sourceCodeEnd":287,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/data-structures/heap/Heap.js#L263-L287","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nimport Heap from './data-structures/heap/Heap';\nconst heap = new Heap(comparator);\nheap.add(3); // throws: heap pair comparison not implemented\n\n// after (option 1: use a concrete heap)\nimport MinHeap from './data-structures/heap/MinHeap';\nconst heap = new MinHeap();\nheap.add(3);\n\n// after (option 2: subclass and implement the hook)\nclass MinPriorityHeap extends Heap {\n  pairIsInCorrectOrder(a, b) {\n    return a.priority <= b.priority;\n  }\n}","handlingStrategy":"type-guard","validationCode":"import Heap from './data-structures/heap/Heap';\nimport MinHeap from './data-structures/heap/MinHeap';\nimport MaxHeap from './data-structures/heap/MaxHeap';\n\nconst isConcreteHeap = (heap) =>\n  heap instanceof MinHeap ||\n  heap instanceof MaxHeap ||\n  (heap instanceof Heap && heap.pairIsInCorrectOrder !== Heap.prototype.pairIsInCorrectOrder);\n\nif (!isConcreteHeap(heap)) {\n  throw new TypeError('Heap is abstract: use MinHeap/MaxHeap or override pairIsInCorrectOrder');\n}","typeGuard":"const isConcreteHeap = (heap) =>\n  heap instanceof MinHeap ||\n  heap instanceof MaxHeap ||\n  (heap instanceof Heap && heap.pairIsInCorrectOrder !== Heap.prototype.pairIsInCorrectOrder);","tryCatchPattern":null,"preventionTips":["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."],"tags":["heap","abstract-method","subclassing","priority-queue","data-structures"],"backgroundTag":"unimplemented-abstract-method","analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}