diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 8fd1f78..a0756fd 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -799,9 +799,9 @@ export class BST< } protected _DEFAULT_COMPARATOR = (a: K, b: K): number => { - if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) { + if (typeof a === 'object' || typeof b === 'object') { throw TypeError( - 'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.' + `When comparing object types, a custom comparator must be defined in the constructor's options parameter.` ); } if (a > b) return 1; diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index ba9eb63..6489812 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -393,9 +393,9 @@ export class Heap extends IterableElementBase { } protected _DEFAULT_COMPARATOR = (a: E, b: E): number => { - if (typeof a === 'object' && typeof b === 'object' && this.comparator === this._DEFAULT_COMPARATOR) { + if (typeof a === 'object' || typeof b === 'object') { throw TypeError( - 'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.' + `When comparing object types, a custom comparator must be defined in the constructor's options parameter.` ); } if (a > b) return 1; @@ -405,7 +405,6 @@ export class Heap extends IterableElementBase { protected _comparator: Comparator = this._DEFAULT_COMPARATOR; - /** * The function returns the value of the _comparator property. * @returns The `_comparator` property is being returned. diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index 89ebef2..ebb6eb6 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -22,12 +22,15 @@ export class MaxHeap extends Heap { constructor( elements: Iterable = [], options: HeapOptions = { - comparator: (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return b - a; + comparator: (a: E, b: E): number => { + if (typeof a === 'object' || typeof b === 'object') { + throw TypeError( + `When comparing object types, a custom comparator must be defined in the constructor's options parameter.` + ); } + if (a < b) return 1; + if (a > b) return -1; + return 0; } } ) { diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index 2ddce15..1f1d2be 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -19,18 +19,7 @@ import { Heap } from './heap'; * 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance. */ export class MinHeap extends Heap { - constructor( - elements: Iterable = [], - options: HeapOptions = { - comparator: (a: E, b: E) => { - if (!(typeof a === 'number' && typeof b === 'number')) { - throw new Error('The a, b params of compare function must be number'); - } else { - return a - b; - } - } - } - ) { + constructor(elements: Iterable = [], options?: HeapOptions) { super(elements, options); } } diff --git a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts index 436fcc9..ba429fb 100644 --- a/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +++ b/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts @@ -734,7 +734,7 @@ describe('AVLTree toEntryFn', () => { { obj: { id: 5 } } ]) ).toThrowError( - 'When comparing two object types, it is necessary to customize a [comparator] function of options parameter during the instantiation of the data structure.' + `When comparing object types, a custom comparator must be defined in the constructor's options parameter.` ); });