[BST, AVL] The 'get' method follows the binary search of a BST for bug fixes during queries. [Project] The editor uses 'tsconfig.json' for code checking to be compatible with the 'test' directory, but compiles using their respective configuration files.

This commit is contained in:
Revone 2023-11-04 10:00:51 +08:00
parent 0ab8054f52
commit 24a961f692
11 changed files with 130 additions and 62 deletions

View file

@ -654,6 +654,40 @@ inherit the existing data structures to implement the customized ones you need.
optimal approach to data structure design.
## Benchmark
<div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 add randomly</td><td>2.12</td><td>470.70</td><td>24</td><td>0.00</td><td>3.55e-5</td></tr><tr><td>1000 add & delete randomly</td><td>5.12</td><td>195.21</td><td>11</td><td>0.01</td><td>8.78e-4</td></tr><tr><td>1000 addMany</td><td>3.95</td><td>253.32</td><td>18</td><td>0.00</td><td>0.00</td></tr><tr><td>1000 get</td><td>3.04</td><td>328.49</td><td>22</td><td>0.00</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>binary-tree</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 add randomly</td><td>13.44</td><td>74.39</td><td>4</td><td>0.01</td><td>0.00</td></tr><tr><td>1000 add & delete randomly</td><td>15.93</td><td>62.78</td><td>4</td><td>0.02</td><td>2.08e-4</td></tr><tr><td>1000 addMany</td><td>10.71</td><td>93.41</td><td>5</td><td>0.01</td><td>2.11e-4</td></tr><tr><td>1000 get</td><td>17.90</td><td>55.86</td><td>3</td><td>0.02</td><td>1.30e-4</td></tr><tr><td>1000 dfs</td><td>68.52</td><td>14.59</td><td>1</td><td>0.07</td><td>3.98e-4</td></tr><tr><td>1000 bfs</td><td>54.57</td><td>18.33</td><td>1</td><td>0.05</td><td>4.34e-4</td></tr><tr><td>1000 morris</td><td>37.37</td><td>26.76</td><td>2</td><td>0.04</td><td>3.60e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>bst</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 add randomly</td><td>2.15</td><td>465.28</td><td>24</td><td>0.00</td><td>1.79e-5</td></tr><tr><td>1000 add & delete randomly</td><td>5.08</td><td>196.92</td><td>10</td><td>0.01</td><td>4.34e-5</td></tr><tr><td>1000 addMany</td><td>2.14</td><td>467.28</td><td>24</td><td>0.00</td><td>4.18e-5</td></tr><tr><td>1000 get</td><td>2.35</td><td>426.21</td><td>22</td><td>0.00</td><td>5.24e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>directed-graph</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 addVertex</td><td>0.10</td><td>9846.76</td><td>505</td><td>1.02e-4</td><td>2.82e-6</td></tr><tr><td>1000 addEdge</td><td>6.23</td><td>160.47</td><td>9</td><td>0.01</td><td>5.72e-4</td></tr><tr><td>1000 getVertex</td><td>0.05</td><td>2.18e+4</td><td>1100</td><td>4.59e-5</td><td>3.84e-7</td></tr><tr><td>1000 getEdge</td><td>23.96</td><td>41.73</td><td>3</td><td>0.02</td><td>0.00</td></tr><tr><td>tarjan</td><td>217.41</td><td>4.60</td><td>1</td><td>0.22</td><td>0.01</td></tr><tr><td>tarjan all</td><td>242.30</td><td>4.13</td><td>1</td><td>0.24</td><td>0.06</td></tr><tr><td>topologicalSort</td><td>184.85</td><td>5.41</td><td>1</td><td>0.18</td><td>0.01</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>heap</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 add & pop</td><td>0.35</td><td>2823.44</td><td>149</td><td>3.54e-4</td><td>5.10e-5</td></tr><tr><td>1000 fib add & pop</td><td>3.92</td><td>255.12</td><td>14</td><td>0.00</td><td>6.68e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>doubly-linked-list</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000000 unshift</td><td>206.78</td><td>4.84</td><td>1</td><td>0.21</td><td>0.03</td></tr><tr><td>1000000 unshift & shift</td><td>170.95</td><td>5.85</td><td>1</td><td>0.17</td><td>0.04</td></tr><tr><td>1000 insertBefore</td><td>0.03</td><td>3.75e+4</td><td>1906</td><td>2.67e-5</td><td>3.24e-7</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>singly-linked-list</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 push & pop</td><td>1.77</td><td>565.87</td><td>30</td><td>0.00</td><td>5.05e-5</td></tr><tr><td>1000 insertBefore</td><td>2.32</td><td>430.56</td><td>23</td><td>0.00</td><td>7.66e-5</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>max-priority-queue</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>10000 refill & poll</td><td>11.45</td><td>87.31</td><td>5</td><td>0.01</td><td>1.45e-4</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000000 push</td><td>214.85</td><td>4.65</td><td>1</td><td>0.21</td><td>0.03</td></tr><tr><td>1000000 shift</td><td>26.08</td><td>38.34</td><td>3</td><td>0.03</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>queue</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000000 push</td><td>44.32</td><td>22.56</td><td>2</td><td>0.04</td><td>0.01</td></tr><tr><td>1000000 push & shift</td><td>79.08</td><td>12.64</td><td>1</td><td>0.08</td><td>0.00</td></tr></table></div>
</div><div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>trie</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>100000 push</td><td>56.20</td><td>17.79</td><td>1</td><td>0.06</td><td>0.01</td></tr><tr><td>100000 getWords</td><td>95.26</td><td>10.50</td><td>1</td><td>0.10</td><td>0.00</td></tr></table></div>
</div>
<div class="json-to-html-collapse clearfix 0">
<div class='collapsible level0' ><span class='json-to-html-label'>avl-tree</span></div>
<div class="content"><table><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>executed times</th><th>sample mean (secs)</th><th>sample deviation</th></tr><tr><td>1000 add randomly</td><td>2.22</td><td>449.77</td><td>23</td><td>0.00</td><td>2.65e-5</td></tr><tr><td>1000 add & delete randomly</td><td>11.58</td><td>86.37</td><td>5</td><td>0.01</td><td>5.00e-4</td></tr><tr><td>1000 addMany</td><td>3.10</td><td>322.62</td><td>17</td><td>0.00</td><td>1.23e-4</td></tr><tr><td>1000 get</td><td>24.91</td><td>40.14</td><td>3</td><td>0.02</td><td>7.25e-4</td></tr></table></div>

View file

@ -2,20 +2,20 @@
"name": "data-structure-typed",
"version": "1.41.5",
"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",
"types": "dist/mjs/index.d.ts",
"main": "dist/cjs/src/index.js",
"module": "dist/mjs/src/index.js",
"types": "dist/mjs/src/index.d.ts",
"umd:main": "dist/umd/data-structure-typed.min.js",
"exports": {
".": {
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/mjs/index.d.ts"
"import": "./dist/mjs/src/index.js",
"require": "./dist/cjs/src/index.js",
"types": "./dist/mjs/src/index.d.ts"
}
},
"scripts": {
"build": "npm run build:mjs && npm run build:cjs && npm run build:umd && npm run build:docs",
"build:mjs": "rm -rf dist/mjs && tsc -p tsconfig.json",
"build:mjs": "rm -rf dist/mjs && tsc -p tsconfig-mjs.json",
"build:cjs": "rm -rf dist/cjs && tsc -p tsconfig-cjs.json",
"build:umd": "tsup",
"build:docs": "typedoc --out docs ./src",

View file

@ -70,12 +70,12 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
* @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
* `((node: N) => node.key)`
* `this.defaultOneParamCallback`
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
*/
override delete<C extends BTNCallback<N>>(
identifier: ReturnType<C>,
callback: C = ((node: N) => node.key) as C
callback: C = this.defaultOneParamCallback as C
): BinaryTreeDeletedResult<N>[] {
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
const deletedResults = super.delete(identifier, callback);

View file

@ -141,6 +141,8 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
return this._size;
}
protected defaultOneParamCallback = (node: N) => node.key;
/**
* Creates a new instance of BinaryTreeNode with the given key and value.
* @param {BTNKey} key - The key for the new node.
@ -284,11 +286,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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
* `((node: N) => node.key)`, which
* `this.defaultOneParamCallback`, which
*/
delete<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C
callback: C = this.defaultOneParamCallback as C
): BinaryTreeDeletedResult<N>[] {
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
if (!this.root) return bstDeletedResult;
@ -506,7 +508,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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
* `((node: N) => node.key)`, which
* `this.defaultOneParamCallback`, 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
@ -520,7 +522,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
*/
getNodes<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
onlyOne = false,
beginRoot: N | null = this.root,
iterationType = this.iterationType
@ -588,7 +590,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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
* `((node: N) => node.key)` is used if no callback function is
* `this.defaultOneParamCallback` 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
@ -599,7 +601,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
*/
has<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot = this.root,
iterationType = this.iterationType
): boolean {
@ -637,7 +639,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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
* (`((node: N) => node.key)`) is used if no callback function is
* (`this.defaultOneParamCallback`) 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
@ -646,7 +648,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
*/
getNode<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot = this.root,
iterationType = this.iterationType
): N | null {
@ -684,7 +686,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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
* (`((node: N) => node.key)`) is used if no callback function is
* (`this.defaultOneParamCallback`) 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
@ -693,7 +695,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
*/
get<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot = this.root,
iterationType = this.iterationType
): V | undefined {
@ -859,7 +861,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The function `subTreeTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
*/
subTreeTraverse<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot: BTNKey | N | null = this.root,
iterationType = this.iterationType
): ReturnType<C>[] {
@ -895,7 +897,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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 `((node: N) => node.key)`, which is a callback function defined elsewhere in the code.
* is `this.defaultOneParamCallback`, 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
@ -906,7 +908,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The function `dfs` returns an array of `ReturnType<BTNCallback<N>>` values.
*/
dfs<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
beginRoot: N | null = this.root,
iterationType: IterationType = IterationType.ITERATIVE
@ -981,7 +983,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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<BTNCallback<N>>`. The default value for this parameter is `((node: N) => node.key)
* `ReturnType<BTNCallback<N>>`. The default value for this parameter is `this.defaultOneParamCallback
* @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.
@ -990,7 +992,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The function `bfs` returns an array of `ReturnType<BTNCallback<N>>[]`.
*/
bfs<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
iterationType = this.iterationType
): ReturnType<C>[] {
@ -1047,7 +1049,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* function `C` applied to the nodes at that level.
*/
listLevels<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot: N | null = this.root,
iterationType = this.iterationType
): ReturnType<C>[][] {
@ -1126,7 +1128,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = 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<BTNCallback<N>>`. The
* default value for this parameter is `((node: N) => node.key)`.
* default value for this parameter is `this.defaultOneParamCallback`.
* @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:
@ -1136,7 +1138,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
* @returns The `morris` function returns an array of `ReturnType<BTNCallback<N>>` values.
*/
morris<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
pattern: DFSOrderPattern = 'in',
beginRoot: N | null = this.root
): ReturnType<C>[] {

View file

@ -36,7 +36,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
}
}
}
/**
* The function creates a new binary search tree node with the given key and value.
* @param {BTNKey} key - The key parameter is the key value that will be associated with
@ -236,7 +236,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
*/
override getNode<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
beginRoot = this.root,
iterationType = this.iterationType
): N | null {
@ -272,7 +272,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
* 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 `((node: N) => node.key)`, which is
* included in the result. The default value for `callback` is `this.defaultOneParamCallback`, 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
@ -287,7 +287,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
*/
override getNodes<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
onlyOne = false,
beginRoot: N | null = this.root,
iterationType = this.iterationType
@ -305,7 +305,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
if (!cur.left && !cur.right) return;
// TODO potential bug
if (callback === ((node: N) => node.key)) {
if (callback === this.defaultOneParamCallback) {
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 {
@ -326,7 +326,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
if (onlyOne) return ans;
}
// TODO potential bug
if (callback === ((node: N) => node.key)) {
if (callback === this.defaultOneParamCallback) {
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 {
@ -360,7 +360,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
* @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<BTNCallback<N>>`.
*/
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
lesserOrGreater: CP = CP.lt,
targetNode: BTNKey | N | null = this.root,
iterationType = this.iterationType

View file

@ -268,7 +268,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = 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
* `((node: N) => node.key)`
* `this.defaultOneParamCallback`
* @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<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
*/
override delete<C extends BTNCallback<N>>(
identifier: ReturnType<C>,
callback: C = ((node: N) => node.key) as C,
callback: C = this.defaultOneParamCallback as C,
ignoreCount = false
): BinaryTreeDeletedResult<N>[] {
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];

View file

@ -1,4 +1,6 @@
import {UndirectedEdge, UndirectedGraph, UndirectedVertex} from '../../../../src';
import saltyVertexes from './SaltyGateStreetIntersections.json';
import saltyEdges from './SaltyGateStreetConnections.json';
describe('UndirectedGraph Operation Test', () => {
let graph: UndirectedGraph;
@ -146,4 +148,18 @@ describe('UndirectedGraph', () => {
expect(degreeOfB).toBe(2);
expect(degreeOfC).toBe(1);
});
it('xxx', () => {
const start = performance.now();
const graph = new UndirectedGraph<{ fromFeatureId: number; name: string; parentName: string; distanceFromParent: number; coordinates: number[]; }, number >()
for (const v of saltyVertexes) {
graph.addVertex(v.name, v);
}
for (const e of saltyEdges) {
const [s,d] = e;
graph.addEdge( s.name, d.name, d.distanceFromParent );
}
const result = graph.getAllPathsBetween('Intersection_1','Intersection_5');
console.log('---xxx', performance.now() - start, result)
})
});

View file

@ -1,27 +1,23 @@
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"declaration": true,
"esModuleInterop": true,
"inlineSourceMap": false,
"lib": ["esnext"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
"rootDir": "src",
"skipLibCheck": true,
"strict": true,
"traceResolution": false,
"types": ["node", "jest"]
"allowJs": true, // Allow the compiler to compile JavaScript files (.js).
"allowSyntheticDefaultImports": true, // Allow synthetic default imports to improve module interoperability.
"baseUrl": "./", // Specify the base path of the root directory for module import.
"declaration": true, // Generate declaration files (.d.ts) for type checking and code hints.
"esModuleInterop": true, // Enable ECMAScript module interop for correct handling of CommonJS modules.
"inlineSourceMap": false, // Whether to map inline sources to compiled JavaScript files.
"lib": ["esnext"], // Specify the standard library version of the compilation target.
"listEmittedFiles": false, // List generated output files.
"listFiles": false, // List source files compiled by the compiler.
"noFallthroughCasesInSwitch": true, // Forced to check whether the case statement in the switch statement has the fallthrough mark.
"pretty": true, // Output a pretty error message.
"resolveJsonModule": true, // Enable parsing of the JSON module.
"rootDir": "./", // Specify the root directory of the source files in the project.
"skipLibCheck": true, // Skip type checking of type libraries.
"strict": true, // Enable strict type checking, including enabling all strict options.
"traceResolution": false, // Whether to trace the module resolution process.
"types": ["node", "jest"] // Specifies the name of the type declaration file to include.
},
"compileOnSave": false,
"exclude": ["node_modules", "dist"],
"include": [
"./src/**/*.ts", // Include your .ts files
]
}
"compileOnSave": false, // Whether to automatically compile when saving.
"exclude": ["node_modules", "dist"], // Specify files or directories that should not be included in the compilation.
}

View file

@ -2,8 +2,12 @@
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"outDir": "dist/cjs",
"target": "ES2015",
"sourceMap": true
}
},
"include": [
"./src/**/*.ts"
]
}

12
tsconfig-mjs.json Normal file
View file

@ -0,0 +1,12 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist/mjs",
"target": "ESNext"
},
"include": [
"./src/**/*.ts"
]
}

View file

@ -5,5 +5,9 @@
"moduleResolution": "NodeNext",
"outDir": "dist/mjs",
"target": "ESNext"
}
},
"include": [
"./src/**/*.ts",
"./test/**/*.ts"
]
}