feat: When comparing two values in the Heap and binary search tree, if one of the values is of object type, it is mandatory to pass a custom comparator function.

This commit is contained in:
Revone 2024-01-29 20:23:06 +08:00
parent 3028a78985
commit 8051850331
5 changed files with 14 additions and 23 deletions

View file

@ -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;

View file

@ -393,9 +393,9 @@ export class Heap<E = any> extends IterableElementBase<E> {
}
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<E = any> extends IterableElementBase<E> {
protected _comparator: Comparator<E> = this._DEFAULT_COMPARATOR;
/**
* The function returns the value of the _comparator property.
* @returns The `_comparator` property is being returned.

View file

@ -22,12 +22,15 @@ export class MaxHeap<E = any> extends Heap<E> {
constructor(
elements: Iterable<E> = [],
options: HeapOptions<E> = {
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;
}
}
) {

View file

@ -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<E = any> extends Heap<E> {
constructor(
elements: Iterable<E> = [],
options: HeapOptions<E> = {
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<E> = [], options?: HeapOptions<E>) {
super(elements, options);
}
}

View file

@ -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.`
);
});