mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
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:
parent
3028a78985
commit
8051850331
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.`
|
||||
);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue