data-structure-typed 1.49.9 → 1.50.1
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/CHANGELOG.md +1 -1
- package/README.md +192 -200
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +158 -152
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/cjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/cjs/data-structures/hash/hash-map.js +6 -6
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/mjs/data-structures/binary-tree/bst.js +131 -46
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/mjs/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/mjs/data-structures/hash/hash-map.js +15 -15
- package/dist/umd/data-structure-typed.js +143 -153
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +12 -41
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -18
- package/src/data-structures/binary-tree/binary-tree.ts +111 -149
- package/src/data-structures/binary-tree/bst.ts +159 -56
- package/src/data-structures/binary-tree/rb-tree.ts +7 -18
- package/src/data-structures/binary-tree/tree-multimap.ts +8 -19
- package/src/data-structures/graph/abstract-graph.ts +14 -15
- package/src/data-structures/graph/directed-graph.ts +6 -7
- package/src/data-structures/graph/undirected-graph.ts +6 -7
- package/src/data-structures/hash/hash-map.ts +23 -22
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/queue/deque.ts +3 -3
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +1 -1
- package/src/data-structures/trie/trie.ts +1 -1
- package/src/types/data-structures/graph/abstract-graph.ts +8 -8
- package/test/integration/all-in-one.test.ts +1 -1
- package/test/integration/index.html +2 -2
- package/test/performance/data-structures/queue/deque.test.ts +8 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +38 -9
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -12
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +4 -4
- package/test/unit/data-structures/graph/abstract-graph.test.ts +2 -1
- package/test/unit/data-structures/hash/hash-map.test.ts +4 -2
- package/test/unit/data-structures/queue/deque.test.ts +8 -6
- package/test/unit/data-structures/queue/queue.test.ts +1 -1
- package/test/unit/unrestricted-interconversion.test.ts +143 -10
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.50.1",
|
|
4
|
+
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
7
7
|
"types": "dist/mjs/index.d.ts",
|
|
@@ -92,17 +92,14 @@
|
|
|
92
92
|
"typescript": "^5.3.2"
|
|
93
93
|
},
|
|
94
94
|
"keywords": [
|
|
95
|
-
"data",
|
|
96
|
-
"structure",
|
|
97
|
-
"structures",
|
|
98
95
|
"data structure",
|
|
99
|
-
"datastructure",
|
|
100
96
|
"data-structure",
|
|
101
|
-
"
|
|
102
|
-
"
|
|
103
|
-
"data
|
|
104
|
-
"
|
|
105
|
-
"
|
|
97
|
+
"datastructure",
|
|
98
|
+
"javascript data structure",
|
|
99
|
+
"typescript data structure",
|
|
100
|
+
"js data structure",
|
|
101
|
+
"data",
|
|
102
|
+
"structure",
|
|
106
103
|
"binary",
|
|
107
104
|
"depth",
|
|
108
105
|
"breadth",
|
|
@@ -112,7 +109,7 @@
|
|
|
112
109
|
"avl",
|
|
113
110
|
"red",
|
|
114
111
|
"black",
|
|
115
|
-
"
|
|
112
|
+
"red black",
|
|
116
113
|
"RB",
|
|
117
114
|
"segment",
|
|
118
115
|
"prefix",
|
|
@@ -150,7 +147,6 @@
|
|
|
150
147
|
"Python",
|
|
151
148
|
"collections",
|
|
152
149
|
"Python Collections",
|
|
153
|
-
"pythoncollections",
|
|
154
150
|
"python-collections",
|
|
155
151
|
"C#",
|
|
156
152
|
"System.Collections.Generic",
|
|
@@ -159,41 +155,30 @@
|
|
|
159
155
|
"Java",
|
|
160
156
|
"util",
|
|
161
157
|
"binary search tree",
|
|
162
|
-
"binarysearchtree",
|
|
163
158
|
"binary-search-tree",
|
|
164
159
|
"BST",
|
|
165
160
|
"binary tree",
|
|
166
|
-
"binarytree",
|
|
167
161
|
"binary-tree",
|
|
168
162
|
"red black tree",
|
|
169
|
-
"
|
|
170
|
-
"redblack tree",
|
|
163
|
+
"red black tree",
|
|
171
164
|
"red-black-tree",
|
|
172
|
-
"
|
|
165
|
+
"rb tree",
|
|
173
166
|
"trie",
|
|
174
167
|
"prefix tree",
|
|
175
|
-
"prefixtree",
|
|
176
168
|
"prefix-tree",
|
|
177
169
|
"avl tree",
|
|
178
|
-
"avltree",
|
|
179
170
|
"avl-tree",
|
|
180
171
|
"tree set",
|
|
181
|
-
"treeset",
|
|
182
172
|
"tree-set",
|
|
183
173
|
"tree multiset",
|
|
184
|
-
"treemultiset",
|
|
185
174
|
"tree-multiset",
|
|
186
175
|
"tree map",
|
|
187
|
-
"treemap",
|
|
188
176
|
"tree-map",
|
|
189
177
|
"tree multimap",
|
|
190
|
-
"treemultimap",
|
|
191
178
|
"tree-multimap",
|
|
192
179
|
"binary indexed tree",
|
|
193
|
-
"binaryindexedtree",
|
|
194
180
|
"binary-indexed-tree",
|
|
195
181
|
"segment tree",
|
|
196
|
-
"segmenttree",
|
|
197
182
|
"segment-tree",
|
|
198
183
|
"sort",
|
|
199
184
|
"sorted",
|
|
@@ -203,60 +188,46 @@
|
|
|
203
188
|
"morris",
|
|
204
189
|
"Morris",
|
|
205
190
|
"bellman ford",
|
|
206
|
-
"bellmanford",
|
|
207
191
|
"bellman-ford",
|
|
208
192
|
"dijkstra",
|
|
209
193
|
"Dijkstra",
|
|
210
194
|
"floyd warshall",
|
|
211
|
-
"floydwarshall",
|
|
212
195
|
"floyd-warshall",
|
|
213
196
|
"tarjan",
|
|
214
197
|
"tarjan's",
|
|
215
198
|
"dfs",
|
|
216
199
|
"depth first Search",
|
|
217
|
-
"depthfirstSearch",
|
|
218
200
|
"depth-first-Search",
|
|
219
201
|
"bfs",
|
|
220
202
|
"breadth first search",
|
|
221
|
-
"breadthfirstsearch",
|
|
222
203
|
"dfs iterative",
|
|
223
204
|
"recursive",
|
|
224
205
|
"iterative",
|
|
225
206
|
"directed graph",
|
|
226
|
-
"directedgraph",
|
|
227
207
|
"directed-graph",
|
|
228
208
|
"undirected graph",
|
|
229
|
-
"undirectedgraph",
|
|
230
209
|
"undirected-graph",
|
|
231
210
|
"min heap",
|
|
232
|
-
"minheap",
|
|
233
211
|
"min-heap",
|
|
234
212
|
"max heap",
|
|
235
|
-
"maxheap",
|
|
236
213
|
"max-heap",
|
|
237
214
|
"priority queue",
|
|
238
|
-
"priorityqueue",
|
|
239
215
|
"priority-queue",
|
|
240
216
|
"max priority queue",
|
|
241
|
-
"maxpriorityqueue",
|
|
242
217
|
"max-priority-queue",
|
|
243
218
|
"min priority queue",
|
|
244
|
-
"minpriorityqueue",
|
|
245
219
|
"min-priority-queue",
|
|
246
220
|
"hash",
|
|
247
221
|
"map",
|
|
248
|
-
"hash map",
|
|
249
222
|
"hashmap",
|
|
223
|
+
"hash map",
|
|
250
224
|
"hash-map",
|
|
251
225
|
"deque",
|
|
252
226
|
"linked list",
|
|
253
|
-
"linkedlist",
|
|
254
227
|
"linked-list",
|
|
255
228
|
"singly linked list",
|
|
256
|
-
"singlylinkedlist",
|
|
257
229
|
"singly-linked-list",
|
|
258
230
|
"doubly linked list",
|
|
259
|
-
"doublylinkedlist",
|
|
260
231
|
"doubly-linked-list",
|
|
261
232
|
"stack",
|
|
262
233
|
"CommonJS",
|
|
@@ -15,7 +15,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
15
15
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
16
16
|
* parameter is used to pass any additional arguments to the `_getIterator` method.
|
|
17
17
|
*/
|
|
18
|
-
*[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
18
|
+
* [Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
|
|
19
19
|
yield* this._getIterator(...args);
|
|
20
20
|
}
|
|
21
21
|
|
|
@@ -30,7 +30,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
30
30
|
* The function returns an iterator that yields key-value pairs from the object, where the value can
|
|
31
31
|
* be undefined.
|
|
32
32
|
*/
|
|
33
|
-
*entries(): IterableIterator<[K, V | undefined]> {
|
|
33
|
+
* entries(): IterableIterator<[K, V | undefined]> {
|
|
34
34
|
for (const item of this) {
|
|
35
35
|
yield item;
|
|
36
36
|
}
|
|
@@ -46,7 +46,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
46
46
|
*
|
|
47
47
|
* The function returns an iterator that yields the keys of a data structure.
|
|
48
48
|
*/
|
|
49
|
-
*keys(): IterableIterator<K> {
|
|
49
|
+
* keys(): IterableIterator<K> {
|
|
50
50
|
for (const item of this) {
|
|
51
51
|
yield item[0];
|
|
52
52
|
}
|
|
@@ -62,7 +62,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
62
62
|
*
|
|
63
63
|
* The function returns an iterator that yields the values of a collection.
|
|
64
64
|
*/
|
|
65
|
-
*values(): IterableIterator<V> {
|
|
65
|
+
* values(): IterableIterator<V> {
|
|
66
66
|
for (const item of this) {
|
|
67
67
|
yield item[1];
|
|
68
68
|
}
|
|
@@ -212,7 +212,7 @@ export abstract class IterableElementBase<V> {
|
|
|
212
212
|
* allows the function to accept any number of arguments as an array. In this case, the `args`
|
|
213
213
|
* parameter is used to pass any number of arguments to the `_getIterator` method.
|
|
214
214
|
*/
|
|
215
|
-
*[Symbol.iterator](...args: any[]): IterableIterator<V> {
|
|
215
|
+
* [Symbol.iterator](...args: any[]): IterableIterator<V> {
|
|
216
216
|
yield* this._getIterator(...args);
|
|
217
217
|
}
|
|
218
218
|
|
|
@@ -226,7 +226,7 @@ export abstract class IterableElementBase<V> {
|
|
|
226
226
|
*
|
|
227
227
|
* The function returns an iterator that yields all the values in the object.
|
|
228
228
|
*/
|
|
229
|
-
*values(): IterableIterator<V> {
|
|
229
|
+
* values(): IterableIterator<V> {
|
|
230
230
|
for (const item of this) {
|
|
231
231
|
yield item;
|
|
232
232
|
}
|
|
@@ -40,14 +40,13 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
|
|
|
40
40
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
41
41
|
*/
|
|
42
42
|
export class AVLTree<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
K = any,
|
|
44
|
+
V = any,
|
|
45
|
+
N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
46
|
+
TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>
|
|
47
|
+
>
|
|
48
48
|
extends BST<K, V, N, TREE>
|
|
49
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
50
|
-
{
|
|
49
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
51
50
|
/**
|
|
52
51
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
53
52
|
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
@@ -99,16 +98,6 @@ export class AVLTree<
|
|
|
99
98
|
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
100
99
|
}
|
|
101
100
|
|
|
102
|
-
/**
|
|
103
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
104
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
105
|
-
* data type.
|
|
106
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
107
|
-
*/
|
|
108
|
-
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
109
|
-
return !(potentialKey instanceof AVLTreeNode);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
101
|
/**
|
|
113
102
|
* Time Complexity: O(log n)
|
|
114
103
|
* Space Complexity: O(1)
|
|
@@ -278,7 +267,7 @@ export class AVLTree<
|
|
|
278
267
|
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
279
268
|
switch (
|
|
280
269
|
this._balanceFactor(A) // second O(1)
|
|
281
|
-
|
|
270
|
+
) {
|
|
282
271
|
case -2:
|
|
283
272
|
if (A && A.left) {
|
|
284
273
|
if (this._balanceFactor(A.left) <= 0) {
|
|
@@ -101,14 +101,13 @@ export class BinaryTreeNode<
|
|
|
101
101
|
*/
|
|
102
102
|
|
|
103
103
|
export class BinaryTree<
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
K = any,
|
|
105
|
+
V = any,
|
|
106
|
+
N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
107
|
+
TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>
|
|
108
|
+
>
|
|
109
109
|
extends IterableEntryBase<K, V | undefined>
|
|
110
|
-
implements IBinaryTree<K, V, N, TREE>
|
|
111
|
-
{
|
|
110
|
+
implements IBinaryTree<K, V, N, TREE> {
|
|
112
111
|
iterationType = IterationType.ITERATIVE;
|
|
113
112
|
|
|
114
113
|
/**
|
|
@@ -197,7 +196,7 @@ export class BinaryTree<
|
|
|
197
196
|
}
|
|
198
197
|
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
199
198
|
node = keyOrNodeOrEntry;
|
|
200
|
-
} else if (this.
|
|
199
|
+
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
201
200
|
node = this.createNode(keyOrNodeOrEntry, value);
|
|
202
201
|
} else {
|
|
203
202
|
return;
|
|
@@ -293,16 +292,6 @@ export class BinaryTree<
|
|
|
293
292
|
return this.isRealNode(node) || node === null;
|
|
294
293
|
}
|
|
295
294
|
|
|
296
|
-
/**
|
|
297
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
298
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
299
|
-
* data type.
|
|
300
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
301
|
-
*/
|
|
302
|
-
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
303
|
-
return !(potentialKey instanceof BinaryTreeNode);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
295
|
/**
|
|
307
296
|
* Time Complexity O(log n) - O(n)
|
|
308
297
|
* Space Complexity O(1)
|
|
@@ -1029,7 +1018,7 @@ export class BinaryTree<
|
|
|
1029
1018
|
*
|
|
1030
1019
|
* The function `getPathToRoot` returns an array of nodes from a given node to the root of a tree
|
|
1031
1020
|
* structure, with the option to reverse the order of the nodes.
|
|
1032
|
-
* @param {K | N | null | undefined}
|
|
1021
|
+
* @param {K | N | null | undefined} beginNode - The `beginRoot` parameter represents the
|
|
1033
1022
|
* starting node from which you want to find the path to the root. It can be of type `K`, `N`,
|
|
1034
1023
|
* `null`, or `undefined`.
|
|
1035
1024
|
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
@@ -1037,20 +1026,20 @@ export class BinaryTree<
|
|
|
1037
1026
|
* reversed before returning it. If `isReverse` is set to `false`, the path will be returned as is
|
|
1038
1027
|
* @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
|
|
1039
1028
|
*/
|
|
1040
|
-
getPathToRoot(
|
|
1029
|
+
getPathToRoot(beginNode: KeyOrNodeOrEntry<K, V, N>, isReverse = true): N[] {
|
|
1041
1030
|
// TODO to support get path through passing key
|
|
1042
1031
|
const result: N[] = [];
|
|
1043
|
-
|
|
1032
|
+
beginNode = this.ensureNode(beginNode);
|
|
1044
1033
|
|
|
1045
|
-
if (!
|
|
1034
|
+
if (!beginNode) return result;
|
|
1046
1035
|
|
|
1047
|
-
while (
|
|
1036
|
+
while (beginNode.parent) {
|
|
1048
1037
|
// Array.push + Array.reverse is more efficient than Array.unshift
|
|
1049
1038
|
// TODO may consider using Deque, so far this is not the performance bottleneck
|
|
1050
|
-
result.push(
|
|
1051
|
-
|
|
1039
|
+
result.push(beginNode);
|
|
1040
|
+
beginNode = beginNode.parent;
|
|
1052
1041
|
}
|
|
1053
|
-
result.push(
|
|
1042
|
+
result.push(beginNode);
|
|
1054
1043
|
return isReverse ? result.reverse() : result;
|
|
1055
1044
|
}
|
|
1056
1045
|
|
|
@@ -1203,99 +1192,94 @@ export class BinaryTree<
|
|
|
1203
1192
|
}
|
|
1204
1193
|
}
|
|
1205
1194
|
|
|
1206
|
-
/**
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
): ReturnType<C>[];
|
|
1217
|
-
|
|
1218
|
-
subTreeTraverse<C extends BTNCallback<N>>(
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
): ReturnType<C>[];
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
1296
|
-
}
|
|
1297
|
-
return ans;
|
|
1298
|
-
}
|
|
1195
|
+
// /**
|
|
1196
|
+
// * Time complexity: O(n)
|
|
1197
|
+
// * Space complexity: O(log n)
|
|
1198
|
+
// */
|
|
1199
|
+
//
|
|
1200
|
+
// subTreeTraverse<C extends BTNCallback<N>>(
|
|
1201
|
+
// callback?: C,
|
|
1202
|
+
// beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1203
|
+
// iterationType?: IterationType,
|
|
1204
|
+
// includeNull?: false
|
|
1205
|
+
// ): ReturnType<C>[];
|
|
1206
|
+
//
|
|
1207
|
+
// subTreeTraverse<C extends BTNCallback<N | null>>(
|
|
1208
|
+
// callback?: C,
|
|
1209
|
+
// beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1210
|
+
// iterationType?: IterationType,
|
|
1211
|
+
// includeNull?: true
|
|
1212
|
+
// ): ReturnType<C>[];
|
|
1213
|
+
//
|
|
1214
|
+
// /**
|
|
1215
|
+
// * Time complexity: O(n)
|
|
1216
|
+
// * Space complexity: O(log n)
|
|
1217
|
+
// *
|
|
1218
|
+
// * The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
1219
|
+
// * node, either recursively or iteratively.
|
|
1220
|
+
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
1221
|
+
// * the subtree traversal. It takes a single parameter, which is the current node being traversed, and
|
|
1222
|
+
// * returns a value of any type.
|
|
1223
|
+
// * @param {K | N | null | undefined} beginRoot - The `beginRoot` parameter represents the
|
|
1224
|
+
// * starting node or key from which the subtree traversal should begin. It can be of type `K`,
|
|
1225
|
+
// * `N`, `null`, or `undefined`. If not provided, the `root` property of the current object is used as
|
|
1226
|
+
// * the default value.
|
|
1227
|
+
// * @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
1228
|
+
// * performed on the subtree. It can have two possible values:
|
|
1229
|
+
// * @param [includeNull=false] - The `includeNull` parameter is a boolean value that determines
|
|
1230
|
+
// * whether to include null values in the traversal. If `includeNull` is set to `true`, the
|
|
1231
|
+
// * traversal will include null values, otherwise it will skip them.
|
|
1232
|
+
// * @returns The function `subTreeTraverse` returns an array of values that are the result of invoking
|
|
1233
|
+
// * the `callback` function on each node in the subtree. The type of the array nodes is determined
|
|
1234
|
+
// * by the return type of the `callback` function.
|
|
1235
|
+
// */
|
|
1236
|
+
// subTreeTraverse<C extends BTNCallback<N | null | undefined>>(
|
|
1237
|
+
// callback: C = this._defaultOneParamCallback as C,
|
|
1238
|
+
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1239
|
+
// iterationType = this.iterationType,
|
|
1240
|
+
// includeNull = false
|
|
1241
|
+
// ): ReturnType<C>[] {
|
|
1242
|
+
// console.warn('subTreeTraverse is unnecessary, since the dfs method can substitute it.');
|
|
1243
|
+
//
|
|
1244
|
+
// beginRoot = this.ensureNode(beginRoot);
|
|
1245
|
+
//
|
|
1246
|
+
// const ans: (ReturnType<BTNCallback<N>> | null | undefined)[] = [];
|
|
1247
|
+
// if (!beginRoot) return ans;
|
|
1248
|
+
//
|
|
1249
|
+
// if (iterationType === IterationType.RECURSIVE) {
|
|
1250
|
+
// const _traverse = (cur: N | null | undefined) => {
|
|
1251
|
+
// if (cur !== undefined) {
|
|
1252
|
+
// ans.push(callback(cur));
|
|
1253
|
+
// if (includeNull) {
|
|
1254
|
+
// cur && this.isNodeOrNull(cur.left) && _traverse(cur.left);
|
|
1255
|
+
// cur && this.isNodeOrNull(cur.right) && _traverse(cur.right);
|
|
1256
|
+
// } else {
|
|
1257
|
+
// cur && cur.left && _traverse(cur.left);
|
|
1258
|
+
// cur && cur.right && _traverse(cur.right);
|
|
1259
|
+
// }
|
|
1260
|
+
// }
|
|
1261
|
+
// };
|
|
1262
|
+
//
|
|
1263
|
+
// _traverse(beginRoot);
|
|
1264
|
+
// } else {
|
|
1265
|
+
// const stack: (N | null | undefined)[] = [beginRoot];
|
|
1266
|
+
//
|
|
1267
|
+
// while (stack.length > 0) {
|
|
1268
|
+
// const cur = stack.pop();
|
|
1269
|
+
// if (cur !== undefined) {
|
|
1270
|
+
// ans.push(callback(cur));
|
|
1271
|
+
// if (includeNull) {
|
|
1272
|
+
// cur && this.isNodeOrNull(cur.right) && stack.push(cur.right);
|
|
1273
|
+
// cur && this.isNodeOrNull(cur.left) && stack.push(cur.left);
|
|
1274
|
+
// } else {
|
|
1275
|
+
// cur && cur.right && stack.push(cur.right);
|
|
1276
|
+
// cur && cur.left && stack.push(cur.left);
|
|
1277
|
+
// }
|
|
1278
|
+
// }
|
|
1279
|
+
// }
|
|
1280
|
+
// }
|
|
1281
|
+
// return ans;
|
|
1282
|
+
// }
|
|
1299
1283
|
|
|
1300
1284
|
dfs<C extends BTNCallback<N>>(
|
|
1301
1285
|
callback?: C,
|
|
@@ -1305,15 +1289,7 @@ export class BinaryTree<
|
|
|
1305
1289
|
includeNull?: false
|
|
1306
1290
|
): ReturnType<C>[];
|
|
1307
1291
|
|
|
1308
|
-
dfs<C extends BTNCallback<N>>(
|
|
1309
|
-
callback?: C,
|
|
1310
|
-
pattern?: DFSOrderPattern,
|
|
1311
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1312
|
-
iterationType?: IterationType,
|
|
1313
|
-
includeNull?: undefined
|
|
1314
|
-
): ReturnType<C>[];
|
|
1315
|
-
|
|
1316
|
-
dfs<C extends BTNCallback<N | null | undefined>>(
|
|
1292
|
+
dfs<C extends BTNCallback<N | null>>(
|
|
1317
1293
|
callback?: C,
|
|
1318
1294
|
pattern?: DFSOrderPattern,
|
|
1319
1295
|
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
@@ -1451,14 +1427,7 @@ export class BinaryTree<
|
|
|
1451
1427
|
includeNull?: false
|
|
1452
1428
|
): ReturnType<C>[];
|
|
1453
1429
|
|
|
1454
|
-
bfs<C extends BTNCallback<N>>(
|
|
1455
|
-
callback?: C,
|
|
1456
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1457
|
-
iterationType?: IterationType,
|
|
1458
|
-
includeNull?: undefined
|
|
1459
|
-
): ReturnType<C>[];
|
|
1460
|
-
|
|
1461
|
-
bfs<C extends BTNCallback<N | null | undefined>>(
|
|
1430
|
+
bfs<C extends BTNCallback<N | null>>(
|
|
1462
1431
|
callback?: C,
|
|
1463
1432
|
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1464
1433
|
iterationType?: IterationType,
|
|
@@ -1486,7 +1455,7 @@ export class BinaryTree<
|
|
|
1486
1455
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1487
1456
|
* the breadth-first traversal of a binary tree.
|
|
1488
1457
|
*/
|
|
1489
|
-
bfs<C extends BTNCallback<N | null
|
|
1458
|
+
bfs<C extends BTNCallback<N | null>>(
|
|
1490
1459
|
callback: C = this._defaultOneParamCallback as C,
|
|
1491
1460
|
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1492
1461
|
iterationType = this.iterationType,
|
|
@@ -1552,14 +1521,7 @@ export class BinaryTree<
|
|
|
1552
1521
|
includeNull?: false
|
|
1553
1522
|
): ReturnType<C>[][];
|
|
1554
1523
|
|
|
1555
|
-
listLevels<C extends BTNCallback<N>>(
|
|
1556
|
-
callback?: C,
|
|
1557
|
-
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1558
|
-
iterationType?: IterationType,
|
|
1559
|
-
includeNull?: undefined
|
|
1560
|
-
): ReturnType<C>[][];
|
|
1561
|
-
|
|
1562
|
-
listLevels<C extends BTNCallback<N | null | undefined>>(
|
|
1524
|
+
listLevels<C extends BTNCallback<N | null>>(
|
|
1563
1525
|
callback?: C,
|
|
1564
1526
|
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
|
|
1565
1527
|
iterationType?: IterationType,
|
|
@@ -1587,7 +1549,7 @@ export class BinaryTree<
|
|
|
1587
1549
|
* be excluded
|
|
1588
1550
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1589
1551
|
*/
|
|
1590
|
-
listLevels<C extends BTNCallback<N | null
|
|
1552
|
+
listLevels<C extends BTNCallback<N | null>>(
|
|
1591
1553
|
callback: C = this._defaultOneParamCallback as C,
|
|
1592
1554
|
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
1593
1555
|
iterationType = this.iterationType,
|
|
@@ -1598,7 +1560,7 @@ export class BinaryTree<
|
|
|
1598
1560
|
if (!beginRoot) return levelsNodes;
|
|
1599
1561
|
|
|
1600
1562
|
if (iterationType === IterationType.RECURSIVE) {
|
|
1601
|
-
const _recursive = (node: N | null
|
|
1563
|
+
const _recursive = (node: N | null, level: number) => {
|
|
1602
1564
|
if (!levelsNodes[level]) levelsNodes[level] = [];
|
|
1603
1565
|
levelsNodes[level].push(callback(node));
|
|
1604
1566
|
if (includeNull) {
|
|
@@ -1612,7 +1574,7 @@ export class BinaryTree<
|
|
|
1612
1574
|
|
|
1613
1575
|
_recursive(beginRoot, 0);
|
|
1614
1576
|
} else {
|
|
1615
|
-
const stack: [N | null
|
|
1577
|
+
const stack: [N | null, number][] = [[beginRoot, 0]];
|
|
1616
1578
|
|
|
1617
1579
|
while (stack.length > 0) {
|
|
1618
1580
|
const head = stack.pop()!;
|
|
@@ -1923,7 +1885,7 @@ export class BinaryTree<
|
|
|
1923
1885
|
display(beginRoot);
|
|
1924
1886
|
}
|
|
1925
1887
|
|
|
1926
|
-
protected
|
|
1888
|
+
protected* _getIterator(node = this.root): IterableIterator<[K, V | undefined]> {
|
|
1927
1889
|
if (!node) return;
|
|
1928
1890
|
|
|
1929
1891
|
if (this.iterationType === IterationType.ITERATIVE) {
|