mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-09 23:54:04 +00:00
[core] rename id to key
This commit is contained in:
parent
45c49c99bf
commit
3bffa7320c
|
@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|||
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
||||
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
||||
|
||||
## [v1.34.7](https://github.com/zrwusa/data-structure-typed/compare/v1.34.1...main) (upcoming)
|
||||
## [v1.34.8](https://github.com/zrwusa/data-structure-typed/compare/v1.34.1...main) (upcoming)
|
||||
|
||||
## [v1.34.1](https://github.com/zrwusa/data-structure-typed/compare/v1.33.4...v1.34.1) (6 October 2023)
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.34.7",
|
||||
"version": "1.34.8",
|
||||
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
||||
"main": "dist/index.js",
|
||||
"module": "lib/index.js",
|
||||
|
|
|
@ -239,14 +239,14 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
|
||||
/**
|
||||
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
||||
* @param {BinaryTreeNodeKey | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
||||
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
||||
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
||||
* node itself. It can also be `null` if no node is specified.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
||||
* being added to the binary tree.
|
||||
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
||||
*/
|
||||
add(idOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
||||
const queue: Array<N | null> = [root];
|
||||
while (queue.length > 0) {
|
||||
|
@ -264,17 +264,17 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
|
||||
let inserted: N | null | undefined, needInsert: N | null;
|
||||
|
||||
if (idOrNode === null) {
|
||||
if (keyOrNode === null) {
|
||||
needInsert = null;
|
||||
} else if (typeof idOrNode === 'number') {
|
||||
needInsert = this.createNode(idOrNode, val);
|
||||
} else if (idOrNode instanceof AbstractBinaryTreeNode) {
|
||||
needInsert = idOrNode;
|
||||
} else if (typeof keyOrNode === 'number') {
|
||||
needInsert = this.createNode(keyOrNode, val);
|
||||
} else if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
||||
needInsert = keyOrNode;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
const existNode = idOrNode ? this.get(idOrNode, 'key') : undefined;
|
||||
const existNode = keyOrNode ? this.get(keyOrNode, 'key') : undefined;
|
||||
|
||||
if (this.root) {
|
||||
if (existNode) {
|
||||
|
@ -310,19 +310,19 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
const inserted: (N | null | undefined)[] = [];
|
||||
|
||||
for (let i = 0; i < idsOrNodes.length; i++) {
|
||||
const idOrNode = idsOrNodes[i];
|
||||
if (idOrNode instanceof AbstractBinaryTreeNode) {
|
||||
inserted.push(this.add(idOrNode.key, idOrNode.val));
|
||||
const keyOrNode = idsOrNodes[i];
|
||||
if (keyOrNode instanceof AbstractBinaryTreeNode) {
|
||||
inserted.push(this.add(keyOrNode.key, keyOrNode.val));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (idOrNode === null) {
|
||||
if (keyOrNode === null) {
|
||||
inserted.push(this.add(null));
|
||||
continue;
|
||||
}
|
||||
|
||||
const val = data?.[i];
|
||||
inserted.push(this.add(idOrNode, val));
|
||||
inserted.push(this.add(keyOrNode, val));
|
||||
}
|
||||
return inserted;
|
||||
}
|
||||
|
|
|
@ -55,21 +55,21 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
/**
|
||||
* The `add` function adds a new node to a binary search tree, either by creating a new node or by updating an existing
|
||||
* node with the same ID.
|
||||
* @param {BinaryTreeNodeKey | N | null} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
||||
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N`
|
||||
* (which represents a binary tree node) or `null`.
|
||||
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
||||
* being added to the binary search tree.
|
||||
* @returns The function `add` returns the inserted node (`inserted`) which can be of type `N`, `null`, or `undefined`.
|
||||
*/
|
||||
override add(idOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
// TODO support node as a param
|
||||
let inserted: N | null = null;
|
||||
let newNode: N | null = null;
|
||||
if (idOrNode instanceof BSTNode) {
|
||||
newNode = idOrNode;
|
||||
} else if (typeof idOrNode === 'number') {
|
||||
newNode = this.createNode(idOrNode, val);
|
||||
} else if (idOrNode === null) {
|
||||
if (keyOrNode instanceof BSTNode) {
|
||||
newNode = keyOrNode;
|
||||
} else if (typeof keyOrNode === 'number') {
|
||||
newNode = this.createNode(keyOrNode, val);
|
||||
} else if (keyOrNode === null) {
|
||||
newNode = null;
|
||||
}
|
||||
if (this.root === null) {
|
||||
|
@ -152,13 +152,13 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
const combinedArr: [BinaryTreeNodeKey | N, N['val']][] = idsOrNodes.map((value, index) => [value, data?.[index]]);
|
||||
let sorted = [];
|
||||
function isNodeOrNullTuple(arr: [BinaryTreeNodeKey | N, N['val']][]): arr is [N, N['val']][] {
|
||||
for (const [idOrNode] of arr) if (idOrNode instanceof BSTNode) return true;
|
||||
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
||||
return false;
|
||||
}
|
||||
function isBinaryTreeKeyOrNullTuple(
|
||||
arr: [BinaryTreeNodeKey | N, N['val']][]
|
||||
): arr is [BinaryTreeNodeKey, N['val']][] {
|
||||
for (const [idOrNode] of arr) if (typeof idOrNode === 'number') return true;
|
||||
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
||||
return false;
|
||||
}
|
||||
let sortedKeysOrNodes: (number | N | null)[] = [],
|
||||
|
@ -171,7 +171,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
|
|||
} else {
|
||||
throw new Error('Invalid input idsOrNodes');
|
||||
}
|
||||
sortedKeysOrNodes = sorted.map(([idOrNode]) => idOrNode);
|
||||
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
||||
sortedData = sorted.map(([, val]) => val);
|
||||
const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => {
|
||||
if (arr.length === 0) return;
|
||||
|
|
|
@ -31,8 +31,8 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
|
|||
return new RBTreeNode(key, val) as N;
|
||||
}
|
||||
|
||||
// override add(idOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
// const inserted = super.add(idOrNode, val);
|
||||
// override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
||||
// const inserted = super.add(keyOrNode, val);
|
||||
// if (inserted) this._fixInsertViolation(inserted);
|
||||
// return inserted;
|
||||
// }
|
||||
|
|
|
@ -106,23 +106,23 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|||
/**
|
||||
* The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
|
||||
* necessary.
|
||||
* @param {BinaryTreeNodeKey | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
||||
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
|
||||
* represents a `BinaryTreeNode`).
|
||||
* @param [val] - The `val` parameter represents the value to be added to the binary tree node.
|
||||
* @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
|
||||
* value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
|
||||
* @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
||||
*/
|
||||
override add(idOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined {
|
||||
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined {
|
||||
count = count ?? 1;
|
||||
let inserted: N | null | undefined = undefined,
|
||||
newNode: N | null;
|
||||
if (idOrNode instanceof TreeMultisetNode) {
|
||||
newNode = this.createNode(idOrNode.key, idOrNode.val, idOrNode.count);
|
||||
} else if (idOrNode === null) {
|
||||
if (keyOrNode instanceof TreeMultisetNode) {
|
||||
newNode = this.createNode(keyOrNode.key, keyOrNode.val, keyOrNode.count);
|
||||
} else if (keyOrNode === null) {
|
||||
newNode = null;
|
||||
} else {
|
||||
newNode = this.createNode(idOrNode, val, count);
|
||||
newNode = this.createNode(keyOrNode, val, count);
|
||||
}
|
||||
if (!this.root) {
|
||||
this._setRoot(newNode);
|
||||
|
@ -222,7 +222,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|||
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
||||
* objects, or null values.
|
||||
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
||||
* the nodes being added. It is used when adding nodes using the `idOrNode` and `data` arguments in the `this.add()`
|
||||
* the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
|
||||
* method. If provided, the `data` array should
|
||||
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
||||
*/
|
||||
|
@ -233,19 +233,19 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|||
const inserted: (N | null | undefined)[] = [];
|
||||
|
||||
for (let i = 0; i < idsOrNodes.length; i++) {
|
||||
const idOrNode = idsOrNodes[i];
|
||||
const keyOrNode = idsOrNodes[i];
|
||||
|
||||
if (idOrNode instanceof TreeMultisetNode) {
|
||||
inserted.push(this.add(idOrNode.key, idOrNode.val, idOrNode.count));
|
||||
if (keyOrNode instanceof TreeMultisetNode) {
|
||||
inserted.push(this.add(keyOrNode.key, keyOrNode.val, keyOrNode.count));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (idOrNode === null) {
|
||||
if (keyOrNode === null) {
|
||||
inserted.push(this.add(NaN, null, 0));
|
||||
continue;
|
||||
}
|
||||
|
||||
inserted.push(this.add(idOrNode, data?.[i], 1));
|
||||
inserted.push(this.add(keyOrNode, data?.[i], 1));
|
||||
}
|
||||
return inserted;
|
||||
}
|
||||
|
|
|
@ -169,11 +169,11 @@ export abstract class AbstractGraph<
|
|||
|
||||
addVertex(key: VertexKey, val?: V['val']): boolean;
|
||||
|
||||
addVertex(idOrVertex: VertexKey | V, val?: V['val']): boolean {
|
||||
if (idOrVertex instanceof AbstractVertex) {
|
||||
return this._addVertexOnly(idOrVertex);
|
||||
addVertex(keyOrVertex: VertexKey | V, val?: V['val']): boolean {
|
||||
if (keyOrVertex instanceof AbstractVertex) {
|
||||
return this._addVertexOnly(keyOrVertex);
|
||||
} else {
|
||||
const newVertex = this.createVertex(idOrVertex, val);
|
||||
const newVertex = this.createVertex(keyOrVertex, val);
|
||||
return this._addVertexOnly(newVertex);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue