diff --git a/.prettierignore b/.prettierignore index 2fcb32b..1938daa 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,6 +1,5 @@ -src/types/data-structures/abstract-binary-tree.ts src/types/data-structures/binary-tree.ts src/types/data-structures/bst.ts src/types/data-structures/avl-tree.ts -src/types/data-structures/tree-multiset.ts src/types/data-structures/rb-tree.ts +src/types/data-structures/tree-multiset.ts diff --git a/src/data-structures/binary-tree/aa-tree.ts b/src/data-structures/binary-tree/aa-tree.ts deleted file mode 100644 index e6ed469..0000000 --- a/src/data-structures/binary-tree/aa-tree.ts +++ /dev/null @@ -1 +0,0 @@ -export class AaTree {} diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts deleted file mode 100644 index 2f8f22a..0000000 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * data-structure-typed - * - * @author Tyler Zeng - * @copyright Copyright (c) 2022 Tyler Zeng - * @license MIT License - */ - -import type {AbstractBinaryTreeNodeNested, BinaryTreeNodeKey} from '../../types'; -import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../../interfaces'; - -export abstract class AbstractBinaryTreeNode< - V = any, - FAMILY extends AbstractBinaryTreeNode = AbstractBinaryTreeNodeNested -> implements IAbstractBinaryTreeNode -{ - /** - * The constructor function initializes a BinaryTreeNode object with a key and an optional value. - * @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be - * stored in the binary tree node. If no value is provided, it will be set to undefined. - */ - protected constructor(val?: V) { - this.val = val; - } - - val: V | undefined; -} - -export abstract class AbstractBinaryTree = AbstractBinaryTreeNode> - implements IAbstractBinaryTree -{ - abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null; -} diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 9b30904..32cb1d1 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -7,12 +7,12 @@ */ import {BST, BSTNode} from './bst'; import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types'; -import {IAVLTree, IAVLTreeNode} from '../../interfaces'; +import {IBinaryTree} from '../../interfaces'; -export class AVLTreeNode = AVLTreeNodeNested> - extends BSTNode - implements IAVLTreeNode -{ +export class AVLTreeNode = AVLTreeNodeNested> extends BSTNode< + V, + FAMILY +> { height: number; constructor(key: BinaryTreeNodeKey, val?: V) { @@ -21,7 +21,7 @@ export class AVLTreeNode = AVLTre } } -export class AVLTree = AVLTreeNode> extends BST implements IAVLTree { +export class AVLTree = AVLTreeNode> extends BST implements IBinaryTree { /** * This is a constructor function for an AVL tree data structure in TypeScript. * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the diff --git a/src/data-structures/binary-tree/b-tree.ts b/src/data-structures/binary-tree/b-tree.ts deleted file mode 100644 index 875c0ad..0000000 --- a/src/data-structures/binary-tree/b-tree.ts +++ /dev/null @@ -1 +0,0 @@ -export class BTree {} diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 0aab87e..98f058e 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -6,12 +6,15 @@ * @license MIT License */ -import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types'; -import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree'; -import {IBinaryTree, IBinaryTreeNode} from '../../interfaces'; +import type { + BinaryTreeNodeKey, + BinaryTreeNodeNested, + BinaryTreeNodeProperties, + BinaryTreeNodeProperty, + BinaryTreeOptions +} from '../../types'; +import {IBinaryTree} from '../../interfaces'; import { - AbstractBinaryTreeNodeProperties, - AbstractBinaryTreeNodeProperty, BinaryTreeDeletedResult, BinaryTreeNodePropertyName, DFSOrderPattern, @@ -21,10 +24,7 @@ import { } from '../../types'; import {trampoline} from '../../utils'; -export class BinaryTreeNode = BinaryTreeNodeNested> - extends AbstractBinaryTreeNode - implements IBinaryTreeNode -{ +export class BinaryTreeNode = BinaryTreeNodeNested> { /** * The constructor function initializes a BinaryTreeNode object with a key and an optional value. * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier @@ -33,12 +33,14 @@ export class BinaryTreeNode = * stored in the binary tree node. If no value is provided, it will be set to undefined. */ constructor(key: BinaryTreeNodeKey, val?: V) { - super(val); this.key = key; + this.val = val; } key: BinaryTreeNodeKey; + val: V | undefined; + private _left: FAMILY | null | undefined; get left(): FAMILY | null | undefined { @@ -99,10 +101,7 @@ export class BinaryTreeNode = } } -export class BinaryTree = BinaryTreeNode> - extends AbstractBinaryTree - implements IBinaryTree -{ +export class BinaryTree = BinaryTreeNode> implements IBinaryTree { /** * This is a constructor function for a binary tree class that takes an optional options parameter. * @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the @@ -110,7 +109,6 @@ export class BinaryTree = BinaryTreeNode> * different configuration options. */ constructor(options?: BinaryTreeOptions) { - super(); if (options !== undefined) { const {loopType = LoopType.ITERATIVE} = options; this._loopType = loopType; @@ -228,7 +226,7 @@ export class BinaryTree = BinaryTreeNode> needInsert = null; } else if (typeof keyOrNode === 'number') { needInsert = this.createNode(keyOrNode, val); - } else if (keyOrNode instanceof AbstractBinaryTreeNode) { + } else if (keyOrNode instanceof BinaryTreeNode) { needInsert = keyOrNode; } else { return; @@ -271,7 +269,7 @@ export class BinaryTree = BinaryTreeNode> for (let i = 0; i < keysOrNodes.length; i++) { const keyOrNode = keysOrNodes[i]; - if (keyOrNode instanceof AbstractBinaryTreeNode) { + if (keyOrNode instanceof BinaryTreeNode) { inserted.push(this.add(keyOrNode.key, keyOrNode.val)); continue; } @@ -894,9 +892,9 @@ export class BinaryTree = BinaryTreeNode> * @param {NodeOrPropertyName} [nodeOrPropertyName] - An optional parameter that represents either a node or a property name. * If a node is provided, the bfs algorithm will be performed starting from that node. * If a property name is provided, the bfs algorithm will be performed starting from the root node, accumulating the specified property. - * @returns An instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`. + * @returns An instance of the `BinaryTreeNodeProperties` class with generic type `N`. */ - bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): AbstractBinaryTreeNodeProperties { + bfs(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { this._clearResults(); const queue: Array = [this.root]; @@ -954,12 +952,9 @@ export class BinaryTree = BinaryTreeNode> * each node based on the specified pattern and property name. * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'key'`. - * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name. + * @returns an instance of the BinaryTreeNodeProperties class, which contains the accumulated properties of the binary tree nodes based on the specified pattern and node or property name. */ - dfs( - pattern: DFSOrderPattern = 'in', - nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperties { + dfs(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { this._clearResults(); const _traverse = (node: N) => { switch (pattern) { @@ -1029,12 +1024,12 @@ export class BinaryTree = BinaryTreeNode> * specify the traversal pattern and the property name to accumulate results by. * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). * @param {NodeOrPropertyName} [nodeOrPropertyName] - The name of a property of the nodes in the binary tree. This property will be used to accumulate values during the depth-first search traversal. By default, it is set to `'key'`. - * @returns An object of type AbstractBinaryTreeNodeProperties. + * @returns An object of type BinaryTreeNodeProperties. */ dfsIterative( pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperties { + ): BinaryTreeNodeProperties { this._clearResults(); if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName); // 0: visit, 1: print @@ -1121,12 +1116,12 @@ export class BinaryTree = BinaryTreeNode> * can be either a `BinaryTreeNode` property name or the string `'key'`. If a property name is provided, the function * will accumulate results based on that property. If no property name is provided, the function will default to * accumulating results based on the 'key' property. - * @returns An object of type `AbstractBinaryTreeNodeProperties`. + * @returns An object of type `BinaryTreeNodeProperties`. */ levelIterative( node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperties { + ): BinaryTreeNodeProperties { if (!node) return []; this._clearResults(); @@ -1194,10 +1189,10 @@ export class BinaryTree = BinaryTreeNode> listLevels( node: N | null = this.root, nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperty[][] { + ): BinaryTreeNodeProperty[][] { if (!node) return []; - const levelsNodes: AbstractBinaryTreeNodeProperty[][] = []; + const levelsNodes: BinaryTreeNodeProperty[][] = []; const collectByProperty = (node: N, level: number) => { switch (nodeOrPropertyName) { @@ -1307,12 +1302,9 @@ export class BinaryTree = BinaryTreeNode> * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm. * @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order). * @param {NodeOrPropertyName} [nodeOrPropertyName] - The property name of the nodes to retrieve or perform operations on during the traversal. It can be any valid property name of the nodes in the binary tree. If not provided, it defaults to 'key'. - * @returns An array of AbstractBinaryTreeNodeProperties objects. + * @returns An array of BinaryTreeNodeProperties objects. */ - morris( - pattern: DFSOrderPattern = 'in', - nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperties { + morris(pattern: DFSOrderPattern = 'in', nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { if (this.root === null) return []; this._clearResults(); @@ -1545,11 +1537,9 @@ export class BinaryTree = BinaryTreeNode> * name. * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that * can accept either a `NodeOrPropertyName` type or be undefined. - * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties`. + * @returns The method `_getResultByPropertyName` returns an instance of `BinaryTreeNodeProperties`. */ - protected _getResultByPropertyName( - nodeOrPropertyName: NodeOrPropertyName = 'key' - ): AbstractBinaryTreeNodeProperties { + protected _getResultByPropertyName(nodeOrPropertyName: NodeOrPropertyName = 'key'): BinaryTreeNodeProperties { switch (nodeOrPropertyName) { case 'key': return this.visitedKey; diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 3da301f..22b1af6 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -14,18 +14,15 @@ import type { } from '../../types'; import {CP, LoopType} from '../../types'; import {BinaryTree, BinaryTreeNode} from './binary-tree'; -import {IBST, IBSTNode} from '../../interfaces'; +import {IBinaryTree} from '../../interfaces'; -export class BSTNode = BSTNodeNested> - extends BinaryTreeNode - implements IBSTNode -{ +export class BSTNode = BSTNodeNested> extends BinaryTreeNode { constructor(key: BinaryTreeNodeKey, val?: V) { super(key, val); } } -export class BST = BSTNode> extends BinaryTree implements IBST { +export class BST = BSTNode> extends BinaryTree implements IBinaryTree { /** * The constructor function initializes a binary search tree object with an optional comparator function. * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree. diff --git a/src/data-structures/binary-tree/index.ts b/src/data-structures/binary-tree/index.ts index b1c908c..87b06d3 100644 --- a/src/data-structures/binary-tree/index.ts +++ b/src/data-structures/binary-tree/index.ts @@ -1,12 +1,7 @@ -export * from './abstract-binary-tree'; export * from './binary-tree'; export * from './bst'; export * from './binary-indexed-tree'; export * from './segment-tree'; export * from './avl-tree'; -export * from './b-tree'; export * from './rb-tree'; -export * from './splay-tree'; -export * from './aa-tree'; export * from './tree-multiset'; -export * from './two-three-tree'; diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 76073e6..7266828 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -1,11 +1,11 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../../types'; -import {IRBTree, IRBTreeNode} from '../../interfaces'; +import {IBinaryTree} from '../../interfaces'; import {BST, BSTNode} from './bst'; -export class RBTreeNode = RBTreeNodeNested> - extends BSTNode - implements IRBTreeNode -{ +export class RBTreeNode = RBTreeNodeNested> extends BSTNode< + V, + FAMILY +> { private _color: RBColor; constructor(key: BinaryTreeNodeKey, val?: V) { @@ -22,7 +22,7 @@ export class RBTreeNode = RBTreeNo } } -export class RBTree = RBTreeNode> extends BST implements IRBTree { +export class RBTree = RBTreeNode> extends BST implements IBinaryTree { constructor(options?: RBTreeOptions) { super(options); } diff --git a/src/data-structures/binary-tree/splay-tree.ts b/src/data-structures/binary-tree/splay-tree.ts deleted file mode 100644 index ff8c3bb..0000000 --- a/src/data-structures/binary-tree/splay-tree.ts +++ /dev/null @@ -1 +0,0 @@ -export class SplayTree {} diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 1aac2a0..f048cbf 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -7,13 +7,13 @@ */ import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types'; import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types'; -import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces'; +import {IBinaryTree} from '../../interfaces'; import {AVLTree, AVLTreeNode} from './avl-tree'; -export class TreeMultisetNode = TreeMultisetNodeNested> - extends AVLTreeNode - implements ITreeMultisetNode -{ +export class TreeMultisetNode< + V = any, + FAMILY extends TreeMultisetNode = TreeMultisetNodeNested +> extends AVLTreeNode { /** * The constructor function initializes a BinaryTreeNode object with a key, value, and count. * @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier @@ -37,7 +37,7 @@ export class TreeMultisetNode = TreeMultisetNode> extends AVLTree - implements ITreeMultiset + implements IBinaryTree { /** * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to diff --git a/src/data-structures/binary-tree/two-three-tree.ts b/src/data-structures/binary-tree/two-three-tree.ts deleted file mode 100644 index cf6f983..0000000 --- a/src/data-structures/binary-tree/two-three-tree.ts +++ /dev/null @@ -1 +0,0 @@ -export class TwoThreeTree {} diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 604c981..92b5380 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -8,7 +8,7 @@ import {arrayRemove, uuidV4} from '../../utils'; import {PriorityQueue} from '../priority-queue'; import type {DijkstraResult, VertexKey} from '../../types'; -import {IAbstractGraph} from '../../interfaces'; +import {IGraph} from '../../interfaces'; export abstract class AbstractVertex { /** @@ -104,7 +104,7 @@ export abstract class AbstractEdge { export abstract class AbstractGraph< V extends AbstractVertex = AbstractVertex, E extends AbstractEdge = AbstractEdge -> implements IAbstractGraph +> implements IGraph { private _vertices: Map = new Map(); diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 6563833..438ea75 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -8,7 +8,7 @@ import {arrayRemove} from '../../utils'; import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph'; import type {TopologicalStatus, VertexKey} from '../../types'; -import {IDirectedGraph} from '../../interfaces'; +import {IGraph} from '../../interfaces'; export class DirectedVertex extends AbstractVertex { /** @@ -64,7 +64,7 @@ export class DirectedEdge extends AbstractEdge { export class DirectedGraph = DirectedVertex, E extends DirectedEdge = DirectedEdge> extends AbstractGraph - implements IDirectedGraph + implements IGraph { /** * The constructor function initializes an instance of a class. diff --git a/src/data-structures/graph/map-graph.ts b/src/data-structures/graph/map-graph.ts index 495eb52..acfa6fa 100644 --- a/src/data-structures/graph/map-graph.ts +++ b/src/data-structures/graph/map-graph.ts @@ -126,7 +126,7 @@ export class MapGraph = MapVertex, E extends MapEd * If the weight is not provided, it can be set to a default value or left undefined. * @param [val] - The `val` parameter is an optional value that can be assigned to the edge. It can be of any type, * depending on the specific implementation of the `MapEdge` class. - * @returns a new instance of the `MapEdge` class, casted as type `E`. + * @returns a new instance of the `MapEdge` class, cast as type `E`. */ override createEdge(src: VertexKey, dest: VertexKey, weight?: number, val?: E['val']): E { return new MapEdge(src, dest, weight, val) as E; diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 6842018..b962047 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -8,7 +8,7 @@ import {arrayRemove} from '../../utils'; import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph'; import type {VertexKey} from '../../types'; -import {IUNDirectedGraph} from '../../interfaces'; +import {IGraph} from '../../interfaces'; export class UndirectedVertex extends AbstractVertex { /** @@ -55,7 +55,7 @@ export class UndirectedGraph< E extends UndirectedEdge = UndirectedEdge > extends AbstractGraph - implements IUNDirectedGraph + implements IGraph { /** * The constructor initializes a new Map object to store edges. diff --git a/src/data-structures/hash/index.ts b/src/data-structures/hash/index.ts index 326410c..728fd5f 100644 --- a/src/data-structures/hash/index.ts +++ b/src/data-structures/hash/index.ts @@ -1,7 +1,6 @@ export * from './hash-table'; export * from './coordinate-map'; export * from './coordinate-set'; -export * from './pair'; export * from './tree-map'; export * from './tree-set'; export * from './hash-map'; diff --git a/src/data-structures/hash/pair.ts b/src/data-structures/hash/pair.ts deleted file mode 100644 index 273e5e9..0000000 --- a/src/data-structures/hash/pair.ts +++ /dev/null @@ -1 +0,0 @@ -export class Pair {} diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index cd5b13b..c6e0afd 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -5,13 +5,13 @@ * @license MIT License */ -import type {CompareFunction} from '../../types'; +import type {HeapComparator, HeapDFSOrderPattern} from '../../types'; export class Heap { protected nodes: E[] = []; - private readonly comparator: CompareFunction; + private readonly comparator: HeapComparator; - constructor(comparator: CompareFunction) { + constructor(comparator: HeapComparator) { this.comparator = comparator; } @@ -158,7 +158,7 @@ export class Heap { * @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: 'in' | 'pre' | 'post'): E[] { + dfs(order: HeapDFSOrderPattern): E[] { const result: E[] = []; // Auxiliary recursive function, traverses the binary heap according to the traversal order @@ -227,7 +227,7 @@ export class Heap { * @param comparator - Comparison function. * @returns A new Heap instance. */ - static heapify(nodes: E[], comparator: CompareFunction): Heap { + static heapify(nodes: E[], comparator: HeapComparator): Heap { const binaryHeap = new Heap(comparator); binaryHeap.nodes = [...nodes]; binaryHeap.fix(); // Fix heap properties diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index 99007ce..410b166 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -7,11 +7,11 @@ */ import {Heap} from './heap'; -import type {CompareFunction} from '../../types'; +import type {HeapComparator} from '../../types'; export class MaxHeap extends Heap { constructor( - comparator: CompareFunction = (a: E, b: E) => { + comparator: HeapComparator = (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 { diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index d50d309..2998199 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -7,11 +7,11 @@ */ import {Heap} from './heap'; -import type {CompareFunction} from '../../types'; +import type {HeapComparator} from '../../types'; export class MinHeap extends Heap { constructor( - comparator: CompareFunction = (a: E, b: E) => { + comparator: HeapComparator = (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 { diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index ed28548..2418a76 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -6,11 +6,11 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {CompareFunction} from '../../types'; +import type {HeapComparator} from '../../types'; export class MaxPriorityQueue extends PriorityQueue { constructor( - compare: CompareFunction = (a: E, b: E) => { + compare: HeapComparator = (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 { diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index 9a41c03..9635a1d 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -6,11 +6,11 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {CompareFunction} from '../../types'; +import type {HeapComparator} from '../../types'; export class MinPriorityQueue extends PriorityQueue { constructor( - compare: CompareFunction = (a: E, b: E) => { + compare: HeapComparator = (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 { diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index a664246..9f4585c 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -7,10 +7,10 @@ */ import {Heap} from '../heap'; -import {CompareFunction} from '../../types'; +import {HeapComparator} from '../../types'; export class PriorityQueue extends Heap { - constructor(comparator: CompareFunction) { + constructor(comparator: HeapComparator) { super(comparator); } } diff --git a/src/interfaces/abstract-binary-tree.ts b/src/interfaces/abstract-binary-tree.ts deleted file mode 100644 index b3fd59d..0000000 --- a/src/interfaces/abstract-binary-tree.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {BinaryTreeNodeKey} from '../types'; -import {AbstractBinaryTreeNode} from '../data-structures'; - -export interface IAbstractBinaryTreeNode> {} - -export interface IAbstractBinaryTree> { - createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N | null; -} diff --git a/src/interfaces/avl-tree.ts b/src/interfaces/avl-tree.ts deleted file mode 100644 index a2ce1d4..0000000 --- a/src/interfaces/avl-tree.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {AVLTreeNode} from '../data-structures'; -import {IBST, IBSTNode} from './bst'; - -export interface IAVLTreeNode> extends IBSTNode { - height: number; -} - -export interface IAVLTree> extends IBST {} diff --git a/src/interfaces/binary-tree.ts b/src/interfaces/binary-tree.ts index 45d9ac6..6f159fb 100644 --- a/src/interfaces/binary-tree.ts +++ b/src/interfaces/binary-tree.ts @@ -1,7 +1,10 @@ import {BinaryTreeNode} from '../data-structures'; -import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from './abstract-binary-tree'; +import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types'; -export interface IBinaryTreeNode> - extends IAbstractBinaryTreeNode {} +export interface IBinaryTree> { + createNode(key: BinaryTreeNodeKey, val?: N['val']): N; -export interface IBinaryTree> extends IAbstractBinaryTree {} + add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined; + + remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult[]; +} diff --git a/src/interfaces/bst.ts b/src/interfaces/bst.ts deleted file mode 100644 index 50bac41..0000000 --- a/src/interfaces/bst.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {BSTNode} from '../data-structures'; -import {IBinaryTree, IBinaryTreeNode} from './binary-tree'; - -export interface IBSTNode> extends IBinaryTreeNode {} - -export interface IBST> extends IBinaryTree {} diff --git a/src/interfaces/directed-graph.ts b/src/interfaces/directed-graph.ts deleted file mode 100644 index e6b38ba..0000000 --- a/src/interfaces/directed-graph.ts +++ /dev/null @@ -1,3 +0,0 @@ -import {IAbstractGraph} from './abstract-graph'; - -export interface IDirectedGraph extends IAbstractGraph {} diff --git a/src/interfaces/abstract-graph.ts b/src/interfaces/graph.ts similarity index 82% rename from src/interfaces/abstract-graph.ts rename to src/interfaces/graph.ts index 73addcd..dd65e90 100644 --- a/src/interfaces/abstract-graph.ts +++ b/src/interfaces/graph.ts @@ -1,6 +1,6 @@ import {VertexKey} from '../types'; -export interface IAbstractGraph { +export interface IGraph { createVertex(key: VertexKey, val?: V): V; createEdge(srcOrV1: VertexKey | string, destOrV2: VertexKey | string, weight?: number, val?: E): E; diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index e06e8b6..6aeeddf 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -1,15 +1,8 @@ -export * from './abstract-binary-tree'; -export * from './abstract-graph'; -export * from './avl-tree'; +export * from './graph'; export * from './binary-tree'; -export * from './bst'; -export * from './directed-graph'; export * from './doubly-linked-list'; export * from './heap'; export * from './navigator'; export * from './priority-queue'; -export * from './rb-tree'; export * from './segment-tree'; export * from './singly-linked-list'; -export * from './tree-multiset'; -export * from './undirected-graph'; diff --git a/src/interfaces/rb-tree.ts b/src/interfaces/rb-tree.ts deleted file mode 100644 index 5c2ead5..0000000 --- a/src/interfaces/rb-tree.ts +++ /dev/null @@ -1,6 +0,0 @@ -import {RBTreeNode} from '../data-structures'; -import {IBST, IBSTNode} from './bst'; - -export interface IRBTreeNode> extends IBSTNode {} - -export interface IRBTree> extends IBST {} diff --git a/src/interfaces/tree-multiset.ts b/src/interfaces/tree-multiset.ts deleted file mode 100644 index f3955ac..0000000 --- a/src/interfaces/tree-multiset.ts +++ /dev/null @@ -1,7 +0,0 @@ -import {TreeMultisetNode} from '../data-structures'; -import {IAVLTree, IAVLTreeNode} from './avl-tree'; - -export interface ITreeMultisetNode> - extends IAVLTreeNode {} - -export interface ITreeMultiset> extends IAVLTree {} diff --git a/src/interfaces/undirected-graph.ts b/src/interfaces/undirected-graph.ts deleted file mode 100644 index bc08f80..0000000 --- a/src/interfaces/undirected-graph.ts +++ /dev/null @@ -1,3 +0,0 @@ -import {IAbstractGraph} from './abstract-graph'; - -export interface IUNDirectedGraph extends IAbstractGraph {} diff --git a/src/types/data-structures/abstract-binary-tree.ts b/src/types/data-structures/abstract-binary-tree.ts deleted file mode 100644 index 3ccc0f6..0000000 --- a/src/types/data-structures/abstract-binary-tree.ts +++ /dev/null @@ -1,49 +0,0 @@ -import {AbstractBinaryTreeNode} from '../../data-structures'; - -/** - * Enum representing different loop types. - * - * - `iterative`: Indicates the iterative loop type (with loops that use iterations). - * - `recursive`: Indicates the recursive loop type (with loops that call themselves). - */ - -export enum LoopType { - ITERATIVE = 'ITERATIVE', - RECURSIVE = 'RECURSIVE' -} - -export enum FamilyPosition { - ROOT = 'ROOT', - LEFT = 'LEFT', - RIGHT = 'RIGHT', - ROOT_LEFT = 'ROOT_LEFT', - ROOT_RIGHT = 'ROOT_RIGHT', - ISOLATED = 'ISOLATED', - MAL_NODE = 'MAL_NODE' -} - -export type BinaryTreeNodePropertyName = 'key' | 'val'; - -export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName; - -export type DFSOrderPattern = 'in' | 'pre' | 'post'; - -export type BinaryTreeNodeKey = number; - -export type BinaryTreeDeletedResult = { deleted: N | null | undefined; needBalanced: N | null }; - -export type AbstractBinaryTreeNodeProperty> = - | N['val'] - | N - | number - | BinaryTreeNodeKey; - - -export type AbstractBinaryTreeNodeProperties> = - AbstractBinaryTreeNodeProperty[]; - -export type AbstractBinaryTreeNodeNested = AbstractBinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>; - -export type AbstractBinaryTreeOptions = { - -}; diff --git a/src/types/data-structures/abstract-graph.ts b/src/types/data-structures/abstract-graph.ts index 41dd860..bd90c43 100644 --- a/src/types/data-structures/abstract-graph.ts +++ b/src/types/data-structures/abstract-graph.ts @@ -1,5 +1,5 @@ export type VertexKey = string | number; -export type EdgeKey = string; + export type DijkstraResult = { distMap: Map; distPaths?: Map; diff --git a/src/types/data-structures/binary-tree.ts b/src/types/data-structures/binary-tree.ts index 27b18d9..a5f8f04 100644 --- a/src/types/data-structures/binary-tree.ts +++ b/src/types/data-structures/binary-tree.ts @@ -1,5 +1,45 @@ import {BinaryTreeNode} from '../../data-structures/binary-tree'; -import {AbstractBinaryTreeOptions, LoopType} from './abstract-binary-tree'; + +/** + * Enum representing different loop types. + * + * - `iterative`: Indicates the iterative loop type (with loops that use iterations). + * - `recursive`: Indicates the recursive loop type (with loops that call themselves). + */ + +export enum LoopType { + ITERATIVE = 'ITERATIVE', + RECURSIVE = 'RECURSIVE' +} + +export enum FamilyPosition { + ROOT = 'ROOT', + LEFT = 'LEFT', + RIGHT = 'RIGHT', + ROOT_LEFT = 'ROOT_LEFT', + ROOT_RIGHT = 'ROOT_RIGHT', + ISOLATED = 'ISOLATED', + MAL_NODE = 'MAL_NODE' +} + +export type BinaryTreeNodePropertyName = 'key' | 'val'; + +export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName; + +export type DFSOrderPattern = 'in' | 'pre' | 'post'; + +export type BinaryTreeNodeKey = number; + +export type BinaryTreeNodeProperty> = + | N['val'] + | N + | number + | BinaryTreeNodeKey; +export type BinaryTreeDeletedResult = { deleted: N | null | undefined; needBalanced: N | null }; + +export type BinaryTreeNodeProperties> = + BinaryTreeNodeProperty[]; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> -export type BinaryTreeOptions = AbstractBinaryTreeOptions & { loopType?: LoopType } + +export type BinaryTreeOptions = { loopType?: LoopType } diff --git a/src/types/data-structures/bst.ts b/src/types/data-structures/bst.ts index b05ef34..88bb72a 100644 --- a/src/types/data-structures/bst.ts +++ b/src/types/data-structures/bst.ts @@ -1,11 +1,11 @@ import {BSTNode} from '../../data-structures/binary-tree'; -import type {BinaryTreeOptions} from './binary-tree'; -import {BinaryTreeNodeKey} from './abstract-binary-tree'; +import type {BinaryTreeNodeKey, BinaryTreeOptions} from './binary-tree'; export type BSTComparator = (a: BinaryTreeNodeKey, b: BinaryTreeNodeKey) => number; // prettier-ignore export type BSTNodeNested = BSTNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + export type BSTOptions = BinaryTreeOptions & { comparator?: BSTComparator, } diff --git a/src/types/data-structures/heap.ts b/src/types/data-structures/heap.ts index 3461df6..55bf2b6 100644 --- a/src/types/data-structures/heap.ts +++ b/src/types/data-structures/heap.ts @@ -1 +1,3 @@ -export type CompareFunction = (a: T, b: T) => number; +export type HeapComparator = (a: T, b: T) => number; + +export type HeapDFSOrderPattern = 'pre' | 'in' | 'post'; diff --git a/src/types/data-structures/index.ts b/src/types/data-structures/index.ts index 54ef693..8a60614 100644 --- a/src/types/data-structures/index.ts +++ b/src/types/data-structures/index.ts @@ -5,10 +5,8 @@ export * from './segment-tree'; export * from './tree-multiset'; export * from './abstract-graph'; export * from './map-graph'; -export * from './abstract-binary-tree'; export * from './rb-tree'; export * from './directed-graph'; -export * from './priority-queue'; export * from './heap'; export * from './singly-linked-list'; export * from './doubly-linked-list'; diff --git a/src/types/data-structures/navigator.ts b/src/types/data-structures/navigator.ts index 6555f58..9d8b9a9 100644 --- a/src/types/data-structures/navigator.ts +++ b/src/types/data-structures/navigator.ts @@ -1,4 +1,5 @@ export type Direction = 'up' | 'right' | 'down' | 'left'; + export type Turning = {[key in Direction]: Direction}; export type NavigatorParams = { diff --git a/src/types/data-structures/priority-queue.ts b/src/types/data-structures/priority-queue.ts deleted file mode 100644 index e64296b..0000000 --- a/src/types/data-structures/priority-queue.ts +++ /dev/null @@ -1,9 +0,0 @@ -export type PriorityQueueComparator = (a: T, b: T) => number; - -export type PriorityQueueOptions = { - nodes?: T[]; - isFix?: boolean; - comparator: PriorityQueueComparator; -}; - -export type PriorityQueueDFSOrderPattern = 'pre' | 'in' | 'post'; diff --git a/test/unit/data-structures/heap/max-heap.test.ts b/test/unit/data-structures/heap/max-heap.test.ts index 6b120fb..744406f 100644 --- a/test/unit/data-structures/heap/max-heap.test.ts +++ b/test/unit/data-structures/heap/max-heap.test.ts @@ -1,7 +1,7 @@ -import {CompareFunction, MaxHeap} from '../../../../src'; +import {HeapComparator, MaxHeap} from '../../../../src'; describe('MaxHeap', () => { - const numberComparator: CompareFunction = (a, b) => b - a; + const numberComparator: HeapComparator = (a, b) => b - a; let maxHeap: MaxHeap; beforeEach(() => { diff --git a/test/unit/data-structures/heap/min-heap.test.ts b/test/unit/data-structures/heap/min-heap.test.ts index 14de30f..2425162 100644 --- a/test/unit/data-structures/heap/min-heap.test.ts +++ b/test/unit/data-structures/heap/min-heap.test.ts @@ -1,7 +1,7 @@ -import {CompareFunction, MinHeap} from '../../../../src'; +import {HeapComparator, MinHeap} from '../../../../src'; describe('MinHeap', () => { - const numberComparator: CompareFunction = (a, b) => a - b; + const numberComparator: HeapComparator = (a, b) => a - b; let minHeap: MinHeap; beforeEach(() => {