bst-typed 1.53.6 → 1.53.8
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/README.md +61 -0
- package/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +28 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +9 -12
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +55 -20
- package/dist/data-structures/binary-tree/binary-tree.js +102 -68
- package/dist/data-structures/binary-tree/bst.d.ts +131 -37
- package/dist/data-structures/binary-tree/bst.js +222 -69
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +53 -0
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +56 -3
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +7 -7
- package/dist/data-structures/hash/hash-map.d.ts +30 -0
- package/dist/data-structures/hash/hash-map.js +30 -0
- package/dist/data-structures/heap/heap.d.ts +26 -9
- package/dist/data-structures/heap/heap.js +37 -17
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +54 -9
- package/dist/data-structures/linked-list/doubly-linked-list.js +80 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +35 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +55 -11
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +111 -6
- package/dist/data-structures/trie/trie.js +123 -14
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +10 -6
- package/dist/utils/utils.js +4 -2
- package/package.json +2 -2
- package/src/common/index.ts +25 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +9 -11
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +110 -66
- package/src/data-structures/binary-tree/bst.ts +232 -72
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +56 -3
- package/src/data-structures/binary-tree/tree-multi-map.ts +6 -6
- package/src/data-structures/hash/hash-map.ts +30 -0
- package/src/data-structures/heap/heap.ts +72 -49
- package/src/data-structures/linked-list/doubly-linked-list.ts +173 -105
- package/src/data-structures/linked-list/singly-linked-list.ts +61 -11
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +123 -13
- package/src/index.ts +2 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +3 -2
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/utils/utils.ts +16 -10
- package/src/utils/utils.ts +4 -2
package/dist/utils/utils.js
CHANGED
|
@@ -213,7 +213,8 @@ exports.roundFixed = roundFixed;
|
|
|
213
213
|
function isPrimitiveComparable(value) {
|
|
214
214
|
const valueType = typeof value;
|
|
215
215
|
if (valueType === 'number')
|
|
216
|
-
return
|
|
216
|
+
return true;
|
|
217
|
+
// if (valueType === 'number') return !Number.isNaN(value);
|
|
217
218
|
return valueType === 'bigint' || valueType === 'string' || valueType === 'boolean';
|
|
218
219
|
}
|
|
219
220
|
/**
|
|
@@ -265,7 +266,8 @@ function isComparable(value, isForceObjectComparable = false) {
|
|
|
265
266
|
if (typeof value !== 'object')
|
|
266
267
|
return false;
|
|
267
268
|
if (value instanceof Date)
|
|
268
|
-
return
|
|
269
|
+
return true;
|
|
270
|
+
// if (value instanceof Date) return !Number.isNaN(value.getTime());
|
|
269
271
|
if (isForceObjectComparable)
|
|
270
272
|
return true;
|
|
271
273
|
const comparableValue = tryObjectToPrimitive(value);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bst-typed",
|
|
3
|
-
"version": "1.53.
|
|
3
|
+
"version": "1.53.8",
|
|
4
4
|
"description": "BST (Binary Search Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -144,6 +144,6 @@
|
|
|
144
144
|
"typescript": "^4.9.5"
|
|
145
145
|
},
|
|
146
146
|
"dependencies": {
|
|
147
|
-
"data-structure-typed": "^1.53.
|
|
147
|
+
"data-structure-typed": "^1.53.8"
|
|
148
148
|
}
|
|
149
149
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isComparable } from '../utils';
|
|
2
|
+
|
|
3
|
+
export enum DFSOperation {
|
|
4
|
+
VISIT = 0,
|
|
5
|
+
PROCESS = 1
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class Range<K> {
|
|
9
|
+
constructor(
|
|
10
|
+
public low: K,
|
|
11
|
+
public high: K,
|
|
12
|
+
public includeLow: boolean = true,
|
|
13
|
+
public includeHigh: boolean = true
|
|
14
|
+
) {
|
|
15
|
+
if (!(isComparable(low) && isComparable(high))) throw new RangeError('low or high is not comparable');
|
|
16
|
+
if (low > high) throw new RangeError('low must be less than or equal to high');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Determine whether a key is within the range
|
|
20
|
+
isInRange(key: K, comparator: (a: K, b: K) => number): boolean {
|
|
21
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
22
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
23
|
+
return lowCheck && highCheck;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -143,8 +143,9 @@ export class AVLTreeMultiMap<
|
|
|
143
143
|
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
144
144
|
iterationType: this.iterationType,
|
|
145
145
|
isMapMode: this._isMapMode,
|
|
146
|
-
|
|
146
|
+
extractComparable: this._extractComparable,
|
|
147
147
|
toEntryFn: this._toEntryFn,
|
|
148
|
+
isReverse: this._isReverse,
|
|
148
149
|
...options
|
|
149
150
|
}) as TREE;
|
|
150
151
|
}
|
|
@@ -172,7 +173,7 @@ export class AVLTreeMultiMap<
|
|
|
172
173
|
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
173
174
|
* @returns either a NODE object or undefined.
|
|
174
175
|
*/
|
|
175
|
-
override
|
|
176
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
176
177
|
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
177
178
|
value?: V,
|
|
178
179
|
count = 1
|
|
@@ -187,17 +188,14 @@ export class AVLTreeMultiMap<
|
|
|
187
188
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
188
189
|
}
|
|
189
190
|
|
|
190
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
191
|
-
|
|
192
191
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
197
|
-
}
|
|
198
|
-
return [undefined, undefined];
|
|
192
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
193
|
+
const finalValue = value ?? entryValue;
|
|
194
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
199
195
|
}
|
|
200
196
|
|
|
197
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
198
|
+
|
|
201
199
|
return [undefined, undefined];
|
|
202
200
|
}
|
|
203
201
|
|
|
@@ -219,7 +217,7 @@ export class AVLTreeMultiMap<
|
|
|
219
217
|
* @returns a boolean value.
|
|
220
218
|
*/
|
|
221
219
|
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count = 1): boolean {
|
|
222
|
-
const [newNode, newValue] = this.
|
|
220
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
223
221
|
if (newNode === undefined) return false;
|
|
224
222
|
|
|
225
223
|
const orgNodeCount = newNode?.count || 0;
|
|
@@ -113,8 +113,9 @@ export class AVLTree<
|
|
|
113
113
|
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
114
114
|
iterationType: this.iterationType,
|
|
115
115
|
isMapMode: this._isMapMode,
|
|
116
|
-
|
|
116
|
+
extractComparable: this._extractComparable,
|
|
117
117
|
toEntryFn: this._toEntryFn,
|
|
118
|
+
isReverse: this._isReverse,
|
|
118
119
|
...options
|
|
119
120
|
}) as TREE;
|
|
120
121
|
}
|
|
@@ -434,7 +435,7 @@ export class AVLTree<
|
|
|
434
435
|
*/
|
|
435
436
|
protected _balancePath(node: BTNRep<K, V, NODE> | R): void {
|
|
436
437
|
node = this.ensureNode(node);
|
|
437
|
-
const path = this.getPathToRoot(node => node,
|
|
438
|
+
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
438
439
|
for (let i = 0; i < path.length; i++) {
|
|
439
440
|
// second O(log n)
|
|
440
441
|
const A = path[i];
|
|
@@ -29,7 +29,7 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
29
29
|
import { isComparable, trampoline } from '../../utils';
|
|
30
30
|
import { Queue } from '../queue';
|
|
31
31
|
import { IterableEntryBase } from '../base';
|
|
32
|
-
import { DFSOperation } from '../../
|
|
32
|
+
import { DFSOperation, Range } from '../../common';
|
|
33
33
|
|
|
34
34
|
/**
|
|
35
35
|
* Represents a node in a binary tree.
|
|
@@ -216,7 +216,7 @@ export class BinaryTree<
|
|
|
216
216
|
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
217
217
|
* value.
|
|
218
218
|
*/
|
|
219
|
-
|
|
219
|
+
protected _keyValueNodeEntryRawToNodeAndValue(
|
|
220
220
|
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
221
221
|
value?: V
|
|
222
222
|
): [OptNodeOrNull<NODE>, V | undefined] {
|
|
@@ -233,17 +233,14 @@ export class BinaryTree<
|
|
|
233
233
|
return [this.createNode(key, finalValue), finalValue];
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
237
|
-
|
|
238
236
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
243
|
-
}
|
|
244
|
-
return [undefined, undefined];
|
|
237
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
238
|
+
const finalValue = value ?? entryValue;
|
|
239
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
|
|
245
240
|
}
|
|
246
241
|
|
|
242
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
243
|
+
|
|
247
244
|
return [undefined, undefined];
|
|
248
245
|
}
|
|
249
246
|
|
|
@@ -275,15 +272,15 @@ export class BinaryTree<
|
|
|
275
272
|
const key = keyNodeEntryOrRaw[0];
|
|
276
273
|
if (key === null) return null;
|
|
277
274
|
if (key === undefined) return;
|
|
278
|
-
return this.
|
|
275
|
+
return this.getNode(key, this._root, iterationType);
|
|
279
276
|
}
|
|
280
277
|
|
|
281
278
|
if (this._toEntryFn) {
|
|
282
279
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw as R);
|
|
283
|
-
if (this.isKey(key)) return this.
|
|
280
|
+
if (this.isKey(key)) return this.getNode(key);
|
|
284
281
|
}
|
|
285
282
|
|
|
286
|
-
if (this.isKey(keyNodeEntryOrRaw)) return this.
|
|
283
|
+
if (this.isKey(keyNodeEntryOrRaw)) return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
287
284
|
return;
|
|
288
285
|
}
|
|
289
286
|
|
|
@@ -302,8 +299,15 @@ export class BinaryTree<
|
|
|
302
299
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
303
300
|
}
|
|
304
301
|
|
|
302
|
+
/**
|
|
303
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
304
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
305
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
306
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
307
|
+
* indicating that it is of type `R`.
|
|
308
|
+
*/
|
|
305
309
|
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R {
|
|
306
|
-
return typeof keyNodeEntryOrRaw === 'object';
|
|
310
|
+
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
307
311
|
}
|
|
308
312
|
|
|
309
313
|
/**
|
|
@@ -345,6 +349,12 @@ export class BinaryTree<
|
|
|
345
349
|
return keyNodeEntryOrRaw === this._NIL;
|
|
346
350
|
}
|
|
347
351
|
|
|
352
|
+
isRange(
|
|
353
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>
|
|
354
|
+
): keyNodeEntryRawOrPredicate is Range<K> {
|
|
355
|
+
return keyNodeEntryRawOrPredicate instanceof Range;
|
|
356
|
+
}
|
|
357
|
+
|
|
348
358
|
/**
|
|
349
359
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
350
360
|
* tree.
|
|
@@ -410,7 +420,7 @@ export class BinaryTree<
|
|
|
410
420
|
* key was found and the node was replaced instead of inserted.
|
|
411
421
|
*/
|
|
412
422
|
add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
|
|
413
|
-
const [newNode, newValue] = this.
|
|
423
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
414
424
|
if (newNode === undefined) return false;
|
|
415
425
|
|
|
416
426
|
// If the tree is empty, directly set the new node as the root node
|
|
@@ -508,6 +518,18 @@ export class BinaryTree<
|
|
|
508
518
|
return inserted;
|
|
509
519
|
}
|
|
510
520
|
|
|
521
|
+
/**
|
|
522
|
+
* Time Complexity: O(k * n)
|
|
523
|
+
* Space Complexity: O(1)
|
|
524
|
+
*
|
|
525
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
526
|
+
* elements from the other tree.
|
|
527
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
528
|
+
*/
|
|
529
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
|
|
530
|
+
this.addMany(anotherTree, []);
|
|
531
|
+
}
|
|
532
|
+
|
|
511
533
|
/**
|
|
512
534
|
* Time Complexity: O(k * n)
|
|
513
535
|
* Space Complexity: O(1)
|
|
@@ -589,41 +611,45 @@ export class BinaryTree<
|
|
|
589
611
|
* Time Complexity: O(n)
|
|
590
612
|
* Space Complexity: O(k + log n)
|
|
591
613
|
*
|
|
592
|
-
* The
|
|
593
|
-
* or
|
|
594
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
595
|
-
*
|
|
596
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
597
|
-
* determines whether
|
|
598
|
-
*
|
|
599
|
-
* @param {
|
|
600
|
-
*
|
|
601
|
-
*
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* @
|
|
607
|
-
*
|
|
614
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
615
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
616
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
617
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
618
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
619
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
620
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
621
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
622
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
623
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
624
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
625
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
626
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
627
|
+
* provided, the search operation will start from the root
|
|
628
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
629
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
630
|
+
* two possible values:
|
|
631
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
632
|
+
* on the search algorithm implemented within the function.
|
|
608
633
|
*/
|
|
609
|
-
|
|
634
|
+
search<C extends NodeCallback<NODE>>(
|
|
610
635
|
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
611
636
|
onlyOne = false,
|
|
637
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
612
638
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
613
639
|
iterationType: IterationType = this.iterationType
|
|
614
|
-
):
|
|
640
|
+
): ReturnType<C>[] {
|
|
615
641
|
if (keyNodeEntryRawOrPredicate === undefined) return [];
|
|
616
642
|
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
617
643
|
startNode = this.ensureNode(startNode);
|
|
618
644
|
if (!startNode) return [];
|
|
619
|
-
const
|
|
645
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
620
646
|
|
|
621
|
-
const ans:
|
|
647
|
+
const ans: ReturnType<C>[] = [];
|
|
622
648
|
|
|
623
649
|
if (iterationType === 'RECURSIVE') {
|
|
624
650
|
const dfs = (cur: NODE) => {
|
|
625
|
-
if (
|
|
626
|
-
ans.push(cur);
|
|
651
|
+
if (predicate(cur)) {
|
|
652
|
+
ans.push(callback(cur));
|
|
627
653
|
if (onlyOne) return;
|
|
628
654
|
}
|
|
629
655
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
@@ -637,8 +663,8 @@ export class BinaryTree<
|
|
|
637
663
|
while (stack.length > 0) {
|
|
638
664
|
const cur = stack.pop();
|
|
639
665
|
if (this.isRealNode(cur)) {
|
|
640
|
-
if (
|
|
641
|
-
ans.push(cur);
|
|
666
|
+
if (predicate(cur)) {
|
|
667
|
+
ans.push(callback(cur));
|
|
642
668
|
if (onlyOne) return ans;
|
|
643
669
|
}
|
|
644
670
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -650,6 +676,36 @@ export class BinaryTree<
|
|
|
650
676
|
return ans;
|
|
651
677
|
}
|
|
652
678
|
|
|
679
|
+
/**
|
|
680
|
+
* Time Complexity: O(n)
|
|
681
|
+
* Space Complexity: O(k + log n)
|
|
682
|
+
*
|
|
683
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
684
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
685
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
686
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
687
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
688
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
689
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
690
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
691
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
692
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
693
|
+
* not provided, the default value is set to `this._root
|
|
694
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
695
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
696
|
+
* can have two possible values:
|
|
697
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
698
|
+
* based on the input parameters and the iteration type specified.
|
|
699
|
+
*/
|
|
700
|
+
getNodes(
|
|
701
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
702
|
+
onlyOne = false,
|
|
703
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
704
|
+
iterationType: IterationType = this.iterationType
|
|
705
|
+
): NODE[] {
|
|
706
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
707
|
+
}
|
|
708
|
+
|
|
653
709
|
/**
|
|
654
710
|
* Time Complexity: O(n)
|
|
655
711
|
* Space Complexity: O(log n).
|
|
@@ -675,24 +731,7 @@ export class BinaryTree<
|
|
|
675
731
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
676
732
|
iterationType: IterationType = this.iterationType
|
|
677
733
|
): OptNodeOrNull<NODE> {
|
|
678
|
-
return this.
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Time Complexity: O(n)
|
|
683
|
-
* Space Complexity: O(log n)
|
|
684
|
-
*
|
|
685
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
686
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
687
|
-
* structure.
|
|
688
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
689
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
690
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
691
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
692
|
-
* (`OptNodeOrNull<NODE>`).
|
|
693
|
-
*/
|
|
694
|
-
getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNodeOrNull<NODE> {
|
|
695
|
-
return this.getNode(key, this._root, iterationType);
|
|
734
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0] ?? null;
|
|
696
735
|
}
|
|
697
736
|
|
|
698
737
|
/**
|
|
@@ -723,7 +762,7 @@ export class BinaryTree<
|
|
|
723
762
|
iterationType: IterationType = this.iterationType
|
|
724
763
|
): V | undefined {
|
|
725
764
|
if (this._isMapMode) {
|
|
726
|
-
const key = this.
|
|
765
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
727
766
|
if (key === null || key === undefined) return;
|
|
728
767
|
return this._store.get(key);
|
|
729
768
|
}
|
|
@@ -756,7 +795,7 @@ export class BinaryTree<
|
|
|
756
795
|
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
757
796
|
iterationType: IterationType = this.iterationType
|
|
758
797
|
): boolean {
|
|
759
|
-
return this.
|
|
798
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
760
799
|
}
|
|
761
800
|
|
|
762
801
|
/**
|
|
@@ -1023,9 +1062,9 @@ export class BinaryTree<
|
|
|
1023
1062
|
* parameter.
|
|
1024
1063
|
*/
|
|
1025
1064
|
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(
|
|
1026
|
-
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
1027
1065
|
beginNode: BTNRep<K, V, NODE> | R,
|
|
1028
|
-
|
|
1066
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
1067
|
+
isReverse = false
|
|
1029
1068
|
): ReturnType<C>[] {
|
|
1030
1069
|
const result: ReturnType<C>[] = [];
|
|
1031
1070
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
@@ -1118,7 +1157,6 @@ export class BinaryTree<
|
|
|
1118
1157
|
iterationType: IterationType = this.iterationType
|
|
1119
1158
|
): ReturnType<C> {
|
|
1120
1159
|
if (this.isNIL(startNode)) return callback(undefined);
|
|
1121
|
-
// TODO support get right most by passing key in
|
|
1122
1160
|
startNode = this.ensureNode(startNode);
|
|
1123
1161
|
if (!startNode) return callback(startNode);
|
|
1124
1162
|
|
|
@@ -2144,16 +2182,16 @@ export class BinaryTree<
|
|
|
2144
2182
|
* Time Complexity: O(1)
|
|
2145
2183
|
* Space Complexity: O(1)
|
|
2146
2184
|
*
|
|
2147
|
-
* The function `
|
|
2185
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
2148
2186
|
* entry, raw data, or null/undefined.
|
|
2149
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
2187
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
2150
2188
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
2151
2189
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
2152
|
-
* @returns The `
|
|
2190
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
2153
2191
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
2154
2192
|
* the conditions checked in the method.
|
|
2155
2193
|
*/
|
|
2156
|
-
protected
|
|
2194
|
+
protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined {
|
|
2157
2195
|
if (keyNodeEntryOrRaw === null) return null;
|
|
2158
2196
|
if (keyNodeEntryOrRaw === undefined) return;
|
|
2159
2197
|
if (keyNodeEntryOrRaw === this._NIL) return;
|
|
@@ -2193,6 +2231,9 @@ export class BinaryTree<
|
|
|
2193
2231
|
}
|
|
2194
2232
|
|
|
2195
2233
|
/**
|
|
2234
|
+
* Time Complexity: O(1)
|
|
2235
|
+
* Space Complexity: O(1)
|
|
2236
|
+
*
|
|
2196
2237
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
2197
2238
|
*/
|
|
2198
2239
|
protected _clearNodes() {
|
|
@@ -2201,6 +2242,9 @@ export class BinaryTree<
|
|
|
2201
2242
|
}
|
|
2202
2243
|
|
|
2203
2244
|
/**
|
|
2245
|
+
* Time Complexity: O(1)
|
|
2246
|
+
* Space Complexity: O(1)
|
|
2247
|
+
*
|
|
2204
2248
|
* The _clearValues function clears all values stored in the _store object.
|
|
2205
2249
|
*/
|
|
2206
2250
|
protected _clearValues() {
|