diff --git a/package.json b/package.json index 58deca1..c4025e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.18.6", + "version": "1.18.7", "description": "Explore our comprehensive Javascript Data Structure / TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.", "main": "dist/index.js", "scripts": { diff --git a/src/data-structures/binary-tree/abstract-binary-tree.ts b/src/data-structures/binary-tree/abstract-binary-tree.ts index 4622d58..1f4f02a 100644 --- a/src/data-structures/binary-tree/abstract-binary-tree.ts +++ b/src/data-structures/binary-tree/abstract-binary-tree.ts @@ -8,9 +8,9 @@ import {trampoline} from '../../utils'; import type { - AbstractRecursiveBinaryTreeNode, - AbstractBinaryTreeNodeProperty, AbstractBinaryTreeNodeProperties, + AbstractBinaryTreeNodeProperty, + AbstractRecursiveBinaryTreeNode, BinaryTreeDeletedResult, BinaryTreeNodeId, BinaryTreeNodePropertyName, @@ -21,9 +21,18 @@ import type { import {AbstractBinaryTreeOptions, FamilyPosition, LoopType} from '../types'; import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../interfaces'; -export abstract class AbstractBinaryTreeNode = AbstractRecursiveBinaryTreeNode> implements IAbstractBinaryTreeNode { +export abstract class AbstractBinaryTreeNode = AbstractRecursiveBinaryTreeNode> implements IAbstractBinaryTreeNode { - constructor(id: BinaryTreeNodeId, val: T, count?: number) { + /** + * The constructor function initializes a BinaryTreeNode object with an id, value, and count. + * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier + * for the binary tree node. + * @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value of the binary + * tree node. If no value is provided, it will be `undefined`. + * @param {number} [count] - The `count` parameter is an optional parameter that represents the number of times the + * value `val` appears in the binary tree node. If the `count` parameter is not provided, it defaults to 1. + */ + constructor(id: BinaryTreeNodeId, val?: T, count?: number) { this._id = id; this._val = val; this._count = count ?? 1; @@ -39,14 +48,14 @@ export abstract class AbstractBinaryTreeNode = RecursiveAVLTreeNode> extends BSTNode implements IAVLTreeNode { - + override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { + return new AVLTreeNode(id, (val === undefined ? id : val) as T, count) as FAMILY; + } } export class AVLTree = AVLTreeNode> extends BST implements IAVLTree { @@ -18,9 +20,8 @@ export class AVLTree = AVLTreeNode> e super(options); } - override createNode(id: BinaryTreeNodeId, val: N['val'], count?: number): N { - const node = new AVLTreeNode(id, val, count); - return node as N; + override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N { + return new AVLTreeNode(id, (val === undefined ? id : val), count) as N; } /** diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index c43b357..745949d 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -8,14 +8,23 @@ import type {BinaryTreeNodeId, RecursiveBinaryTreeNode} from '../types'; import {BinaryTreeOptions} from '../types'; -import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../interfaces'; import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree'; import {IBinaryTree, IBinaryTreeNode} from '../interfaces/binary-tree'; export class BinaryTreeNode = RecursiveBinaryTreeNode> extends AbstractBinaryTreeNode implements IBinaryTreeNode { - createNode(id: BinaryTreeNodeId, val: T | null, count?: number): FAMILY | null { - return val !== null ? new BinaryTreeNode(id, val, count) as FAMILY : null; + /** + * The function creates a new binary tree node with an optional value and count, and returns it as a specified type. + * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type + * `BinaryTreeNodeId`, which could be a string or a number depending on how you want to identify your nodes. + * @param {T} [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the + * value stored in the node. + * @param {number} [count] - The count parameter is an optional parameter that represents the number of times the value + * appears in the binary tree node. + * @returns a new instance of the BinaryTreeNode class, casted as the FAMILY type. + */ + createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { + return new BinaryTreeNode(id, (val === undefined ? id : val) as T, count) as FAMILY; } } @@ -43,9 +52,8 @@ export class BinaryTree = BinaryTreeNode> * of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`. * @returns a BinaryTreeNode object if the value is not null, otherwise it returns null. */ - createNode(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null { - const node = new BinaryTreeNode(id, val, count); - return node as N | null; + createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N { + return new BinaryTreeNode(id, val === undefined ? id : val, count) as N; } } \ No newline at end of file diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 5f5d78d..59a955a 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -8,10 +8,22 @@ import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, RecursiveBSTNode} from '../types'; import {BinaryTreeDeletedResult, BSTOptions, CP, FamilyPosition, LoopType} from '../types'; import {BinaryTree, BinaryTreeNode} from './binary-tree'; -import {IAbstractBinaryTree, IAbstractBinaryTreeNode, IBST, IBSTNode} from '../interfaces'; +import {IBST, IBSTNode} from '../interfaces'; export class BSTNode = RecursiveBSTNode> extends BinaryTreeNode implements IBSTNode { - + /** + * The function creates a new binary search tree node with the specified id, value, and count. + * @param {BinaryTreeNodeId} id - The id parameter is the identifier for the binary tree node. It is used to uniquely + * identify each node in the tree. + * @param {T} [val] - The "val" parameter represents the value that will be stored in the binary tree node. It is an + * optional parameter, meaning it can be omitted when calling the "createNode" function. + * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary + * search tree node. It is an optional parameter, so it can be omitted when calling the `createNode` method. + * @returns The method is returning a new instance of the BSTNode class, casted as the FAMILY type. + */ + override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { + return new BSTNode(id, (val === undefined ? id : val) as T, count) as FAMILY; + } } export class BST = BSTNode> extends BinaryTree implements IBST { @@ -29,9 +41,18 @@ export class BST = BSTNode> extends Binar } } - override createNode(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null { - const node = val !== null ? new BSTNode(id, val, count) : null; - return node as N; + /** + * The function creates a new binary search tree node with the given id, value, and count. + * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type + * `BinaryTreeNodeId`. + * @param {N['val'] | null} [val] - The `val` parameter is the value that will be stored in the node. It can be of any + * type `N['val']` or `null`. + * @param {number} [count] - The `count` parameter is an optional parameter that represents the number of occurrences + * of a particular value in the binary search tree node. + * @returns a new instance of the BSTNode class, casted as type N. + */ + override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N { + return new BSTNode(id, val === undefined ? id : val, count) as N; } /** diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 93cc75c..6495a1c 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -1,14 +1,9 @@ -import {BinaryTreeNode} from './binary-tree'; -import {RBColor, RBTreeOptions} from '../types'; +import {BinaryTreeNodeId, RBColor, RBTreeOptions} from '../types'; import {IRBTree, IRBTreeNode} from '../interfaces/rb-tree'; -import {BST} from './bst'; +import {BST, BSTNode} from './bst'; -export class RBTreeNode> extends BinaryTreeNode implements IRBTreeNode { - // override createNode(id: BinaryTreeNodeId, val: T | null, count?: number): RBNode | null { - // return val !== null ? new RBNode(id, val, count) : null; - // } - +export class RBTreeNode> extends BSTNode implements IRBTreeNode { constructor(id: number, val: T, count?: number) { super(id, val, count); } @@ -23,6 +18,20 @@ export class RBTreeNode> extends BinaryT this._color = value; } + /** + * The function creates a new RBTreeNode with the given id, value, and count and returns it as a FAMILY object. + * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely + * identify each node in the tree. + * @param {T | null} [val] - The "val" parameter represents the value to be stored in the node. It can be of type T + * (generic type) or null. + * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree + * node. + * @returns The method is returning a new instance of the RBTreeNode class, casted as a FAMILY type. + */ + override createNode(id: BinaryTreeNodeId, val?: T | null, count?: number): FAMILY { + return new RBTreeNode(id, val, count) as FAMILY; + } + // private override _parent: RBNode | null; // override set parent(v: RBNode | null) { // this._parent = v; @@ -64,9 +73,9 @@ export class RBTree> extends BST implements super(options); } - // override createNode(id: BinaryTreeNodeId, val: N | null, count?: number): RBNode | null { - // return val !== null ? new RBNode(id, val, count) : null; - // } + override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N { + return new RBTreeNode(id, val, count) as N; + } // private override _root: BinaryTreeNode | null = null; // diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 35b8fa0..8797f5f 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -7,10 +7,22 @@ */ import {BST, BSTNode} from './bst'; import type {BinaryTreeNodeId, RecursiveTreeMultiSetNode, TreeMultiSetOptions} from '../types'; -import {IAbstractBinaryTree, IAbstractBinaryTreeNode, IBST, IBSTNode} from '../interfaces'; +import {IBST, IBSTNode} from '../interfaces'; export class TreeMultiSetNode = RecursiveTreeMultiSetNode> extends BSTNode implements IBSTNode { - + /** + * The function creates a new node in a binary tree with an optional value and count. + * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely + * identify each node in the tree. + * @param {T} [val] - The `val` parameter represents the value to be stored in the node. It is an optional parameter, + * meaning it can be omitted when calling the `createNode` method. + * @param {number} [count] - The `count` parameter represents the number of occurrences of the value in the binary tree + * node. It is an optional parameter, so it can be omitted when calling the `createNode` method. + * @returns The method is returning a new instance of the TreeMultiSetNode class, casted as the FAMILY type. + */ + override createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY { + return new TreeMultiSetNode(id, (val === undefined ? id : val) as T, count) as FAMILY; + } } /** @@ -30,8 +42,7 @@ export class TreeMultiSet = BSTNode> exte * occurrences of the value in the binary search tree node. If not provided, the count will default to 1. * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided). */ - override createNode(id: BinaryTreeNodeId, val: N['val'], count?: number): N { - const node = new TreeMultiSetNode(id, val, count); - return node as N; + override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N { + return new TreeMultiSetNode(id, val === undefined ? id : val, count) as N; } } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 261df3c..9f842ff 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -123,19 +123,10 @@ export abstract class AbstractGraph, E extends Abs abstract removeEdge(edge: E): E | null; - protected _getVertex(vertexOrId: VertexId | V): V | null { - const vertexId = this._getVertexId(vertexOrId); - return this._vertices.get(vertexId) || null; - } - getVertex(vertexId: VertexId): V | null { return this._vertices.get(vertexId) || null; } - protected _getVertexId(vertexOrId: V | VertexId): VertexId { - return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId; - } - /** * The function checks if a vertex exists in a graph. * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID @@ -666,12 +657,6 @@ export abstract class AbstractGraph, E extends Abs return {distMap, preMap, seen, paths, minDist, minPath}; } - /** - * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes. - * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges. - * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower. - */ - /** * Dijkstra algorithm time: O(logVE) space: O(V + E) * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative. @@ -784,10 +769,9 @@ export abstract class AbstractGraph, E extends Abs } /** - * BellmanFord time:O(VE) space:O(V) - * one to rest pairs - * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios. - * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to + * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes. + * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges. + * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower. */ /** @@ -837,12 +821,6 @@ export abstract class AbstractGraph, E extends Abs } - /** - * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle - * all pairs - * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes. - */ - /** * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs. * Tarjan can find cycles in directed or undirected graph @@ -967,6 +945,27 @@ export abstract class AbstractGraph, E extends Abs return {dfnMap, lowMap, bridges, articulationPoints, SCCs, cycles}; } + /** + * BellmanFord time:O(VE) space:O(V) + * one to rest pairs + * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios. + * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to + */ + + protected _getVertex(vertexOrId: VertexId | V): V | null { + const vertexId = this._getVertexId(vertexOrId); + return this._vertices.get(vertexId) || null; + } + + /** + * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle + * all pairs + * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes. + */ + + protected _getVertexId(vertexOrId: V | VertexId): VertexId { + return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId; + } /**--- start find cycles --- */ diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 005f33c..58e5e7d 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, VertexId} from '../types'; -import {IDirectedGraph, IAbstractGraph} from '../interfaces'; +import {IDirectedGraph} from '../interfaces'; export class DirectedVertex extends AbstractVertex { /** diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 2930738..260f213 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -50,7 +50,7 @@ export class UndirectedEdge extends AbstractEdge { } } -export class UndirectedGraph = UndirectedVertex, E extends UndirectedEdge = UndirectedEdge> extends AbstractGraph implements IUNDirectedGraph{ +export class UndirectedGraph = UndirectedVertex, E extends UndirectedEdge = UndirectedEdge> extends AbstractGraph implements IUNDirectedGraph { constructor() { super(); diff --git a/src/data-structures/interfaces/abstract-binary-tree.ts b/src/data-structures/interfaces/abstract-binary-tree.ts index 33fba8e..8f88275 100644 --- a/src/data-structures/interfaces/abstract-binary-tree.ts +++ b/src/data-structures/interfaces/abstract-binary-tree.ts @@ -1,6 +1,6 @@ import { - AbstractBinaryTreeNodeProperty, AbstractBinaryTreeNodeProperties, + AbstractBinaryTreeNodeProperty, BinaryTreeDeletedResult, BinaryTreeNodeId, BinaryTreeNodePropertyName, @@ -13,44 +13,36 @@ import {AbstractBinaryTreeNode} from '../binary-tree'; export interface IAbstractBinaryTreeNode> { - createNode(id: BinaryTreeNodeId, val: T | null, count?: number): FAMILY | null; - + createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY; get id(): BinaryTreeNodeId set id(v: BinaryTreeNodeId) + get val(): T | undefined - get val(): T - - set val(v: T) - + set val(v: T | undefined) get left(): FAMILY | null | undefined set left(v: FAMILY | null | undefined) - get right(): FAMILY | null | undefined set right(v: FAMILY | null | undefined) - get parent(): FAMILY | null | undefined set parent(v: FAMILY | null | undefined) - get familyPosition(): FamilyPosition set familyPosition(v: FamilyPosition) - get count(): number set count(v: number) - get height(): number set height(v: number) @@ -64,18 +56,29 @@ export interface IAbstractBinaryTree + get visitedNode(): N[] + get visitedCount(): number[] + get visitedLeftSum(): number[] + get autoIncrementId(): boolean + get maxId(): number + get isDuplicatedVal(): boolean + get root(): N | null + get size(): number + get count(): number - + clear(): void isEmpty(): boolean @@ -118,6 +121,8 @@ export interface IAbstractBinaryTree> extends IBinaryTreeNode { - + createNode(id: BinaryTreeNodeId, val?: T, count?: number): FAMILY } export interface IBST> extends IBinaryTree { - createNode(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null + createNode(id: BinaryTreeNodeId, val?: N['val'] | null, count?: number): N add(id: BinaryTreeNodeId, val: N['val'] | null, count: number): N | null diff --git a/src/data-structures/interfaces/rb-tree.ts b/src/data-structures/interfaces/rb-tree.ts index 82e641a..72e2a95 100644 --- a/src/data-structures/interfaces/rb-tree.ts +++ b/src/data-structures/interfaces/rb-tree.ts @@ -1,11 +1,12 @@ import {RBTreeNode} from '../binary-tree'; import {IBST, IBSTNode} from './bst'; +import {BinaryTreeNodeId} from '../types'; export interface IRBTreeNode> extends IBSTNode { - + createNode(id: BinaryTreeNodeId, val?: T | null, count?: number): FAMILY } export interface IRBTree> extends IBST { - + createNode(id: BinaryTreeNodeId, val?: N | null, count?: number): N } \ No newline at end of file diff --git a/src/data-structures/interfaces/undirected-graph.ts b/src/data-structures/interfaces/undirected-graph.ts index fdbcb76..7e7200d 100644 --- a/src/data-structures/interfaces/undirected-graph.ts +++ b/src/data-structures/interfaces/undirected-graph.ts @@ -1,6 +1,6 @@ import {VertexId} from '../types'; import {IAbstractGraph} from './abstract-graph'; -export interface IUNDirectedGraph extends IAbstractGraph{ +export interface IUNDirectedGraph extends IAbstractGraph { removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null; } \ No newline at end of file