[core] Shared Comparator and DFSOrderPattern among multiple data structures. ESLint config improved

This commit is contained in:
Revone 2023-10-20 11:43:26 +08:00
parent 9a90ae40fc
commit 4fa0816223
14 changed files with 34 additions and 31 deletions

View file

@ -1,9 +1,14 @@
module.exports = {
"parser": "@typescript-eslint/parser",
"plugins": [
"import",
"@typescript-eslint"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier"
],
ignorePatterns: ["lib/", "dist/", "umd/", "coverage/", "docs/"],
"ignorePatterns": ["lib/", "dist/", "umd/", "coverage/", "docs/"],
"rules": {
"import/no-anonymous-default-export": "off",
"@typescript-eslint/no-unused-vars": "error",
@ -39,9 +44,6 @@ module.exports = {
}
]
},
"plugins": [
"import"
],
"settings": {
"import/parsers": {
"@typescript-eslint/parser": [

View file

@ -17,6 +17,7 @@
"build:commonjs": "rm -rf dist && tsc --project tsconfig.prod.json",
"build:umd": "webpack",
"build:docs": "typedoc --out docs ./src",
"check": "tsc --noEmit",
"lint:src": "eslint --fix 'src/**/*.{js,ts}'",
"lint:test": "eslint --fix 'test/**/*.{js,ts}'",
"lint": "npm run lint:src && npm run lint:test",

View file

@ -13,7 +13,6 @@ import type {
BinaryTreeNodeProperty,
BinaryTreeOptions
} from '../../types';
import {IBinaryTree} from '../../interfaces';
import {
BinaryTreeDeletedResult,
BinaryTreeNodePropertyName,
@ -22,6 +21,7 @@ import {
LoopType,
NodeOrPropertyName
} from '../../types';
import {IBinaryTree} from '../../interfaces';
import {trampoline} from '../../utils';
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
@ -574,7 +574,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
/**
* The function `getLeftMost` returns the leftmost node in a binary tree, starting from a specified node or the root if
* no node is specified.
* generic type representing a node in a binary tree), `BinaryTreeNodeKey` (a type representing the ID of a binary tree
* generic type representing a node in a binary tree, `BinaryTreeNodeKey` (a type representing the ID of a binary tree
* node), or `null`.
* @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
* provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the traversal

View file

@ -5,13 +5,14 @@
* @license MIT License
*/
import type {HeapComparator, HeapDFSOrderPattern} from '../../types';
import type {Comparator} from '../../types';
import {DFSOrderPattern} from '../../types';
export class Heap<E> {
protected nodes: E[] = [];
private readonly comparator: HeapComparator<E>;
private readonly comparator: Comparator<E>;
constructor(comparator: HeapComparator<E>) {
constructor(comparator: Comparator<E>) {
this.comparator = comparator;
}
@ -158,7 +159,7 @@ export class Heap<E> {
* @param order - Traversal order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
* @returns An array containing elements traversed in the specified order.
*/
dfs(order: HeapDFSOrderPattern): E[] {
dfs(order: DFSOrderPattern): E[] {
const result: E[] = [];
// Auxiliary recursive function, traverses the binary heap according to the traversal order
@ -227,7 +228,7 @@ export class Heap<E> {
* @param comparator - Comparison function.
* @returns A new Heap instance.
*/
static heapify<E>(nodes: E[], comparator: HeapComparator<E>): Heap<E> {
static heapify<E>(nodes: E[], comparator: Comparator<E>): Heap<E> {
const binaryHeap = new Heap<E>(comparator);
binaryHeap.nodes = [...nodes];
binaryHeap.fix(); // Fix heap properties

View file

@ -7,11 +7,11 @@
*/
import {Heap} from './heap';
import type {HeapComparator} from '../../types';
import type {Comparator} from '../../types';
export class MaxHeap<E = any> extends Heap<E> {
constructor(
comparator: HeapComparator<E> = (a: E, b: E) => {
comparator: Comparator<E> = (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 {

View file

@ -7,11 +7,11 @@
*/
import {Heap} from './heap';
import type {HeapComparator} from '../../types';
import type {Comparator} from '../../types';
export class MinHeap<E = any> extends Heap<E> {
constructor(
comparator: HeapComparator<E> = (a: E, b: E) => {
comparator: Comparator<E> = (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 {

View file

@ -6,11 +6,11 @@
* @license MIT License
*/
import {PriorityQueue} from './priority-queue';
import type {HeapComparator} from '../../types';
import type {Comparator} from '../../types';
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: HeapComparator<E> = (a: E, b: E) => {
compare: Comparator<E> = (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 {

View file

@ -6,11 +6,11 @@
* @license MIT License
*/
import {PriorityQueue} from './priority-queue';
import type {HeapComparator} from '../../types';
import type {Comparator} from '../../types';
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
constructor(
compare: HeapComparator<E> = (a: E, b: E) => {
compare: Comparator<E> = (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 {

View file

@ -7,10 +7,10 @@
*/
import {Heap} from '../heap';
import {HeapComparator} from '../../types';
import {Comparator} from '../../types';
export class PriorityQueue<E> extends Heap<E> {
constructor(comparator: HeapComparator<E>) {
constructor(comparator: Comparator<E>) {
super(comparator);
}
}

View file

@ -26,8 +26,6 @@ export type BinaryTreeNodePropertyName = 'key' | 'val';
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
export type DFSOrderPattern = 'in' | 'pre' | 'post';
export type BinaryTreeNodeKey = number;
export type BinaryTreeNodeProperty<N extends BinaryTreeNode<N['val'], N>> =

View file

@ -1,3 +1 @@
export type HeapComparator<T> = (a: T, b: T) => number;
export type HeapDFSOrderPattern = 'pre' | 'in' | 'post';
export {};

View file

@ -1 +1,4 @@
export {};
export type Comparator<T> = (a: T, b: T) => number;
// export enum DFSOrderPattern {'pre' = 'pre', 'in' = 'in', 'post' = 'post'}
export type DFSOrderPattern = 'pre' | 'in' | 'post';

View file

@ -1,7 +1,7 @@
import {HeapComparator, MaxHeap} from '../../../../src';
import {Comparator, MaxHeap} from '../../../../src';
describe('MaxHeap', () => {
const numberComparator: HeapComparator<number> = (a, b) => b - a;
const numberComparator: Comparator<number> = (a, b) => b - a;
let maxHeap: MaxHeap<number>;
beforeEach(() => {

View file

@ -1,7 +1,7 @@
import {HeapComparator, MinHeap} from '../../../../src';
import {Comparator, MinHeap} from '../../../../src';
describe('MinHeap', () => {
const numberComparator: HeapComparator<number> = (a, b) => a - b;
const numberComparator: Comparator<number> = (a, b) => a - b;
let minHeap: MinHeap<number>;
beforeEach(() => {