diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb300d..5c67d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.39.2](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.39.4](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/package-lock.json b/package-lock.json index 608505b..6678db2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "data-structure-typed", - "version": "1.39.2", + "version": "1.39.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "data-structure-typed", - "version": "1.39.2", + "version": "1.39.4", "license": "MIT", "devDependencies": { "@types/benchmark": "^2.1.3", @@ -15,17 +15,17 @@ "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "auto-changelog": "^2.4.0", - "avl-tree-typed": "^1.39.1", + "avl-tree-typed": "^1.39.3", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.39.1", - "bst-typed": "^1.39.1", + "binary-tree-typed": "^1.39.3", + "bst-typed": "^1.39.3", "dependency-cruiser": "^14.1.0", "eslint": "^8.50.0", "eslint-config-prettier": "^9.0.0", "eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.28.1", - "heap-typed": "^1.39.1", + "heap-typed": "^1.39.3", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", @@ -2728,12 +2728,12 @@ } }, "node_modules/avl-tree-typed": { - "version": "1.39.1", - "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.39.1.tgz", - "integrity": "sha512-V4H5cBue8qzsUYzDIngPR6FJELuuMtwYSk34BdpgwPHpo79ZL0LqJmEiIY9FCmkoYPQFvZAz/Z7+RH9MJXgZPA==", + "version": "1.39.3", + "resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.39.3.tgz", + "integrity": "sha512-5tgPsLtuPkgLH6CDuz9OXGR+4AxXXdu6sOtQNrQeqvMkPlgkduxjOd5aMqAvCjkRKHZ3TjFi43sNEYRxzoQ6AQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.39.0" + "data-structure-typed": "^1.39.2" } }, "node_modules/babel-jest": { @@ -2927,12 +2927,12 @@ } }, "node_modules/binary-tree-typed": { - "version": "1.39.1", - "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.39.1.tgz", - "integrity": "sha512-fbPwGQXmy9GKw/23yRm9c+9wDNgXbhvGUP6za0NQuo3kvJ522SAEzvgwPXEqO92RIKT/OpP0+aO5dpTJaOiodQ==", + "version": "1.39.3", + "resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.39.3.tgz", + "integrity": "sha512-bzNlbU5DVx0tK798O0loGfgFqFCTR/lHXptCEvU8YvQQ6E+/rv9zj926HHqQClU0vhtMdBQdQIoxJubGlYnwQQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.39.0" + "data-structure-typed": "^1.39.2" } }, "node_modules/brace-expansion": { @@ -3011,12 +3011,12 @@ } }, "node_modules/bst-typed": { - "version": "1.39.1", - "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.39.1.tgz", - "integrity": "sha512-Oo86lN3GJIODrGHZSpNJZ7X1N1adgEpY8kd7jOvZd+zF2BHEJVpW8uTk3PtSgbCLGnEkSzE2lXWaaOZh5F9x1A==", + "version": "1.39.3", + "resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.39.3.tgz", + "integrity": "sha512-cb0ZMMBfITnStTek9oGxIQBK1ycqZsDzxwmgmNICRNVnrjA7RHa9jck3RIg0MTN/+W4DLTbZJDyqeUZwmWb+xQ==", "dev": true, "dependencies": { - "data-structure-typed": "^1.39.0" + "data-structure-typed": "^1.39.2" } }, "node_modules/buffer-from": { @@ -3413,9 +3413,9 @@ } }, "node_modules/data-structure-typed": { - "version": "1.39.1", - "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.39.1.tgz", - "integrity": "sha512-zAotqpGJoWKG12b4pybA05oih3RAy50RlD++ye8M58AjcM5Aw8lZjWlsNmi+ICiAdq8P0xdJM6Z7/etysctJZg==", + "version": "1.39.2", + "resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.39.2.tgz", + "integrity": "sha512-KGr12Nntd4z8y9vQ+3q7t2vJ6rqjxKP0p/+arH2ZtuPn6HoQ9xZat2yRaHulQKeyiSIzYIpAlj5ZmFgaYRttrg==", "dev": true }, "node_modules/debug": { @@ -4771,12 +4771,12 @@ } }, "node_modules/heap-typed": { - "version": "1.39.1", - "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.39.1.tgz", - "integrity": "sha512-+fKr3EKYNGLK9ZVLeZZFP58eKvGv/zWGYbBOZYIXkQZeBXtdL0b4rHL0LsoIpuyPMbhXDFNOhaasG4D1NbCu2Q==", + "version": "1.39.3", + "resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.39.3.tgz", + "integrity": "sha512-wq1d+5KwpCoY19Mls6e+ughQljwDNo3Z2TcPdmw0AYdXbEhajgaemggHbujxKpOB/r0iNaX4rdoGAm4HVJ3R3Q==", "dev": true, "dependencies": { - "data-structure-typed": "^1.39.0" + "data-structure-typed": "^1.39.2" } }, "node_modules/html-escaper": { diff --git a/package.json b/package.json index 33a2080..f14225c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "data-structure-typed", - "version": "1.39.3", + "version": "1.39.4", "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/cjs/index.js", "module": "dist/mjs/index.js", @@ -61,17 +61,17 @@ "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", "auto-changelog": "^2.4.0", - "avl-tree-typed": "^1.39.1", + "avl-tree-typed": "^1.39.3", "benchmark": "^2.1.4", - "binary-tree-typed": "^1.39.1", - "bst-typed": "^1.39.1", + "binary-tree-typed": "^1.39.3", + "bst-typed": "^1.39.3", "dependency-cruiser": "^14.1.0", "eslint": "^8.50.0", "eslint-config-prettier": "^9.0.0", "eslint-import-resolver-alias": "^1.1.2", "eslint-import-resolver-typescript": "^3.6.1", "eslint-plugin-import": "^2.28.1", - "heap-typed": "^1.39.1", + "heap-typed": "^1.39.3", "istanbul-badges-readme": "^1.8.5", "jest": "^29.7.0", "prettier": "^3.0.3", diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index db9dd2a..4fc9324 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -71,13 +71,14 @@ export class AVLTree = AVLTreeNode node.key)` * @returns The method is returning an array of `BinaryTreeDeletedResult` objects. */ override delete>( identifier: ReturnType, - callback: C = this._defaultCallbackByKey as C + callback: C = ((node: N) => node.key) as C ): BinaryTreeDeletedResult[] { + if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C; const deletedResults = super.delete(identifier, callback); for (const {needBalanced} of deletedResults) { if (needBalanced) { diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 072e730..da5c9b3 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -217,7 +217,7 @@ export class BinaryTree = BinaryTreeNode } const key = typeof keyOrNode === 'number' ? keyOrNode : keyOrNode ? keyOrNode.key : undefined; - const existNode = key !== undefined ? this.get(key, this._defaultCallbackByKey) : undefined; + const existNode = key !== undefined ? this.get(key, (node: N) => node.key) : undefined; if (this.root) { if (existNode) { @@ -296,11 +296,11 @@ export class BinaryTree = BinaryTreeNode * @param callback - The `callback` parameter is a function that takes a node as input and returns a * value. This value is compared with the `identifier` parameter to determine if the node should be * included in the result. The `callback` parameter has a default value of - * `this._defaultCallbackByKey`, which + * `((node: N) => node.key)`, which */ delete>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C + callback: C = ((node: N) => node.key) as C ): BinaryTreeDeletedResult[] { const bstDeletedResult: BinaryTreeDeletedResult[] = []; if (!this.root) return bstDeletedResult; @@ -487,7 +487,7 @@ export class BinaryTree = BinaryTreeNode getNodes>( identifier: BTNKey, - callback: C, + callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType @@ -495,7 +495,7 @@ export class BinaryTree = BinaryTreeNode getNodes>( identifier: N | null, - callback: C, + callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType @@ -518,7 +518,7 @@ export class BinaryTree = BinaryTreeNode * @param callback - The `callback` parameter is a function that takes a node as input and returns a * value. This value is compared with the `identifier` parameter to determine if the node should be * included in the result. The `callback` parameter has a default value of - * `this._defaultCallbackByKey`, which + * `((node: N) => node.key)`, which * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the * first node that matches the identifier. If set to true, the function will return an array with * only one element (or an empty array if no matching node is found). If set to false (default), the @@ -532,7 +532,7 @@ export class BinaryTree = BinaryTreeNode */ getNodes>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, onlyOne = false, beginRoot: N | null = this.root, iterationType = this.iterationType @@ -600,7 +600,7 @@ export class BinaryTree = BinaryTreeNode * @param callback - The `callback` parameter is a function that is used to determine whether a node * matches the desired criteria. It takes a node as input and returns a boolean value indicating * whether the node matches the criteria or not. The default callback function - * `this._defaultCallbackByKey` is used if no callback function is + * `((node: N) => node.key)` is used if no callback function is * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies * the node from which the search should begin. By default, it is set to `this.root`, which means the * search will start from the root node of the binary tree. However, you can provide a different node @@ -611,7 +611,7 @@ export class BinaryTree = BinaryTreeNode */ has>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot = this.root, iterationType = this.iterationType ): boolean { @@ -649,7 +649,7 @@ export class BinaryTree = BinaryTreeNode * @param callback - The `callback` parameter is a function that is used to determine whether a node * matches the desired criteria. It takes a node as input and returns a boolean value indicating * whether the node matches the criteria or not. The default callback function - * (`this._defaultCallbackByKey`) is used if no callback function is + * (`((node: N) => node.key)`) is used if no callback function is * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies * the root node from which the search should begin. * @param iterationType - The `iterationType` parameter specifies the type of iteration to be @@ -658,7 +658,7 @@ export class BinaryTree = BinaryTreeNode */ get>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot = this.root, iterationType = this.iterationType ): N | null { @@ -824,7 +824,7 @@ export class BinaryTree = BinaryTreeNode * @returns The function `subTreeTraverse` returns an array of `ReturnType>`. */ subTreeTraverse>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot: BTNKey | N | null = this.root, iterationType = this.iterationType ): ReturnType[] { @@ -860,7 +860,7 @@ export class BinaryTree = BinaryTreeNode * function on each node according to a specified order pattern. * @param callback - The `callback` parameter is a function that will be called on each node during * the depth-first search traversal. It takes a node as input and returns a value. The default value - * is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code. + * is `((node: N) => node.key)`, which is a callback function defined elsewhere in the code. * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the * nodes are visited during the depth-first search. There are three possible values for `pattern`: * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first @@ -871,7 +871,7 @@ export class BinaryTree = BinaryTreeNode * @returns The function `dfs` returns an array of `ReturnType>` values. */ dfs>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, pattern: DFSOrderPattern = 'in', beginRoot: N | null = this.root, iterationType: IterationType = IterationType.ITERATIVE @@ -946,7 +946,7 @@ export class BinaryTree = BinaryTreeNode * function on each node. * @param callback - The `callback` parameter is a function that will be called for each node in the * breadth-first search. It takes a node of type `N` as its argument and returns a value of type - * `ReturnType>`. The default value for this parameter is `this._defaultCallbackByKey + * `ReturnType>`. The default value for this parameter is `((node: N) => node.key) * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search * will not be performed and an empty array will be returned. @@ -955,7 +955,7 @@ export class BinaryTree = BinaryTreeNode * @returns The function `bfs` returns an array of `ReturnType>[]`. */ bfs>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot: N | null = this.root, iterationType = this.iterationType ): ReturnType[] { @@ -1012,7 +1012,7 @@ export class BinaryTree = BinaryTreeNode * function `C` applied to the nodes at that level. */ listLevels>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot: N | null = this.root, iterationType = this.iterationType ): ReturnType[][] { @@ -1071,7 +1071,7 @@ export class BinaryTree = BinaryTreeNode * algorithm and returns an array of values obtained by applying a callback function to each node. * @param callback - The `callback` parameter is a function that will be called on each node in the * tree. It takes a node of type `N` as input and returns a value of type `ReturnType>`. The - * default value for this parameter is `this._defaultCallbackByKey`. + * default value for this parameter is `((node: N) => node.key)`. * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function * determines the order in which the nodes of a binary tree are traversed. It can have one of the * following values: @@ -1081,7 +1081,7 @@ export class BinaryTree = BinaryTreeNode * @returns The `morris` function returns an array of `ReturnType>` values. */ morris>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, pattern: DFSOrderPattern = 'in', beginRoot: N | null = this.root ): ReturnType[] { @@ -1226,15 +1226,6 @@ export class BinaryTree = BinaryTreeNode return destNode; } - /** - * Time complexity is O(n) - * Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack - * The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete, - * the tree's structure should be restored to its original state to maintain the tree's integrity. - * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape. - */ - protected _defaultCallbackByKey: BTNCallback = node => node.key; - /** * The function `_addTo` adds a new node to a binary tree if there is an available position. * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 84a6944..4868953 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -234,7 +234,7 @@ export class BST = BSTNode> */ override get>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, beginRoot = this.root, iterationType = this.iterationType ): N | null { @@ -270,7 +270,7 @@ export class BST = BSTNode> * generic type `N`. * @param callback - The `callback` parameter is a function that takes a node as input and returns a * value. This value is compared with the `nodeProperty` parameter to determine if the node should be - * included in the result. The default value for `callback` is `this._defaultCallbackByKey`, which is + * included in the result. The default value for `callback` is `((node: N) => node.key)`, which is * a * @param [onlyOne=false] - A boolean value indicating whether to stop the traversal after finding * the first node that matches the nodeProperty. If set to true, the function will return an array @@ -285,7 +285,7 @@ export class BST = BSTNode> */ override getNodes>( identifier: ReturnType | null, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, onlyOne = false, beginRoot: N | null = this.root, iterationType = this.iterationType @@ -303,7 +303,7 @@ export class BST = BSTNode> if (!cur.left && !cur.right) return; // TODO potential bug - if (callback === this._defaultCallbackByKey) { + if (callback === ((node: N) => node.key)) { if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left); if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right); } else { @@ -324,7 +324,7 @@ export class BST = BSTNode> if (onlyOne) return ans; } // TODO potential bug - if (callback === this._defaultCallbackByKey) { + if (callback === ((node: N) => node.key)) { if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left); if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right); } else { @@ -358,7 +358,7 @@ export class BST = BSTNode> * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType>`. */ lesserOrGreaterTraverse>( - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, lesserOrGreater: CP = CP.lt, targetNode: BTNKey | N | null = this.root, iterationType = this.iterationType diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 15b9a81..e196af7 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -268,7 +268,7 @@ export class TreeMultiset = TreeMultis * @param callback - The `callback` parameter is a function that takes a node as input and returns a * value. This value is compared with the `identifier` parameter to determine if the node should be * included in the result. The `callback` parameter has a default value of - * `this._defaultCallbackByKey` + * `((node: N) => node.key)` * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node * being deleted. If set to true, the count of the node will not be considered and the node will be * deleted regardless of its count. If set to false (default), the count of the node will be @@ -277,7 +277,7 @@ export class TreeMultiset = TreeMultis */ override delete>( identifier: ReturnType, - callback: C = this._defaultCallbackByKey as C, + callback: C = ((node: N) => node.key) as C, ignoreCount = false ): BinaryTreeDeletedResult[] { const bstDeletedResult: BinaryTreeDeletedResult[] = []; diff --git a/test/unit/data-structures/binary-tree/binary-tree.test.ts b/test/unit/data-structures/binary-tree/binary-tree.test.ts index 3759552..2bd6647 100644 --- a/test/unit/data-structures/binary-tree/binary-tree.test.ts +++ b/test/unit/data-structures/binary-tree/binary-tree.test.ts @@ -480,7 +480,6 @@ describe('BinaryTree', () => { tree.delete(5); tree.delete(7); tree.delete(3); - expect(tree.root).toBe(null); expect(tree.getHeight()).toBe(-1); });