min-heap-typed 1.50.0 → 1.50.2
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/base/iterable-base.d.ts +114 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/data-structures/binary-tree/bst.js +30 -71
- package/dist/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/data-structures/graph/directed-graph.js +26 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/data-structures/graph/undirected-graph.js +25 -0
- package/dist/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/data-structures/hash/hash-map.js +160 -25
- package/dist/data-structures/heap/heap.d.ts +66 -6
- package/dist/data-structures/heap/heap.js +66 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/queue/deque.d.ts +49 -19
- package/dist/data-structures/queue/deque.js +101 -47
- package/dist/data-structures/queue/queue.d.ts +39 -5
- package/dist/data-structures/queue/queue.js +47 -5
- package/dist/data-structures/stack/stack.d.ts +16 -0
- package/dist/data-structures/stack/stack.js +22 -0
- package/dist/data-structures/trie/trie.d.ts +38 -1
- package/dist/data-structures/trie/trie.js +41 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -18,12 +18,12 @@ import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
import { Queue } from '../queue';
|
|
20
20
|
|
|
21
|
-
export class BSTNode<K = any, V = any,
|
|
21
|
+
export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<
|
|
22
22
|
K,
|
|
23
23
|
V,
|
|
24
|
-
|
|
24
|
+
NODE
|
|
25
25
|
> {
|
|
26
|
-
override parent?:
|
|
26
|
+
override parent?: NODE;
|
|
27
27
|
|
|
28
28
|
constructor(key: K, value?: V) {
|
|
29
29
|
super(key, value);
|
|
@@ -32,42 +32,28 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
|
|
|
32
32
|
this._right = undefined;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
protected override _left?:
|
|
35
|
+
protected override _left?: NODE;
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
* Get the left child node.
|
|
39
|
-
*/
|
|
40
|
-
override get left(): N | undefined {
|
|
37
|
+
override get left(): NODE | undefined {
|
|
41
38
|
return this._left;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
* Set the left child node.
|
|
46
|
-
* @param {N | undefined} v - The left child node.
|
|
47
|
-
*/
|
|
48
|
-
override set left(v: N | undefined) {
|
|
41
|
+
override set left(v: NODE | undefined) {
|
|
49
42
|
if (v) {
|
|
50
|
-
v.parent = this as unknown as
|
|
43
|
+
v.parent = this as unknown as NODE;
|
|
51
44
|
}
|
|
52
45
|
this._left = v;
|
|
53
46
|
}
|
|
54
47
|
|
|
55
|
-
protected override _right?:
|
|
48
|
+
protected override _right?: NODE;
|
|
56
49
|
|
|
57
|
-
|
|
58
|
-
* Get the right child node.
|
|
59
|
-
*/
|
|
60
|
-
override get right(): N | undefined {
|
|
50
|
+
override get right(): NODE | undefined {
|
|
61
51
|
return this._right;
|
|
62
52
|
}
|
|
63
53
|
|
|
64
|
-
|
|
65
|
-
* Set the right child node.
|
|
66
|
-
* @param {N | undefined} v - The right child node.
|
|
67
|
-
*/
|
|
68
|
-
override set right(v: N | undefined) {
|
|
54
|
+
override set right(v: NODE | undefined) {
|
|
69
55
|
if (v) {
|
|
70
|
-
v.parent = this as unknown as
|
|
56
|
+
v.parent = this as unknown as NODE;
|
|
71
57
|
}
|
|
72
58
|
this._right = v;
|
|
73
59
|
}
|
|
@@ -85,11 +71,11 @@ export class BSTNode<K = any, V = any, N extends BSTNode<K, V, N> = BSTNodeNeste
|
|
|
85
71
|
export class BST<
|
|
86
72
|
K = any,
|
|
87
73
|
V = any,
|
|
88
|
-
|
|
89
|
-
TREE extends BST<K, V,
|
|
74
|
+
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
75
|
+
TREE extends BST<K, V, NODE, TREE> = BST<K, V, NODE, BSTNested<K, V, NODE>>
|
|
90
76
|
>
|
|
91
|
-
extends BinaryTree<K, V,
|
|
92
|
-
implements IBinaryTree<K, V,
|
|
77
|
+
extends BinaryTree<K, V, NODE, TREE>
|
|
78
|
+
implements IBinaryTree<K, V, NODE, TREE> {
|
|
93
79
|
/**
|
|
94
80
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
95
81
|
* the tree with optional keysOrNodesOrEntries and options.
|
|
@@ -98,7 +84,7 @@ export class BST<
|
|
|
98
84
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
99
85
|
* configuration options for the binary search tree. It can have the following properties:
|
|
100
86
|
*/
|
|
101
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
87
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: BSTOptions<K>) {
|
|
102
88
|
super([], options);
|
|
103
89
|
|
|
104
90
|
if (options) {
|
|
@@ -111,9 +97,9 @@ export class BST<
|
|
|
111
97
|
if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
|
|
112
98
|
}
|
|
113
99
|
|
|
114
|
-
protected override _root?:
|
|
100
|
+
protected override _root?: NODE;
|
|
115
101
|
|
|
116
|
-
override get root():
|
|
102
|
+
override get root(): NODE | undefined {
|
|
117
103
|
return this._root;
|
|
118
104
|
}
|
|
119
105
|
|
|
@@ -131,8 +117,8 @@ export class BST<
|
|
|
131
117
|
* represents the value associated with the node in a binary search tree.
|
|
132
118
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
133
119
|
*/
|
|
134
|
-
override createNode(key: K, value?: V):
|
|
135
|
-
return new BSTNode<K, V,
|
|
120
|
+
override createNode(key: K, value?: V): NODE {
|
|
121
|
+
return new BSTNode<K, V, NODE>(key, value) as NODE;
|
|
136
122
|
}
|
|
137
123
|
|
|
138
124
|
/**
|
|
@@ -143,7 +129,7 @@ export class BST<
|
|
|
143
129
|
* @returns a new instance of the BST class with the specified options.
|
|
144
130
|
*/
|
|
145
131
|
override createTree(options?: Partial<BSTOptions<K>>): TREE {
|
|
146
|
-
return new BST<K, V,
|
|
132
|
+
return new BST<K, V, NODE, TREE>([], {
|
|
147
133
|
iterationType: this.iterationType,
|
|
148
134
|
variant: this.variant,
|
|
149
135
|
...options
|
|
@@ -151,15 +137,15 @@ export class BST<
|
|
|
151
137
|
}
|
|
152
138
|
|
|
153
139
|
/**
|
|
154
|
-
* The function `
|
|
140
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
155
141
|
* otherwise it returns undefined.
|
|
156
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
142
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
157
143
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
158
|
-
* `
|
|
159
|
-
* @returns a node of type
|
|
144
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
145
|
+
* @returns a node of type NODE or undefined.
|
|
160
146
|
*/
|
|
161
|
-
override
|
|
162
|
-
let node:
|
|
147
|
+
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
148
|
+
let node: NODE | undefined;
|
|
163
149
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
164
150
|
return;
|
|
165
151
|
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
@@ -182,7 +168,6 @@ export class BST<
|
|
|
182
168
|
/**
|
|
183
169
|
* Time Complexity: O(log n)
|
|
184
170
|
* Space Complexity: O(log n)
|
|
185
|
-
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
186
171
|
*/
|
|
187
172
|
|
|
188
173
|
/**
|
|
@@ -191,17 +176,17 @@ export class BST<
|
|
|
191
176
|
*
|
|
192
177
|
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
193
178
|
* otherwise it returns the key itself.
|
|
194
|
-
* @param {K |
|
|
179
|
+
* @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
|
|
195
180
|
* `undefined`.
|
|
196
181
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
197
182
|
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
198
|
-
* @returns either a node object (
|
|
183
|
+
* @returns either a node object (NODE) or undefined.
|
|
199
184
|
*/
|
|
200
185
|
override ensureNode(
|
|
201
|
-
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
186
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
202
187
|
iterationType = IterationType.ITERATIVE
|
|
203
|
-
):
|
|
204
|
-
let res:
|
|
188
|
+
): NODE | undefined {
|
|
189
|
+
let res: NODE | undefined;
|
|
205
190
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
206
191
|
res = keyOrNodeOrEntry;
|
|
207
192
|
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
@@ -214,17 +199,16 @@ export class BST<
|
|
|
214
199
|
|
|
215
200
|
/**
|
|
216
201
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
217
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,
|
|
202
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
218
203
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
219
204
|
*/
|
|
220
|
-
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
205
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
221
206
|
return keyOrNodeOrEntry instanceof BSTNode;
|
|
222
207
|
}
|
|
223
208
|
|
|
224
209
|
/**
|
|
225
210
|
* Time Complexity: O(log n)
|
|
226
211
|
* Space Complexity: O(1)
|
|
227
|
-
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
228
212
|
*/
|
|
229
213
|
|
|
230
214
|
/**
|
|
@@ -239,8 +223,8 @@ export class BST<
|
|
|
239
223
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
240
224
|
* node was not added.
|
|
241
225
|
*/
|
|
242
|
-
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
243
|
-
const newNode = this.
|
|
226
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
227
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
244
228
|
if (newNode === undefined) return false;
|
|
245
229
|
|
|
246
230
|
if (this.root === undefined) {
|
|
@@ -266,7 +250,6 @@ export class BST<
|
|
|
266
250
|
} else if (this._compare(current.key, newNode.key) === CP.gt) {
|
|
267
251
|
if (current.left === undefined) {
|
|
268
252
|
current.left = newNode;
|
|
269
|
-
newNode.parent = current;
|
|
270
253
|
this._size++;
|
|
271
254
|
return true;
|
|
272
255
|
}
|
|
@@ -274,7 +257,6 @@ export class BST<
|
|
|
274
257
|
} else {
|
|
275
258
|
if (current.right === undefined) {
|
|
276
259
|
current.right = newNode;
|
|
277
|
-
newNode.parent = current;
|
|
278
260
|
this._size++;
|
|
279
261
|
return true;
|
|
280
262
|
}
|
|
@@ -287,13 +269,12 @@ export class BST<
|
|
|
287
269
|
|
|
288
270
|
/**
|
|
289
271
|
* Time Complexity: O(k log n)
|
|
290
|
-
* Space Complexity: O(k)
|
|
291
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
272
|
+
* Space Complexity: O(k + log n)
|
|
292
273
|
*/
|
|
293
274
|
|
|
294
275
|
/**
|
|
295
276
|
* Time Complexity: O(k log n)
|
|
296
|
-
* Space Complexity: O(k)
|
|
277
|
+
* Space Complexity: O(k + log n)
|
|
297
278
|
*
|
|
298
279
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
299
280
|
* balancing the tree after each addition.
|
|
@@ -309,10 +290,10 @@ export class BST<
|
|
|
309
290
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
310
291
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
311
292
|
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
312
|
-
* @returns The function `addMany` returns an array of nodes (`
|
|
293
|
+
* @returns The function `addMany` returns an array of nodes (`NODE`) or `undefined` values.
|
|
313
294
|
*/
|
|
314
295
|
override addMany(
|
|
315
|
-
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
296
|
+
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>>,
|
|
316
297
|
values?: Iterable<V | undefined>,
|
|
317
298
|
isBalanceAdd = true,
|
|
318
299
|
iterationType = this.iterationType
|
|
@@ -334,9 +315,9 @@ export class BST<
|
|
|
334
315
|
return inserted;
|
|
335
316
|
}
|
|
336
317
|
|
|
337
|
-
const realBTNExemplars: BTNodePureExemplar<K, V,
|
|
318
|
+
const realBTNExemplars: BTNodePureExemplar<K, V, NODE>[] = [];
|
|
338
319
|
|
|
339
|
-
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V,
|
|
320
|
+
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, NODE>): kve is BTNodePureExemplar<K, V, NODE> => {
|
|
340
321
|
if (kve === undefined || kve === null) return false;
|
|
341
322
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
342
323
|
};
|
|
@@ -345,7 +326,7 @@ export class BST<
|
|
|
345
326
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
346
327
|
}
|
|
347
328
|
|
|
348
|
-
let sorted: BTNodePureExemplar<K, V,
|
|
329
|
+
let sorted: BTNodePureExemplar<K, V, NODE>[] = [];
|
|
349
330
|
|
|
350
331
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
351
332
|
let aR: number, bR: number;
|
|
@@ -360,7 +341,7 @@ export class BST<
|
|
|
360
341
|
return aR - bR;
|
|
361
342
|
});
|
|
362
343
|
|
|
363
|
-
const _dfs = (arr: BTNodePureExemplar<K, V,
|
|
344
|
+
const _dfs = (arr: BTNodePureExemplar<K, V, NODE>[]) => {
|
|
364
345
|
if (arr.length === 0) return;
|
|
365
346
|
|
|
366
347
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -413,13 +394,13 @@ export class BST<
|
|
|
413
394
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
414
395
|
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
415
396
|
* values:
|
|
416
|
-
* @returns The function `getNodeByKey` returns a node (`
|
|
397
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
417
398
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
418
399
|
*/
|
|
419
|
-
override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE):
|
|
400
|
+
override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
|
|
420
401
|
if (!this.root) return undefined;
|
|
421
402
|
if (iterationType === IterationType.RECURSIVE) {
|
|
422
|
-
const _dfs = (cur:
|
|
403
|
+
const _dfs = (cur: NODE): NODE | undefined => {
|
|
423
404
|
if (cur.key === key) return cur;
|
|
424
405
|
if (!cur.left && !cur.right) return;
|
|
425
406
|
|
|
@@ -429,7 +410,7 @@ export class BST<
|
|
|
429
410
|
|
|
430
411
|
return _dfs(this.root);
|
|
431
412
|
} else {
|
|
432
|
-
const queue = new Queue<
|
|
413
|
+
const queue = new Queue<NODE>([this.root]);
|
|
433
414
|
while (queue.size > 0) {
|
|
434
415
|
const cur = queue.shift();
|
|
435
416
|
if (cur) {
|
|
@@ -443,46 +424,45 @@ export class BST<
|
|
|
443
424
|
|
|
444
425
|
/**
|
|
445
426
|
* Time Complexity: O(log n)
|
|
446
|
-
* Space Complexity: O(log n)
|
|
447
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
427
|
+
* Space Complexity: O(k + log n)
|
|
448
428
|
* /
|
|
449
429
|
|
|
450
430
|
/**
|
|
451
431
|
* Time Complexity: O(log n)
|
|
452
|
-
* Space Complexity: O(log n)
|
|
432
|
+
* Space Complexity: O(k + log n)
|
|
453
433
|
*
|
|
454
434
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
455
435
|
* recursive or iterative approach.
|
|
456
436
|
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value that you
|
|
457
437
|
* want to search for in the nodes of the binary tree. It can be of any type that is returned by the
|
|
458
438
|
* callback function `C`.
|
|
459
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
439
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as its
|
|
460
440
|
* argument and returns a value of type `ReturnType<C>`. The `C` type parameter represents a callback
|
|
461
|
-
* function type that extends the `BTNCallback<
|
|
441
|
+
* function type that extends the `BTNCallback<NODE>` type. The `BTNCallback<NODE>` type is
|
|
462
442
|
* @param [onlyOne=false] - A boolean flag indicating whether to stop searching after finding the
|
|
463
443
|
* first node that matches the identifier. If set to true, the function will return an array
|
|
464
444
|
* containing only the first matching node. If set to false (default), the function will continue
|
|
465
445
|
* searching for all nodes that match the identifier and return an array containing
|
|
466
|
-
* @param {K |
|
|
446
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter represents the starting node
|
|
467
447
|
* for the traversal. It can be either a key value or a node object. If it is undefined, the
|
|
468
448
|
* traversal will start from the root of the tree.
|
|
469
449
|
* @param iterationType - The `iterationType` parameter determines the type of iteration to be
|
|
470
450
|
* performed on the binary tree. It can have two possible values:
|
|
471
|
-
* @returns The method returns an array of nodes (`
|
|
451
|
+
* @returns The method returns an array of nodes (`NODE[]`).
|
|
472
452
|
*/
|
|
473
|
-
override getNodes<C extends BTNCallback<
|
|
453
|
+
override getNodes<C extends BTNCallback<NODE>>(
|
|
474
454
|
identifier: ReturnType<C> | undefined,
|
|
475
455
|
callback: C = this._defaultOneParamCallback as C,
|
|
476
456
|
onlyOne = false,
|
|
477
|
-
beginRoot: KeyOrNodeOrEntry<K, V,
|
|
457
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
478
458
|
iterationType = this.iterationType
|
|
479
|
-
):
|
|
459
|
+
): NODE[] {
|
|
480
460
|
beginRoot = this.ensureNode(beginRoot);
|
|
481
461
|
if (!beginRoot) return [];
|
|
482
|
-
const ans:
|
|
462
|
+
const ans: NODE[] = [];
|
|
483
463
|
|
|
484
464
|
if (iterationType === IterationType.RECURSIVE) {
|
|
485
|
-
const _traverse = (cur:
|
|
465
|
+
const _traverse = (cur: NODE) => {
|
|
486
466
|
const callbackResult = callback(cur);
|
|
487
467
|
if (callbackResult === identifier) {
|
|
488
468
|
ans.push(cur);
|
|
@@ -502,7 +482,7 @@ export class BST<
|
|
|
502
482
|
|
|
503
483
|
_traverse(beginRoot);
|
|
504
484
|
} else {
|
|
505
|
-
const queue = new Queue<
|
|
485
|
+
const queue = new Queue<NODE>([beginRoot]);
|
|
506
486
|
while (queue.size > 0) {
|
|
507
487
|
const cur = queue.shift();
|
|
508
488
|
if (cur) {
|
|
@@ -526,26 +506,6 @@ export class BST<
|
|
|
526
506
|
return ans;
|
|
527
507
|
}
|
|
528
508
|
|
|
529
|
-
// /**
|
|
530
|
-
// * The function overrides the subTreeTraverse method and returns the result of calling the super
|
|
531
|
-
// * method with the provided arguments.
|
|
532
|
-
// * @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
533
|
-
// * the subtree traversal. It should accept a single parameter of type `N`, which represents a node in
|
|
534
|
-
// * the tree. The return type of the callback function can be any type.
|
|
535
|
-
// * @param beginRoot - The `beginRoot` parameter is the starting point for traversing the subtree. It
|
|
536
|
-
// * can be either a key, a node, or an entry.
|
|
537
|
-
// * @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
538
|
-
// * be performed during the traversal of the subtree. It can have one of the following values:
|
|
539
|
-
// * @returns The method is returning an array of the return type of the callback function.
|
|
540
|
-
// */
|
|
541
|
-
// override subTreeTraverse<C extends BTNCallback<N>>(
|
|
542
|
-
// callback: C = this._defaultOneParamCallback as C,
|
|
543
|
-
// beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
544
|
-
// iterationType = this.iterationType
|
|
545
|
-
// ): ReturnType<C>[] {
|
|
546
|
-
// return super.subTreeTraverse(callback, beginRoot, iterationType, false);
|
|
547
|
-
// }
|
|
548
|
-
|
|
549
509
|
/**
|
|
550
510
|
* Time complexity: O(n)
|
|
551
511
|
* Space complexity: O(n)
|
|
@@ -570,10 +530,10 @@ export class BST<
|
|
|
570
530
|
* following values:
|
|
571
531
|
* @returns The method is returning an array of the return type of the callback function.
|
|
572
532
|
*/
|
|
573
|
-
override dfs<C extends BTNCallback<
|
|
533
|
+
override dfs<C extends BTNCallback<NODE>>(
|
|
574
534
|
callback: C = this._defaultOneParamCallback as C,
|
|
575
535
|
pattern: DFSOrderPattern = 'in',
|
|
576
|
-
beginRoot: KeyOrNodeOrEntry<K, V,
|
|
536
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
577
537
|
iterationType: IterationType = IterationType.ITERATIVE
|
|
578
538
|
): ReturnType<C>[] {
|
|
579
539
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
@@ -601,9 +561,9 @@ export class BST<
|
|
|
601
561
|
* nodes are visited.
|
|
602
562
|
* @returns The method is returning an array of the return type of the callback function.
|
|
603
563
|
*/
|
|
604
|
-
override bfs<C extends BTNCallback<
|
|
564
|
+
override bfs<C extends BTNCallback<NODE>>(
|
|
605
565
|
callback: C = this._defaultOneParamCallback as C,
|
|
606
|
-
beginRoot: KeyOrNodeOrEntry<K, V,
|
|
566
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
607
567
|
iterationType = this.iterationType
|
|
608
568
|
): ReturnType<C>[] {
|
|
609
569
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
@@ -621,7 +581,7 @@ export class BST<
|
|
|
621
581
|
* The function overrides the listLevels method and returns an array of arrays containing the return
|
|
622
582
|
* type of the callback function for each level of the tree.
|
|
623
583
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
624
|
-
* `BTNCallback<
|
|
584
|
+
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the tree
|
|
625
585
|
* during the level listing process.
|
|
626
586
|
* @param beginRoot - The `beginRoot` parameter is used to specify the starting point for listing the
|
|
627
587
|
* levels of a binary tree. It can be either a key, a node, or an entry in the binary tree. If not
|
|
@@ -632,34 +592,33 @@ export class BST<
|
|
|
632
592
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
633
593
|
* function.
|
|
634
594
|
*/
|
|
635
|
-
override listLevels<C extends BTNCallback<
|
|
595
|
+
override listLevels<C extends BTNCallback<NODE>>(
|
|
636
596
|
callback: C = this._defaultOneParamCallback as C,
|
|
637
|
-
beginRoot: KeyOrNodeOrEntry<K, V,
|
|
597
|
+
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
638
598
|
iterationType = this.iterationType
|
|
639
599
|
): ReturnType<C>[][] {
|
|
640
600
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
641
601
|
}
|
|
642
602
|
|
|
643
603
|
/**
|
|
644
|
-
* Time Complexity: O(
|
|
645
|
-
* Space Complexity: O(
|
|
646
|
-
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
604
|
+
* Time Complexity: O(log n)
|
|
605
|
+
* Space Complexity: O(1)
|
|
647
606
|
*/
|
|
648
607
|
|
|
649
608
|
/**
|
|
650
|
-
* Time Complexity: O(
|
|
651
|
-
* Space Complexity: O(
|
|
609
|
+
* Time Complexity: O(log n)
|
|
610
|
+
* Space Complexity: O(1)
|
|
652
611
|
*
|
|
653
612
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
654
613
|
* leftmost node if the comparison result is greater than.
|
|
655
|
-
* @param {K |
|
|
656
|
-
* type `K`, `
|
|
614
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
615
|
+
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
657
616
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
658
617
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
659
618
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
660
619
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
661
620
|
*/
|
|
662
|
-
lastKey(beginRoot: KeyOrNodeOrEntry<K, V,
|
|
621
|
+
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root): K | undefined {
|
|
663
622
|
let current = this.ensureNode(beginRoot);
|
|
664
623
|
if (!current) return undefined;
|
|
665
624
|
|
|
@@ -680,7 +639,6 @@ export class BST<
|
|
|
680
639
|
/**
|
|
681
640
|
* Time Complexity: O(log n)
|
|
682
641
|
* Space Complexity: O(log n)
|
|
683
|
-
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
684
642
|
*/
|
|
685
643
|
|
|
686
644
|
/**
|
|
@@ -691,12 +649,12 @@ export class BST<
|
|
|
691
649
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
692
650
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
693
651
|
* that satisfies the condition specified by the `lesserOrGreater` parameter. It takes a single
|
|
694
|
-
* parameter of type `
|
|
652
|
+
* parameter of type `NODE` (the node type) and returns a value of any type.
|
|
695
653
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
696
654
|
* traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It is of type
|
|
697
655
|
* `CP`, which is a custom type representing the comparison operator. The possible values for
|
|
698
656
|
* `lesserOrGreater` are
|
|
699
|
-
* @param {K |
|
|
657
|
+
* @param {K | NODE | undefined} targetNode - The `targetNode` parameter represents the node in the
|
|
700
658
|
* binary tree that you want to traverse from. It can be specified either by its key, by the node
|
|
701
659
|
* object itself, or it can be left undefined to start the traversal from the root of the tree.
|
|
702
660
|
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
@@ -704,21 +662,21 @@ export class BST<
|
|
|
704
662
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
705
663
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
706
664
|
*/
|
|
707
|
-
lesserOrGreaterTraverse<C extends BTNCallback<
|
|
665
|
+
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
708
666
|
callback: C = this._defaultOneParamCallback as C,
|
|
709
667
|
lesserOrGreater: CP = CP.lt,
|
|
710
|
-
targetNode: KeyOrNodeOrEntry<K, V,
|
|
668
|
+
targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
711
669
|
iterationType = this.iterationType
|
|
712
670
|
): ReturnType<C>[] {
|
|
713
671
|
targetNode = this.ensureNode(targetNode);
|
|
714
|
-
const ans: ReturnType<BTNCallback<
|
|
672
|
+
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
715
673
|
if (!targetNode) return ans;
|
|
716
674
|
if (!this.root) return ans;
|
|
717
675
|
|
|
718
676
|
const targetKey = targetNode.key;
|
|
719
677
|
|
|
720
678
|
if (iterationType === IterationType.RECURSIVE) {
|
|
721
|
-
const _traverse = (cur:
|
|
679
|
+
const _traverse = (cur: NODE) => {
|
|
722
680
|
const compared = this._compare(cur.key, targetKey);
|
|
723
681
|
if (compared === lesserOrGreater) ans.push(callback(cur));
|
|
724
682
|
|
|
@@ -730,7 +688,7 @@ export class BST<
|
|
|
730
688
|
_traverse(this.root);
|
|
731
689
|
return ans;
|
|
732
690
|
} else {
|
|
733
|
-
const queue = new Queue<
|
|
691
|
+
const queue = new Queue<NODE>([this.root]);
|
|
734
692
|
while (queue.size > 0) {
|
|
735
693
|
const cur = queue.shift();
|
|
736
694
|
if (cur) {
|
|
@@ -809,8 +767,8 @@ export class BST<
|
|
|
809
767
|
*/
|
|
810
768
|
|
|
811
769
|
/**
|
|
812
|
-
* Time Complexity: O(n)
|
|
813
|
-
* Space Complexity: O(n)
|
|
770
|
+
* Time Complexity: O(n)
|
|
771
|
+
* Space Complexity: O(log n)
|
|
814
772
|
*/
|
|
815
773
|
|
|
816
774
|
/**
|
|
@@ -828,7 +786,7 @@ export class BST<
|
|
|
828
786
|
let balanced = true;
|
|
829
787
|
|
|
830
788
|
if (iterationType === IterationType.RECURSIVE) {
|
|
831
|
-
const _height = (cur:
|
|
789
|
+
const _height = (cur: NODE | undefined): number => {
|
|
832
790
|
if (!cur) return 0;
|
|
833
791
|
const leftHeight = _height(cur.left),
|
|
834
792
|
rightHeight = _height(cur.right);
|
|
@@ -837,10 +795,10 @@ export class BST<
|
|
|
837
795
|
};
|
|
838
796
|
_height(this.root);
|
|
839
797
|
} else {
|
|
840
|
-
const stack:
|
|
841
|
-
let node:
|
|
842
|
-
last:
|
|
843
|
-
const depths: Map<
|
|
798
|
+
const stack: NODE[] = [];
|
|
799
|
+
let node: NODE | undefined = this.root,
|
|
800
|
+
last: NODE | undefined = undefined;
|
|
801
|
+
const depths: Map<NODE, number> = new Map();
|
|
844
802
|
|
|
845
803
|
while (stack.length > 0 || node) {
|
|
846
804
|
if (node) {
|
|
@@ -866,7 +824,7 @@ export class BST<
|
|
|
866
824
|
return balanced;
|
|
867
825
|
}
|
|
868
826
|
|
|
869
|
-
protected _setRoot(v:
|
|
827
|
+
protected _setRoot(v: NODE | undefined) {
|
|
870
828
|
if (v) {
|
|
871
829
|
v.parent = undefined;
|
|
872
830
|
}
|