min-heap-typed 1.51.0 → 1.51.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/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/data-structures/binary-tree/bst.js +68 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/data-structures/binary-tree/rb-tree.js +8 -63
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/bst.ts +69 -34
- package/src/data-structures/binary-tree/rb-tree.ts +8 -74
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
|
@@ -194,7 +194,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
194
194
|
* decremented by 1 and
|
|
195
195
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
196
196
|
*/
|
|
197
|
-
delete(identifier, callback = this.
|
|
197
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
198
198
|
var _a;
|
|
199
199
|
const deletedResult = [];
|
|
200
200
|
if (!this.root)
|
|
@@ -108,7 +108,7 @@ export declare class AVLTree<K = any, V = any, NODE extends AVLTreeNode<K, V, NO
|
|
|
108
108
|
* `callback` function.
|
|
109
109
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
110
110
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
111
|
-
* default to the `
|
|
111
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
112
112
|
* parameter of type `NODE
|
|
113
113
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
114
114
|
*/
|
|
@@ -133,11 +133,11 @@ class AVLTree extends bst_1.BST {
|
|
|
133
133
|
* `callback` function.
|
|
134
134
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
135
135
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
136
|
-
* default to the `
|
|
136
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
137
137
|
* parameter of type `NODE
|
|
138
138
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
139
139
|
*/
|
|
140
|
-
delete(identifier, callback = this.
|
|
140
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
141
141
|
if (identifier instanceof AVLTreeNode)
|
|
142
142
|
callback = (node => node);
|
|
143
143
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -97,6 +97,12 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
97
97
|
* @returns The size of the object, which is a number.
|
|
98
98
|
*/
|
|
99
99
|
get size(): number;
|
|
100
|
+
protected _NIL: NODE;
|
|
101
|
+
/**
|
|
102
|
+
* The function returns the value of the _NIL property.
|
|
103
|
+
* @returns The method is returning the value of the `_NIL` property.
|
|
104
|
+
*/
|
|
105
|
+
get NIL(): NODE;
|
|
100
106
|
/**
|
|
101
107
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
102
108
|
* @param {K} key - The key for the new node.
|
|
@@ -140,19 +146,18 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
140
146
|
* itself if it is not a valid node key.
|
|
141
147
|
*/
|
|
142
148
|
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | null | undefined;
|
|
149
|
+
/**
|
|
150
|
+
* The function checks if a given node is a real node or null.
|
|
151
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
152
|
+
* @returns a boolean value.
|
|
153
|
+
*/
|
|
154
|
+
isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null;
|
|
143
155
|
/**
|
|
144
156
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
145
157
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
146
158
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the class NODE.
|
|
147
159
|
*/
|
|
148
160
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
149
|
-
/**
|
|
150
|
-
* The function checks if a given value is an entry in a binary tree node.
|
|
151
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
152
|
-
* two type parameters V and NODE, representing the value and node type respectively.
|
|
153
|
-
* @returns a boolean value.
|
|
154
|
-
*/
|
|
155
|
-
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V>;
|
|
156
161
|
/**
|
|
157
162
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
158
163
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -167,11 +172,12 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
167
172
|
*/
|
|
168
173
|
isNIL(node: KeyOrNodeOrEntry<K, V, NODE>): boolean;
|
|
169
174
|
/**
|
|
170
|
-
* The function checks if a given
|
|
171
|
-
* @param
|
|
175
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
176
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
177
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
172
178
|
* @returns a boolean value.
|
|
173
179
|
*/
|
|
174
|
-
|
|
180
|
+
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V>;
|
|
175
181
|
/**
|
|
176
182
|
* Time Complexity O(n)
|
|
177
183
|
* Space Complexity O(1)
|
|
@@ -576,7 +582,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
576
582
|
* 4. `middleIndex`: The index of the middle character
|
|
577
583
|
*/
|
|
578
584
|
protected _displayAux(node: NODE | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
|
|
579
|
-
protected
|
|
585
|
+
protected _DEFAULT_CALLBACK: (node: NODE | null | undefined) => K | undefined;
|
|
580
586
|
/**
|
|
581
587
|
* Swap the data of two nodes in the binary tree.
|
|
582
588
|
* @param {NODE} srcNode - The source node to swap.
|
|
@@ -107,7 +107,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
107
107
|
super();
|
|
108
108
|
this.iterationType = 'ITERATIVE';
|
|
109
109
|
this._extractor = (key) => (typeof key === 'number' ? key : Number(key));
|
|
110
|
-
this.
|
|
110
|
+
this._NIL = new BinaryTreeNode(NaN);
|
|
111
|
+
this._DEFAULT_CALLBACK = (node) => (node ? node.key : undefined);
|
|
111
112
|
if (options) {
|
|
112
113
|
const { iterationType, extractor } = options;
|
|
113
114
|
if (iterationType)
|
|
@@ -141,6 +142,13 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
141
142
|
get size() {
|
|
142
143
|
return this._size;
|
|
143
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* The function returns the value of the _NIL property.
|
|
147
|
+
* @returns The method is returning the value of the `_NIL` property.
|
|
148
|
+
*/
|
|
149
|
+
get NIL() {
|
|
150
|
+
return this._NIL;
|
|
151
|
+
}
|
|
144
152
|
/**
|
|
145
153
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
146
154
|
* @param {K} key - The key for the new node.
|
|
@@ -217,23 +225,31 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
217
225
|
* itself if it is not a valid node key.
|
|
218
226
|
*/
|
|
219
227
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
220
|
-
let res;
|
|
221
228
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
222
|
-
|
|
229
|
+
return keyOrNodeOrEntry;
|
|
223
230
|
}
|
|
224
231
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
225
232
|
if (keyOrNodeOrEntry[0] === null)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
233
|
+
return null;
|
|
234
|
+
if (keyOrNodeOrEntry[0] === undefined)
|
|
235
|
+
return;
|
|
236
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
229
237
|
}
|
|
230
238
|
else {
|
|
231
239
|
if (keyOrNodeOrEntry === null)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
240
|
+
return null;
|
|
241
|
+
if (keyOrNodeOrEntry === undefined)
|
|
242
|
+
return;
|
|
243
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
235
244
|
}
|
|
236
|
-
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The function checks if a given node is a real node or null.
|
|
248
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
249
|
+
* @returns a boolean value.
|
|
250
|
+
*/
|
|
251
|
+
isNodeOrNull(node) {
|
|
252
|
+
return this.isRealNode(node) || node === null;
|
|
237
253
|
}
|
|
238
254
|
/**
|
|
239
255
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
@@ -243,15 +259,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
243
259
|
isNode(keyOrNodeOrEntry) {
|
|
244
260
|
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
245
261
|
}
|
|
246
|
-
/**
|
|
247
|
-
* The function checks if a given value is an entry in a binary tree node.
|
|
248
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
249
|
-
* two type parameters V and NODE, representing the value and node type respectively.
|
|
250
|
-
* @returns a boolean value.
|
|
251
|
-
*/
|
|
252
|
-
isEntry(keyOrNodeOrEntry) {
|
|
253
|
-
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
254
|
-
}
|
|
255
262
|
/**
|
|
256
263
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
257
264
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -259,7 +266,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
259
266
|
* @returns a boolean value.
|
|
260
267
|
*/
|
|
261
268
|
isRealNode(node) {
|
|
262
|
-
|
|
269
|
+
if (!this.isNode(node))
|
|
270
|
+
return false;
|
|
271
|
+
return node !== this.NIL;
|
|
263
272
|
}
|
|
264
273
|
/**
|
|
265
274
|
* The function checks if a given node is a BinaryTreeNode instance and has a key value of NaN.
|
|
@@ -267,15 +276,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
267
276
|
* @returns a boolean value.
|
|
268
277
|
*/
|
|
269
278
|
isNIL(node) {
|
|
270
|
-
return node
|
|
279
|
+
return node === this.NIL;
|
|
271
280
|
}
|
|
272
281
|
/**
|
|
273
|
-
* The function checks if a given
|
|
274
|
-
* @param
|
|
282
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
283
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
284
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
275
285
|
* @returns a boolean value.
|
|
276
286
|
*/
|
|
277
|
-
|
|
278
|
-
return
|
|
287
|
+
isEntry(keyOrNodeOrEntry) {
|
|
288
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
279
289
|
}
|
|
280
290
|
/**
|
|
281
291
|
* Time Complexity O(n)
|
|
@@ -409,14 +419,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
409
419
|
* specific node based on its value or object.
|
|
410
420
|
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
411
421
|
* identifier of the node to be deleted. It is optional and has a default value of
|
|
412
|
-
* `this.
|
|
422
|
+
* `this._DEFAULT_CALLBACK`. The `callback` function should return the identifier of the node.
|
|
413
423
|
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
414
424
|
*/
|
|
415
|
-
delete(identifier, callback = this.
|
|
425
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
416
426
|
const deletedResult = [];
|
|
417
427
|
if (!this.root)
|
|
418
428
|
return deletedResult;
|
|
419
|
-
if ((!callback || callback === this.
|
|
429
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
420
430
|
callback = (node => node);
|
|
421
431
|
const curr = this.getNode(identifier, callback);
|
|
422
432
|
if (!curr)
|
|
@@ -475,7 +485,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
475
485
|
* specific value.
|
|
476
486
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
477
487
|
* input and returns a value of type `C`. It is used to determine if a node matches the given
|
|
478
|
-
* identifier. If no callback is provided, the `
|
|
488
|
+
* identifier. If no callback is provided, the `_DEFAULT_CALLBACK` function is used as the
|
|
479
489
|
* default
|
|
480
490
|
* @param [onlyOne=false] - A boolean value indicating whether to only return the first node that
|
|
481
491
|
* matches the identifier. If set to true, the function will stop iterating once it finds a matching
|
|
@@ -488,39 +498,39 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
488
498
|
* traverse the binary tree. It can have two possible values:
|
|
489
499
|
* @returns an array of nodes of type `NODE`.
|
|
490
500
|
*/
|
|
491
|
-
getNodes(identifier, callback = this.
|
|
492
|
-
if ((!callback || callback === this.
|
|
501
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
502
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
493
503
|
callback = (node => node);
|
|
494
504
|
beginRoot = this.ensureNode(beginRoot);
|
|
495
505
|
if (!beginRoot)
|
|
496
506
|
return [];
|
|
497
507
|
const ans = [];
|
|
498
508
|
if (iterationType === 'RECURSIVE') {
|
|
499
|
-
const
|
|
509
|
+
const dfs = (cur) => {
|
|
500
510
|
if (callback(cur) === identifier) {
|
|
501
511
|
ans.push(cur);
|
|
502
512
|
if (onlyOne)
|
|
503
513
|
return;
|
|
504
514
|
}
|
|
505
|
-
if (!cur.left && !cur.right)
|
|
515
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
506
516
|
return;
|
|
507
|
-
cur.left &&
|
|
508
|
-
cur.right &&
|
|
517
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
518
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
509
519
|
};
|
|
510
|
-
|
|
520
|
+
dfs(beginRoot);
|
|
511
521
|
}
|
|
512
522
|
else {
|
|
513
|
-
const
|
|
514
|
-
while (
|
|
515
|
-
const cur =
|
|
516
|
-
if (cur) {
|
|
523
|
+
const stack = [beginRoot];
|
|
524
|
+
while (stack.length > 0) {
|
|
525
|
+
const cur = stack.pop();
|
|
526
|
+
if (this.isRealNode(cur)) {
|
|
517
527
|
if (callback(cur) === identifier) {
|
|
518
528
|
ans.push(cur);
|
|
519
529
|
if (onlyOne)
|
|
520
530
|
return ans;
|
|
521
531
|
}
|
|
522
|
-
cur.left &&
|
|
523
|
-
cur.right &&
|
|
532
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
533
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
524
534
|
}
|
|
525
535
|
}
|
|
526
536
|
}
|
|
@@ -551,10 +561,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
551
561
|
* nodes are visited during the search.
|
|
552
562
|
* @returns a value of type `NODE | null | undefined`.
|
|
553
563
|
*/
|
|
554
|
-
getNode(identifier, callback = this.
|
|
564
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
555
565
|
var _a;
|
|
556
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
557
|
-
callback = (node => node);
|
|
558
566
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
559
567
|
}
|
|
560
568
|
/**
|
|
@@ -579,27 +587,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
579
587
|
if (!this.root)
|
|
580
588
|
return undefined;
|
|
581
589
|
if (iterationType === 'RECURSIVE') {
|
|
582
|
-
const
|
|
590
|
+
const dfs = (cur) => {
|
|
583
591
|
if (cur.key === key)
|
|
584
592
|
return cur;
|
|
585
593
|
if (!cur.left && !cur.right)
|
|
586
594
|
return;
|
|
587
595
|
if (cur.left)
|
|
588
|
-
return
|
|
596
|
+
return dfs(cur.left);
|
|
589
597
|
if (cur.right)
|
|
590
|
-
return
|
|
598
|
+
return dfs(cur.right);
|
|
591
599
|
};
|
|
592
|
-
return
|
|
600
|
+
return dfs(this.root);
|
|
593
601
|
}
|
|
594
602
|
else {
|
|
595
|
-
const
|
|
596
|
-
while (
|
|
597
|
-
const cur =
|
|
603
|
+
const stack = [this.root];
|
|
604
|
+
while (stack.length > 0) {
|
|
605
|
+
const cur = stack.pop();
|
|
598
606
|
if (cur) {
|
|
599
607
|
if (cur.key === key)
|
|
600
608
|
return cur;
|
|
601
|
-
cur.left &&
|
|
602
|
-
cur.right &&
|
|
609
|
+
cur.left && stack.push(cur.left);
|
|
610
|
+
cur.right && stack.push(cur.right);
|
|
603
611
|
}
|
|
604
612
|
}
|
|
605
613
|
}
|
|
@@ -630,10 +638,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
630
638
|
* @returns The value of the node with the given identifier is being returned. If the node is not
|
|
631
639
|
* found, `undefined` is returned.
|
|
632
640
|
*/
|
|
633
|
-
get(identifier, callback = this.
|
|
641
|
+
get(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
634
642
|
var _a, _b;
|
|
635
|
-
if ((!callback || callback === this._defaultOneParamCallback) && identifier instanceof BinaryTreeNode)
|
|
636
|
-
callback = (node => node);
|
|
637
643
|
return (_b = (_a = this.getNode(identifier, callback, beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : undefined;
|
|
638
644
|
}
|
|
639
645
|
/**
|
|
@@ -661,8 +667,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
661
667
|
* be performed in a pre-order, in-order, or post-order manner.
|
|
662
668
|
* @returns a boolean value.
|
|
663
669
|
*/
|
|
664
|
-
has(identifier, callback = this.
|
|
665
|
-
if ((!callback || callback === this.
|
|
670
|
+
has(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
671
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && identifier instanceof BinaryTreeNode)
|
|
666
672
|
callback = (node => node);
|
|
667
673
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
668
674
|
}
|
|
@@ -967,21 +973,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
967
973
|
if (!this.isRealNode(beginRoot))
|
|
968
974
|
return beginRoot;
|
|
969
975
|
if (iterationType === 'RECURSIVE') {
|
|
970
|
-
const
|
|
976
|
+
const dfs = (cur) => {
|
|
971
977
|
if (!this.isRealNode(cur.left))
|
|
972
978
|
return cur;
|
|
973
|
-
return
|
|
979
|
+
return dfs(cur.left);
|
|
974
980
|
};
|
|
975
|
-
return
|
|
981
|
+
return dfs(beginRoot);
|
|
976
982
|
}
|
|
977
983
|
else {
|
|
978
984
|
// Indirect implementation of iteration using tail recursion optimization
|
|
979
|
-
const
|
|
985
|
+
const dfs = (0, utils_1.trampoline)((cur) => {
|
|
980
986
|
if (!this.isRealNode(cur.left))
|
|
981
987
|
return cur;
|
|
982
|
-
return
|
|
988
|
+
return dfs.cont(cur.left);
|
|
983
989
|
});
|
|
984
|
-
return
|
|
990
|
+
return dfs(beginRoot);
|
|
985
991
|
}
|
|
986
992
|
}
|
|
987
993
|
/**
|
|
@@ -1011,21 +1017,21 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1011
1017
|
if (!beginRoot)
|
|
1012
1018
|
return beginRoot;
|
|
1013
1019
|
if (iterationType === 'RECURSIVE') {
|
|
1014
|
-
const
|
|
1020
|
+
const dfs = (cur) => {
|
|
1015
1021
|
if (!this.isRealNode(cur.right))
|
|
1016
1022
|
return cur;
|
|
1017
|
-
return
|
|
1023
|
+
return dfs(cur.right);
|
|
1018
1024
|
};
|
|
1019
|
-
return
|
|
1025
|
+
return dfs(beginRoot);
|
|
1020
1026
|
}
|
|
1021
1027
|
else {
|
|
1022
1028
|
// Indirect implementation of iteration using tail recursion optimization
|
|
1023
|
-
const
|
|
1029
|
+
const dfs = (0, utils_1.trampoline)((cur) => {
|
|
1024
1030
|
if (!this.isRealNode(cur.right))
|
|
1025
1031
|
return cur;
|
|
1026
|
-
return
|
|
1032
|
+
return dfs.cont(cur.right);
|
|
1027
1033
|
});
|
|
1028
|
-
return
|
|
1034
|
+
return dfs(beginRoot);
|
|
1029
1035
|
}
|
|
1030
1036
|
}
|
|
1031
1037
|
/**
|
|
@@ -1110,65 +1116,65 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1110
1116
|
* `false`, null or undefined
|
|
1111
1117
|
* @returns an array of values that are the return values of the callback function.
|
|
1112
1118
|
*/
|
|
1113
|
-
dfs(callback = this.
|
|
1119
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1114
1120
|
beginRoot = this.ensureNode(beginRoot);
|
|
1115
1121
|
if (!beginRoot)
|
|
1116
1122
|
return [];
|
|
1117
1123
|
const ans = [];
|
|
1118
1124
|
if (iterationType === 'RECURSIVE') {
|
|
1119
|
-
const
|
|
1125
|
+
const dfs = (node) => {
|
|
1120
1126
|
switch (pattern) {
|
|
1121
1127
|
case 'IN':
|
|
1122
1128
|
if (includeNull) {
|
|
1123
1129
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1124
|
-
|
|
1130
|
+
dfs(node.left);
|
|
1125
1131
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1126
1132
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1127
|
-
|
|
1133
|
+
dfs(node.right);
|
|
1128
1134
|
}
|
|
1129
1135
|
else {
|
|
1130
1136
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1131
|
-
|
|
1137
|
+
dfs(node.left);
|
|
1132
1138
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1133
1139
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1134
|
-
|
|
1140
|
+
dfs(node.right);
|
|
1135
1141
|
}
|
|
1136
1142
|
break;
|
|
1137
1143
|
case 'PRE':
|
|
1138
1144
|
if (includeNull) {
|
|
1139
1145
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1140
1146
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1141
|
-
|
|
1147
|
+
dfs(node.left);
|
|
1142
1148
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1143
|
-
|
|
1149
|
+
dfs(node.right);
|
|
1144
1150
|
}
|
|
1145
1151
|
else {
|
|
1146
1152
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1147
1153
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1148
|
-
|
|
1154
|
+
dfs(node.left);
|
|
1149
1155
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1150
|
-
|
|
1156
|
+
dfs(node.right);
|
|
1151
1157
|
}
|
|
1152
1158
|
break;
|
|
1153
1159
|
case 'POST':
|
|
1154
1160
|
if (includeNull) {
|
|
1155
1161
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1156
|
-
|
|
1162
|
+
dfs(node.left);
|
|
1157
1163
|
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1158
|
-
|
|
1164
|
+
dfs(node.right);
|
|
1159
1165
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1160
1166
|
}
|
|
1161
1167
|
else {
|
|
1162
1168
|
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1163
|
-
|
|
1169
|
+
dfs(node.left);
|
|
1164
1170
|
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1165
|
-
|
|
1171
|
+
dfs(node.right);
|
|
1166
1172
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1167
1173
|
}
|
|
1168
1174
|
break;
|
|
1169
1175
|
}
|
|
1170
1176
|
};
|
|
1171
|
-
|
|
1177
|
+
dfs(beginRoot);
|
|
1172
1178
|
}
|
|
1173
1179
|
else {
|
|
1174
1180
|
// 0: visit, 1: print
|
|
@@ -1241,14 +1247,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1241
1247
|
* @returns an array of values that are the result of invoking the callback function on each node in
|
|
1242
1248
|
* the breadth-first traversal of a binary tree.
|
|
1243
1249
|
*/
|
|
1244
|
-
bfs(callback = this.
|
|
1250
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1245
1251
|
beginRoot = this.ensureNode(beginRoot);
|
|
1246
1252
|
if (!beginRoot)
|
|
1247
1253
|
return [];
|
|
1248
1254
|
const ans = [];
|
|
1249
1255
|
if (iterationType === 'RECURSIVE') {
|
|
1250
1256
|
const queue = new queue_1.Queue([beginRoot]);
|
|
1251
|
-
const
|
|
1257
|
+
const dfs = (level) => {
|
|
1252
1258
|
if (queue.size === 0)
|
|
1253
1259
|
return;
|
|
1254
1260
|
const current = queue.shift();
|
|
@@ -1265,9 +1271,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1265
1271
|
if (this.isRealNode(current.right))
|
|
1266
1272
|
queue.push(current.right);
|
|
1267
1273
|
}
|
|
1268
|
-
|
|
1274
|
+
dfs(level + 1);
|
|
1269
1275
|
};
|
|
1270
|
-
|
|
1276
|
+
dfs(0);
|
|
1271
1277
|
}
|
|
1272
1278
|
else {
|
|
1273
1279
|
const queue = new queue_1.Queue([beginRoot]);
|
|
@@ -1318,7 +1324,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1318
1324
|
* be excluded
|
|
1319
1325
|
* @returns The function `listLevels` returns a two-dimensional array of type `ReturnType<C>[][]`.
|
|
1320
1326
|
*/
|
|
1321
|
-
listLevels(callback = this.
|
|
1327
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType, includeNull = false) {
|
|
1322
1328
|
beginRoot = this.ensureNode(beginRoot);
|
|
1323
1329
|
const levelsNodes = [];
|
|
1324
1330
|
if (!beginRoot)
|
|
@@ -1390,7 +1396,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1390
1396
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1391
1397
|
* by the return type of the `callback` function.
|
|
1392
1398
|
*/
|
|
1393
|
-
morris(callback = this.
|
|
1399
|
+
morris(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root) {
|
|
1394
1400
|
beginRoot = this.ensureNode(beginRoot);
|
|
1395
1401
|
if (beginRoot === null)
|
|
1396
1402
|
return [];
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
|
-
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
9
|
+
import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNodeNested<K, V>> extends BinaryTreeNode<K, V, NODE> {
|
|
@@ -209,6 +209,32 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
209
209
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
210
210
|
*/
|
|
211
211
|
getNodes<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE[];
|
|
212
|
+
/**
|
|
213
|
+
* Time Complexity: O(log n)
|
|
214
|
+
* Space Complexity: O(1)
|
|
215
|
+
*/
|
|
216
|
+
/**
|
|
217
|
+
* Time Complexity: O(log n)
|
|
218
|
+
* Space Complexity: O(1)
|
|
219
|
+
*
|
|
220
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
221
|
+
* callback function.
|
|
222
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
223
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
224
|
+
* with the type of nodes in the tree.
|
|
225
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
226
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
227
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
228
|
+
* `identifier` parameter.
|
|
229
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
230
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
231
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
232
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
233
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
234
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
235
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
236
|
+
*/
|
|
237
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
212
238
|
/**
|
|
213
239
|
* Time complexity: O(n)
|
|
214
240
|
* Space complexity: O(n)
|