min-heap-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/dist/data-structures/binary-tree/avl-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/avl-tree.js +0 -9
- package/dist/data-structures/binary-tree/binary-tree.d.ts +5 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -80
- package/dist/data-structures/binary-tree/bst.d.ts +89 -27
- package/dist/data-structures/binary-tree/bst.js +131 -46
- package/dist/data-structures/binary-tree/rb-tree.d.ts +0 -7
- package/dist/data-structures/binary-tree/rb-tree.js +1 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +0 -7
- package/dist/data-structures/binary-tree/tree-multimap.js +2 -11
- package/dist/data-structures/hash/hash-map.d.ts +3 -3
- package/dist/data-structures/hash/hash-map.js +6 -6
- package/package.json +2 -2
- 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
|
@@ -18,9 +18,6 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
18
18
|
[key: string]: HashMapStoreItem<K, V>;
|
|
19
19
|
};
|
|
20
20
|
protected _objMap: Map<object, V>;
|
|
21
|
-
protected _toEntryFn: (rawElement: R) => [K, V];
|
|
22
|
-
get toEntryFn(): (rawElement: R) => [K, V];
|
|
23
|
-
isEntry(rawElement: any): rawElement is [K, V];
|
|
24
21
|
/**
|
|
25
22
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
26
23
|
* options.
|
|
@@ -29,8 +26,11 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
|
|
|
29
26
|
* @param [options] - The `options` parameter is an optional object that can contain two properties:
|
|
30
27
|
*/
|
|
31
28
|
constructor(rawCollection?: Iterable<R>, options?: HashMapOptions<K, V, R>);
|
|
29
|
+
protected _toEntryFn: (rawElement: R) => [K, V];
|
|
30
|
+
get toEntryFn(): (rawElement: R) => [K, V];
|
|
32
31
|
protected _size: number;
|
|
33
32
|
get size(): number;
|
|
33
|
+
isEntry(rawElement: any): rawElement is [K, V];
|
|
34
34
|
isEmpty(): boolean;
|
|
35
35
|
clear(): void;
|
|
36
36
|
/**
|
|
@@ -10,12 +10,6 @@ const utils_1 = require("../../utils");
|
|
|
10
10
|
* 4. Unordered Collection: HashMap does not guarantee the order of entries, and the order may change over time.
|
|
11
11
|
*/
|
|
12
12
|
class HashMap extends base_1.IterableEntryBase {
|
|
13
|
-
get toEntryFn() {
|
|
14
|
-
return this._toEntryFn;
|
|
15
|
-
}
|
|
16
|
-
isEntry(rawElement) {
|
|
17
|
-
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
18
|
-
}
|
|
19
13
|
/**
|
|
20
14
|
* The constructor function initializes a HashMap object with an optional initial collection and
|
|
21
15
|
* options.
|
|
@@ -51,9 +45,15 @@ class HashMap extends base_1.IterableEntryBase {
|
|
|
51
45
|
this.setMany(rawCollection);
|
|
52
46
|
}
|
|
53
47
|
}
|
|
48
|
+
get toEntryFn() {
|
|
49
|
+
return this._toEntryFn;
|
|
50
|
+
}
|
|
54
51
|
get size() {
|
|
55
52
|
return this._size;
|
|
56
53
|
}
|
|
54
|
+
isEntry(rawElement) {
|
|
55
|
+
return Array.isArray(rawElement) && rawElement.length === 2;
|
|
56
|
+
}
|
|
57
57
|
isEmpty() {
|
|
58
58
|
return this.size === 0;
|
|
59
59
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.50.1",
|
|
4
4
|
"description": "Min Heap. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -132,6 +132,6 @@
|
|
|
132
132
|
"typescript": "^4.9.5"
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"data-structure-typed": "^1.
|
|
135
|
+
"data-structure-typed": "^1.50.0"
|
|
136
136
|
}
|
|
137
137
|
}
|
|
@@ -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) {
|