data-structure-typed 1.36.7 → 1.36.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -1
- package/CHANGELOG.md +3 -1
- package/README.md +8 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/avl-tree.js +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/dist/data-structures/binary-tree/binary-tree.js +76 -128
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/dist/data-structures/binary-tree/tree-multiset.js +10 -20
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +3 -3
- package/dist/data-structures/hash/hash-table.js +3 -3
- package/dist/data-structures/heap/heap.d.ts +136 -11
- package/dist/data-structures/heap/heap.js +293 -13
- package/dist/data-structures/heap/heap.js.map +1 -1
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -3
- package/dist/data-structures/queue/deque.d.ts +2 -2
- package/dist/data-structures/queue/deque.js +2 -2
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -5
- package/lib/data-structures/binary-tree/avl-tree.js +6 -6
- package/lib/data-structures/binary-tree/binary-tree.d.ts +18 -85
- package/lib/data-structures/binary-tree/binary-tree.js +76 -128
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +9 -16
- package/lib/data-structures/binary-tree/tree-multiset.js +10 -20
- package/lib/data-structures/hash/hash-map.d.ts +1 -1
- package/lib/data-structures/hash/hash-map.js +1 -1
- package/lib/data-structures/hash/hash-table.d.ts +3 -3
- package/lib/data-structures/hash/hash-table.js +3 -3
- package/lib/data-structures/heap/heap.d.ts +136 -11
- package/lib/data-structures/heap/heap.js +290 -12
- package/lib/data-structures/linked-list/skip-linked-list.d.ts +3 -3
- package/lib/data-structures/linked-list/skip-linked-list.js +3 -3
- package/lib/data-structures/queue/deque.d.ts +2 -2
- package/lib/data-structures/queue/deque.js +2 -2
- package/lib/data-structures/queue/queue.js +1 -1
- package/lib/data-structures/trie/trie.d.ts +2 -2
- package/lib/data-structures/trie/trie.js +2 -2
- package/lib/interfaces/binary-tree.d.ts +1 -1
- package/package.json +7 -6
- package/src/data-structures/binary-tree/avl-tree.ts +6 -6
- package/src/data-structures/binary-tree/binary-tree.ts +79 -214
- package/src/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +10 -21
- package/src/data-structures/hash/hash-map.ts +1 -1
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +340 -16
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -3
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/test/types/index.ts +1 -0
- package/test/types/utils/big-o.ts +1 -0
- package/test/types/utils/index.ts +1 -0
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +14 -14
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/bst.test.ts +28 -28
- package/test/unit/data-structures/binary-tree/overall.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +28 -28
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +1 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +2 -2
- package/test/unit/data-structures/hash/hash-table.test.ts +5 -5
- package/test/unit/data-structures/heap/heap.test.ts +192 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +7 -7
- package/test/unit/data-structures/linked-list/skip-list.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +3 -3
- package/test/unit/data-structures/trie/trie.test.ts +5 -5
- package/test/utils/big-o.ts +199 -0
- package/test/utils/index.ts +1 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/test/utils/magnitude.ts +0 -21
package/.eslintrc.js
CHANGED
|
@@ -11,7 +11,7 @@ module.exports = {
|
|
|
11
11
|
"ignorePatterns": ["lib/", "dist/", "umd/", "coverage/", "docs/"],
|
|
12
12
|
"rules": {
|
|
13
13
|
"import/no-anonymous-default-export": "off",
|
|
14
|
-
"@typescript-eslint/no-unused-vars": "
|
|
14
|
+
"@typescript-eslint/no-unused-vars": "warn",
|
|
15
15
|
"@typescript-eslint/ban-ts-comment": "off",
|
|
16
16
|
"@typescript-eslint/no-explicit-any": "off",
|
|
17
17
|
"@typescript-eslint/no-var-requires": "off",
|
package/CHANGELOG.md
CHANGED
|
@@ -8,10 +8,12 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v1.36.
|
|
11
|
+
## [v1.36.9](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
### Changes
|
|
14
14
|
|
|
15
|
+
- 1. No need for dfsIterative; integrate it directly into the dfs metho… [`#17`](https://github.com/zrwusa/data-structure-typed/pull/17)
|
|
16
|
+
- [heap] fibonacci heap implemented. [test] big O estimate. [project] n… [`#15`](https://github.com/zrwusa/data-structure-typed/pull/15)
|
|
15
17
|
- [rbtree] implemented, but with bugs [`#13`](https://github.com/zrwusa/data-structure-typed/pull/13)
|
|
16
18
|
- [trie] renamed ambiguous methods and add comments to all methods. [`#12`](https://github.com/zrwusa/data-structure-typed/pull/12)
|
|
17
19
|
- [binarytree] modified the getDepth method to adhere to the proper def… [`#11`](https://github.com/zrwusa/data-structure-typed/pull/11)
|
package/README.md
CHANGED
|
@@ -641,6 +641,14 @@ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D
|
|
|
641
641
|
|
|
642
642
|
## Code design
|
|
643
643
|
|
|
644
|
+
### Adhere to ES6 standard naming conventions for APIs.
|
|
645
|
+
|
|
646
|
+
Standardize API conventions by using 'add' and 'delete' for element manipulation methods in all data structures.
|
|
647
|
+
|
|
648
|
+
Opt for concise and clear method names, avoiding excessive length while ensuring explicit intent.
|
|
649
|
+
|
|
650
|
+
### Object-oriented programming(OOP)
|
|
651
|
+
|
|
644
652
|
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly
|
|
645
653
|
inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the
|
|
646
654
|
optimal approach to data structure design.
|
|
@@ -21,13 +21,13 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
21
21
|
*/
|
|
22
22
|
constructor(options?: AVLTreeOptions);
|
|
23
23
|
/**
|
|
24
|
-
* The `
|
|
25
|
-
* @param {N} srcNode - The source node that you want to
|
|
24
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
25
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
26
26
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
27
27
|
* be swapped to.
|
|
28
28
|
* @returns The `destNode` is being returned.
|
|
29
29
|
*/
|
|
30
|
-
|
|
30
|
+
protected _swap(srcNode: N, destNode: N): N;
|
|
31
31
|
/**
|
|
32
32
|
* The function creates a new AVL tree node with the given key and value.
|
|
33
33
|
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
@@ -46,13 +46,13 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
46
46
|
*/
|
|
47
47
|
add(key: BinaryTreeNodeKey, val?: N['val']): N | null | undefined;
|
|
48
48
|
/**
|
|
49
|
-
* The function overrides the
|
|
49
|
+
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
|
|
50
50
|
* deletion.
|
|
51
51
|
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
52
52
|
* removed.
|
|
53
53
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
delete(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
56
56
|
/**
|
|
57
57
|
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
58
58
|
* height of its right subtree.
|
|
@@ -27,13 +27,13 @@ class AVLTree extends bst_1.BST {
|
|
|
27
27
|
super(options);
|
|
28
28
|
}
|
|
29
29
|
/**
|
|
30
|
-
* The `
|
|
31
|
-
* @param {N} srcNode - The source node that you want to
|
|
30
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
31
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
32
32
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
33
33
|
* be swapped to.
|
|
34
34
|
* @returns The `destNode` is being returned.
|
|
35
35
|
*/
|
|
36
|
-
|
|
36
|
+
_swap(srcNode, destNode) {
|
|
37
37
|
const { key, val, height } = destNode;
|
|
38
38
|
const tempNode = this.createNode(key, val);
|
|
39
39
|
if (tempNode) {
|
|
@@ -73,14 +73,14 @@ class AVLTree extends bst_1.BST {
|
|
|
73
73
|
return inserted;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
|
-
* The function overrides the
|
|
76
|
+
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
|
|
77
77
|
* deletion.
|
|
78
78
|
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
79
79
|
* removed.
|
|
80
80
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
81
81
|
*/
|
|
82
|
-
|
|
83
|
-
const deletedResults = super.
|
|
82
|
+
delete(key) {
|
|
83
|
+
const deletedResults = super.delete(key);
|
|
84
84
|
for (const { needBalanced } of deletedResults) {
|
|
85
85
|
if (needBalanced) {
|
|
86
86
|
this._balancePath(needBalanced);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"avl-tree.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/avl-tree.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,+BAAmC;AAInC,MAAa,WAAmF,SAAQ,aAGvG;IAGC,YAAY,GAAsB,EAAE,GAAO;QACzC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AAVD,kCAUC;AAED,MAAa,OAA0D,SAAQ,SAAM;IACnF;;;;;OAKG;IACH,YAAY,OAAwB;QAClC,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;IAED;;;;;;OAMG;
|
|
1
|
+
{"version":3,"file":"avl-tree.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/avl-tree.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,+BAAmC;AAInC,MAAa,WAAmF,SAAQ,aAGvG;IAGC,YAAY,GAAsB,EAAE,GAAO;QACzC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,C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I,EAAE;YACnB,IAAI,CAAC;gBAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;SACzB;aAAM;YACL,IAAI,SAAS,EAAE;gBACb,IAAI,SAAS,CAAC,IAAI,KAAK,CAAC,EAAE;oBACxB,SAAS,CAAC,IAAI,GAAG,CAAC,CAAC;iBACpB;qBAAM;oBACL,SAAS,CAAC,KAAK,GAAG,CAAC,CAAC;iBACrB;aACF;SACF;QAED,IAAI,CAAC;YAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC;YAAE,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC;QAC7B,IAAI,CAAC;YAAE,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC;YAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;QAEnB,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QACtB,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC,IAAI,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAC7B,CAAC;CACF;AAxTD,0BAwTC"}
|
|
@@ -55,17 +55,18 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
55
55
|
get size(): number;
|
|
56
56
|
private _loopType;
|
|
57
57
|
get loopType(): LoopType;
|
|
58
|
+
set loopType(v: LoopType);
|
|
58
59
|
visitedKey: BinaryTreeNodeKey[];
|
|
59
60
|
visitedVal: N['val'][];
|
|
60
61
|
visitedNode: N[];
|
|
61
62
|
/**
|
|
62
|
-
* The `
|
|
63
|
-
* @param {N} srcNode - The source node that you want to
|
|
63
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
64
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
64
65
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
65
66
|
* be swapped to.
|
|
66
67
|
* @returns The `destNode` is being returned.
|
|
67
68
|
*/
|
|
68
|
-
|
|
69
|
+
protected _swap(srcNode: N, destNode: N): N;
|
|
69
70
|
/**
|
|
70
71
|
* The clear() function resets the root, size, and maxKey properties to their initial values.
|
|
71
72
|
*/
|
|
@@ -111,13 +112,13 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
111
112
|
*/
|
|
112
113
|
refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
|
|
113
114
|
/**
|
|
114
|
-
* The `
|
|
115
|
+
* The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
115
116
|
* containing the deleted node and the node that needs to be balanced.
|
|
116
117
|
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
117
118
|
* node ID (`BinaryTreeNodeKey`).
|
|
118
|
-
* @returns The function `
|
|
119
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
119
120
|
*/
|
|
120
|
-
|
|
121
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
121
122
|
/**
|
|
122
123
|
* The function calculates the depth of a node in a binary tree.
|
|
123
124
|
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
|
|
@@ -236,10 +237,10 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
236
237
|
getRightMost(beginRoot: N): N;
|
|
237
238
|
/**
|
|
238
239
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
239
|
-
* @param {N | null}
|
|
240
|
+
* @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
240
241
|
* @returns a boolean value.
|
|
241
242
|
*/
|
|
242
|
-
isSubtreeBST(
|
|
243
|
+
isSubtreeBST(subTreeRoot: N | null): boolean;
|
|
243
244
|
/**
|
|
244
245
|
* The function isBST checks if the binary tree is valid binary search tree.
|
|
245
246
|
* @returns The `isBST()` function is returning a boolean value.
|
|
@@ -263,16 +264,14 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
263
264
|
*/
|
|
264
265
|
subTreeSum(subTreeRoot: N | BinaryTreeNodeKey | null, propertyName?: BinaryTreeNodePropertyName): number;
|
|
265
266
|
/**
|
|
266
|
-
* The function `
|
|
267
|
+
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
267
268
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
268
269
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
269
|
-
* @param
|
|
270
|
-
* each node in the subtree should be incremented.
|
|
271
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
270
|
+
* @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
|
|
272
271
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
273
272
|
* @returns a boolean value.
|
|
274
273
|
*/
|
|
275
|
-
|
|
274
|
+
subTreeForeach(subTreeRoot: N | BinaryTreeNodeKey | null, callback: (node: N) => any): boolean;
|
|
276
275
|
/**
|
|
277
276
|
* Performs a breadth-first search (bfs) on a binary tree, accumulating properties of each node based on their 'key' property.
|
|
278
277
|
* @returns An array of binary tree node IDs.
|
|
@@ -311,87 +310,26 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
311
310
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
312
311
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
313
312
|
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
313
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
314
314
|
* @returns An array of values corresponding to the specified property.
|
|
315
315
|
*/
|
|
316
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
316
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'key', loopType?: LoopType): BinaryTreeNodeKey[];
|
|
317
317
|
/**
|
|
318
318
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
319
319
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
320
320
|
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
321
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
321
322
|
* @returns An array of 'val' properties from each node.
|
|
322
323
|
*/
|
|
323
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N[];
|
|
324
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'val', loopType?: LoopType): N[];
|
|
324
325
|
/**
|
|
325
326
|
* Performs a depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
326
327
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
327
328
|
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
329
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
328
330
|
* @returns An array of binary tree nodes.
|
|
329
331
|
*/
|
|
330
|
-
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
331
|
-
/**
|
|
332
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
333
|
-
* @returns An array of binary tree node IDs.
|
|
334
|
-
*/
|
|
335
|
-
dfsIterative(): BinaryTreeNodeKey[];
|
|
336
|
-
/**
|
|
337
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on their 'key' property.
|
|
338
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
339
|
-
* @returns An array of values corresponding to the specified property.
|
|
340
|
-
*/
|
|
341
|
-
dfsIterative(pattern: DFSOrderPattern): BinaryTreeNodeKey[];
|
|
342
|
-
/**
|
|
343
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates properties of each node based on the specified property name.
|
|
344
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
345
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
346
|
-
* @returns An array of values corresponding to the specified property.
|
|
347
|
-
*/
|
|
348
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
349
|
-
/**
|
|
350
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates the 'val' property of each node.
|
|
351
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
352
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
353
|
-
* @returns An array of 'val' properties from each node.
|
|
354
|
-
*/
|
|
355
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'val'): N['val'][];
|
|
356
|
-
/**
|
|
357
|
-
* Performs an iterative depth-first search (dfs) traversal on a binary tree and accumulates nodes themselves.
|
|
358
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
359
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
360
|
-
* @returns An array of binary tree nodes.
|
|
361
|
-
*/
|
|
362
|
-
dfsIterative(pattern: DFSOrderPattern, nodeOrPropertyName: 'node'): N[];
|
|
363
|
-
/**
|
|
364
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
365
|
-
* @returns An array of binary tree node IDs.
|
|
366
|
-
*/
|
|
367
|
-
levelIterative(): BinaryTreeNodeKey[];
|
|
368
|
-
/**
|
|
369
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on their 'key' property.
|
|
370
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
371
|
-
* @returns An array of binary tree node IDs.
|
|
372
|
-
*/
|
|
373
|
-
levelIterative(node: N | null): BinaryTreeNodeKey[];
|
|
374
|
-
/**
|
|
375
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates properties of each node based on the specified property name.
|
|
376
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
377
|
-
* @param {string} nodeOrPropertyName - The name of the property to accumulate.
|
|
378
|
-
* @returns An array of values corresponding to the specified property.
|
|
379
|
-
*/
|
|
380
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'key'): BinaryTreeNodeKey[];
|
|
381
|
-
/**
|
|
382
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates the 'val' property of each node.
|
|
383
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
384
|
-
* @param {'val'} nodeOrPropertyName - The name of the property to accumulate.
|
|
385
|
-
* @returns An array of 'val' properties from each node.
|
|
386
|
-
*/
|
|
387
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'val'): N['val'][];
|
|
388
|
-
/**
|
|
389
|
-
* Performs a level-order traversal on a binary tree starting from the specified node and accumulates nodes themselves.
|
|
390
|
-
* @param {N | null} node - The starting node for the level order traversal. If null, the root node of the tree is used as the starting node.
|
|
391
|
-
* @param {'node'} nodeOrPropertyName - The name of the property to accumulate.
|
|
392
|
-
* @returns An array of binary tree nodes.
|
|
393
|
-
*/
|
|
394
|
-
levelIterative(node: N | null, nodeOrPropertyName: 'node'): N[];
|
|
332
|
+
dfs(pattern: DFSOrderPattern, nodeOrPropertyName: 'node', loopType?: LoopType): N[];
|
|
395
333
|
/**
|
|
396
334
|
* Collects nodes from a binary tree by a specified property and organizes them into levels.
|
|
397
335
|
* @returns A 2D array of AbstractBinaryTreeNodeProperty<N> objects.
|
|
@@ -477,11 +415,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
477
415
|
* undefined.
|
|
478
416
|
*/
|
|
479
417
|
protected _addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
480
|
-
/**
|
|
481
|
-
* The function sets the loop type for a protected variable.
|
|
482
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
483
|
-
*/
|
|
484
|
-
protected _setLoopType(value: LoopType): void;
|
|
485
418
|
/**
|
|
486
419
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
487
420
|
* parent property of the value to undefined.
|
|
@@ -118,14 +118,17 @@ class BinaryTree {
|
|
|
118
118
|
get loopType() {
|
|
119
119
|
return this._loopType;
|
|
120
120
|
}
|
|
121
|
+
set loopType(v) {
|
|
122
|
+
this._loopType = v;
|
|
123
|
+
}
|
|
121
124
|
/**
|
|
122
|
-
* The `
|
|
123
|
-
* @param {N} srcNode - The source node that you want to
|
|
125
|
+
* The `_swap` function swaps the location of two nodes in a binary tree.
|
|
126
|
+
* @param {N} srcNode - The source node that you want to _swap with the destination node.
|
|
124
127
|
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
|
125
128
|
* be swapped to.
|
|
126
129
|
* @returns The `destNode` is being returned.
|
|
127
130
|
*/
|
|
128
|
-
|
|
131
|
+
_swap(srcNode, destNode) {
|
|
129
132
|
const { key, val } = destNode;
|
|
130
133
|
const tempNode = this.createNode(key, val);
|
|
131
134
|
if (tempNode) {
|
|
@@ -262,13 +265,13 @@ class BinaryTree {
|
|
|
262
265
|
return keysOrNodes.length === this.addMany(keysOrNodes, data).length;
|
|
263
266
|
}
|
|
264
267
|
/**
|
|
265
|
-
* The `
|
|
268
|
+
* The `delete` function in TypeScript is used to delete a node from a binary search tree and returns an array of objects
|
|
266
269
|
* containing the deleted node and the node that needs to be balanced.
|
|
267
270
|
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object (`N`) or a binary tree
|
|
268
271
|
* node ID (`BinaryTreeNodeKey`).
|
|
269
|
-
* @returns The function `
|
|
272
|
+
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
270
273
|
*/
|
|
271
|
-
|
|
274
|
+
delete(nodeOrKey) {
|
|
272
275
|
const bstDeletedResult = [];
|
|
273
276
|
if (!this.root)
|
|
274
277
|
return bstDeletedResult;
|
|
@@ -297,7 +300,7 @@ class BinaryTree {
|
|
|
297
300
|
const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
|
|
298
301
|
if (leftSubTreeRightMost) {
|
|
299
302
|
const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
|
|
300
|
-
orgCurrent = this.
|
|
303
|
+
orgCurrent = this._swap(curr, leftSubTreeRightMost);
|
|
301
304
|
if (parentOfLeftSubTreeMax) {
|
|
302
305
|
if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
|
|
303
306
|
parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
|
|
@@ -593,12 +596,12 @@ class BinaryTree {
|
|
|
593
596
|
}
|
|
594
597
|
/**
|
|
595
598
|
* The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
|
|
596
|
-
* @param {N | null}
|
|
599
|
+
* @param {N | null} subTreeRoot - The `node` parameter represents the root node of a binary search tree (BST).
|
|
597
600
|
* @returns a boolean value.
|
|
598
601
|
*/
|
|
599
|
-
isSubtreeBST(
|
|
602
|
+
isSubtreeBST(subTreeRoot) {
|
|
600
603
|
// TODO there is a bug
|
|
601
|
-
if (!
|
|
604
|
+
if (!subTreeRoot)
|
|
602
605
|
return true;
|
|
603
606
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
604
607
|
const dfs = (cur, min, max) => {
|
|
@@ -608,11 +611,11 @@ class BinaryTree {
|
|
|
608
611
|
return false;
|
|
609
612
|
return dfs(cur.left, min, cur.key) && dfs(cur.right, cur.key, max);
|
|
610
613
|
};
|
|
611
|
-
return dfs(
|
|
614
|
+
return dfs(subTreeRoot, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
612
615
|
}
|
|
613
616
|
else {
|
|
614
617
|
const stack = [];
|
|
615
|
-
let prev = Number.MIN_SAFE_INTEGER, curr =
|
|
618
|
+
let prev = Number.MIN_SAFE_INTEGER, curr = subTreeRoot;
|
|
616
619
|
while (curr || stack.length > 0) {
|
|
617
620
|
while (curr) {
|
|
618
621
|
stack.push(curr);
|
|
@@ -715,33 +718,21 @@ class BinaryTree {
|
|
|
715
718
|
return sum;
|
|
716
719
|
}
|
|
717
720
|
/**
|
|
718
|
-
* The function `
|
|
721
|
+
* The function `subTreeForeach` adds a delta value to a specified property of each node in a subtree.
|
|
719
722
|
* @param {N | BinaryTreeNodeKey | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
|
|
720
723
|
* tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
|
|
721
|
-
* @param
|
|
722
|
-
* each node in the subtree should be incremented.
|
|
723
|
-
* @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
|
|
724
|
+
* @param callBack - The `callBack` parameter is a function that takes a node as a parameter and returns a number.
|
|
724
725
|
* specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'key'.
|
|
725
726
|
* @returns a boolean value.
|
|
726
727
|
*/
|
|
727
|
-
|
|
728
|
+
subTreeForeach(subTreeRoot, callback) {
|
|
728
729
|
if (typeof subTreeRoot === 'number')
|
|
729
730
|
subTreeRoot = this.get(subTreeRoot, 'key');
|
|
730
731
|
if (!subTreeRoot)
|
|
731
732
|
return false;
|
|
732
|
-
const _addByProperty = (cur) => {
|
|
733
|
-
switch (propertyName) {
|
|
734
|
-
case 'key':
|
|
735
|
-
cur.key += delta;
|
|
736
|
-
break;
|
|
737
|
-
default:
|
|
738
|
-
cur.key += delta;
|
|
739
|
-
break;
|
|
740
|
-
}
|
|
741
|
-
};
|
|
742
733
|
if (this._loopType === types_1.LoopType.RECURSIVE) {
|
|
743
734
|
const _traverse = (cur) => {
|
|
744
|
-
|
|
735
|
+
callback(cur);
|
|
745
736
|
cur.left && _traverse(cur.left);
|
|
746
737
|
cur.right && _traverse(cur.right);
|
|
747
738
|
};
|
|
@@ -751,7 +742,7 @@ class BinaryTree {
|
|
|
751
742
|
const stack = [subTreeRoot];
|
|
752
743
|
while (stack.length > 0) {
|
|
753
744
|
const cur = stack.pop();
|
|
754
|
-
|
|
745
|
+
callback(cur);
|
|
755
746
|
cur.right && stack.push(cur.right);
|
|
756
747
|
cur.left && stack.push(cur.left);
|
|
757
748
|
}
|
|
@@ -769,6 +760,7 @@ class BinaryTree {
|
|
|
769
760
|
this._clearResults();
|
|
770
761
|
const queue = [this.root];
|
|
771
762
|
while (queue.length !== 0) {
|
|
763
|
+
// TODO Array.shift is not efficient, consider using Deque
|
|
772
764
|
const cur = queue.shift();
|
|
773
765
|
if (cur) {
|
|
774
766
|
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
@@ -785,111 +777,74 @@ class BinaryTree {
|
|
|
785
777
|
* each node based on the specified pattern and property name.
|
|
786
778
|
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
787
779
|
* @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'`.
|
|
780
|
+
* @param loopType - The type of loop to use for the depth-first search traversal. The default value is `LoopType.ITERATIVE`.
|
|
788
781
|
* @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.
|
|
789
782
|
*/
|
|
790
|
-
dfs(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
783
|
+
dfs(pattern = 'in', nodeOrPropertyName = 'key', loopType = types_1.LoopType.ITERATIVE) {
|
|
791
784
|
this._clearResults();
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
case 'in':
|
|
795
|
-
if (node.left)
|
|
796
|
-
_traverse(node.left);
|
|
797
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
798
|
-
if (node.right)
|
|
799
|
-
_traverse(node.right);
|
|
800
|
-
break;
|
|
801
|
-
case 'pre':
|
|
802
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
803
|
-
if (node.left)
|
|
804
|
-
_traverse(node.left);
|
|
805
|
-
if (node.right)
|
|
806
|
-
_traverse(node.right);
|
|
807
|
-
break;
|
|
808
|
-
case 'post':
|
|
809
|
-
if (node.left)
|
|
810
|
-
_traverse(node.left);
|
|
811
|
-
if (node.right)
|
|
812
|
-
_traverse(node.right);
|
|
813
|
-
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
814
|
-
break;
|
|
815
|
-
}
|
|
816
|
-
};
|
|
817
|
-
this.root && _traverse(this.root);
|
|
818
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* The dfsIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
|
|
822
|
-
* specify the traversal pattern and the property name to accumulate results by.
|
|
823
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The traversal pattern: 'in' (in-order), 'pre' (pre-order), or 'post' (post-order).
|
|
824
|
-
* @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'`.
|
|
825
|
-
* @returns An object of type BinaryTreeNodeProperties<N>.
|
|
826
|
-
*/
|
|
827
|
-
dfsIterative(pattern = 'in', nodeOrPropertyName = 'key') {
|
|
828
|
-
this._clearResults();
|
|
829
|
-
if (!this.root)
|
|
830
|
-
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
831
|
-
// 0: visit, 1: print
|
|
832
|
-
const stack = [{ opt: 0, node: this.root }];
|
|
833
|
-
while (stack.length > 0) {
|
|
834
|
-
const cur = stack.pop();
|
|
835
|
-
if (!cur || !cur.node)
|
|
836
|
-
continue;
|
|
837
|
-
if (cur.opt === 1) {
|
|
838
|
-
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
839
|
-
}
|
|
840
|
-
else {
|
|
785
|
+
if (loopType === types_1.LoopType.RECURSIVE) {
|
|
786
|
+
const _traverse = (node) => {
|
|
841
787
|
switch (pattern) {
|
|
842
788
|
case 'in':
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
789
|
+
if (node.left)
|
|
790
|
+
_traverse(node.left);
|
|
791
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
792
|
+
if (node.right)
|
|
793
|
+
_traverse(node.right);
|
|
846
794
|
break;
|
|
847
795
|
case 'pre':
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
796
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
797
|
+
if (node.left)
|
|
798
|
+
_traverse(node.left);
|
|
799
|
+
if (node.right)
|
|
800
|
+
_traverse(node.right);
|
|
851
801
|
break;
|
|
852
802
|
case 'post':
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
stack.push({ opt: 0, node: cur.node.right });
|
|
859
|
-
stack.push({ opt: 1, node: cur.node });
|
|
860
|
-
stack.push({ opt: 0, node: cur.node.left });
|
|
803
|
+
if (node.left)
|
|
804
|
+
_traverse(node.left);
|
|
805
|
+
if (node.right)
|
|
806
|
+
_traverse(node.right);
|
|
807
|
+
this._accumulatedByPropertyName(node, nodeOrPropertyName);
|
|
861
808
|
break;
|
|
862
809
|
}
|
|
863
|
-
}
|
|
810
|
+
};
|
|
811
|
+
this.root && _traverse(this.root);
|
|
864
812
|
}
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
* accumulating results based on the 'key' property.
|
|
877
|
-
* @returns An object of type `BinaryTreeNodeProperties<N>`.
|
|
878
|
-
*/
|
|
879
|
-
levelIterative(node = this.root, nodeOrPropertyName = 'key') {
|
|
880
|
-
if (!node)
|
|
881
|
-
return [];
|
|
882
|
-
this._clearResults();
|
|
883
|
-
const queue = [node];
|
|
884
|
-
while (queue.length > 0) {
|
|
885
|
-
const cur = queue.shift();
|
|
886
|
-
if (cur) {
|
|
887
|
-
this._accumulatedByPropertyName(cur, nodeOrPropertyName);
|
|
888
|
-
if (cur.left) {
|
|
889
|
-
queue.push(cur.left);
|
|
813
|
+
else {
|
|
814
|
+
if (!this.root)
|
|
815
|
+
return this._getResultByPropertyName(nodeOrPropertyName);
|
|
816
|
+
// 0: visit, 1: print
|
|
817
|
+
const stack = [{ opt: 0, node: this.root }];
|
|
818
|
+
while (stack.length > 0) {
|
|
819
|
+
const cur = stack.pop();
|
|
820
|
+
if (!cur || !cur.node)
|
|
821
|
+
continue;
|
|
822
|
+
if (cur.opt === 1) {
|
|
823
|
+
this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
|
|
890
824
|
}
|
|
891
|
-
|
|
892
|
-
|
|
825
|
+
else {
|
|
826
|
+
switch (pattern) {
|
|
827
|
+
case 'in':
|
|
828
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
829
|
+
stack.push({ opt: 1, node: cur.node });
|
|
830
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
831
|
+
break;
|
|
832
|
+
case 'pre':
|
|
833
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
834
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
835
|
+
stack.push({ opt: 1, node: cur.node });
|
|
836
|
+
break;
|
|
837
|
+
case 'post':
|
|
838
|
+
stack.push({ opt: 1, node: cur.node });
|
|
839
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
840
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
841
|
+
break;
|
|
842
|
+
default:
|
|
843
|
+
stack.push({ opt: 0, node: cur.node.right });
|
|
844
|
+
stack.push({ opt: 1, node: cur.node });
|
|
845
|
+
stack.push({ opt: 0, node: cur.node.left });
|
|
846
|
+
break;
|
|
847
|
+
}
|
|
893
848
|
}
|
|
894
849
|
}
|
|
895
850
|
}
|
|
@@ -1094,13 +1049,6 @@ class BinaryTree {
|
|
|
1094
1049
|
return;
|
|
1095
1050
|
}
|
|
1096
1051
|
}
|
|
1097
|
-
/**
|
|
1098
|
-
* The function sets the loop type for a protected variable.
|
|
1099
|
-
* @param {LoopType} value - The value parameter is of type LoopType.
|
|
1100
|
-
*/
|
|
1101
|
-
_setLoopType(value) {
|
|
1102
|
-
this._loopType = value;
|
|
1103
|
-
}
|
|
1104
1052
|
/**
|
|
1105
1053
|
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
|
1106
1054
|
* parent property of the value to undefined.
|