data-structure-typed 1.53.6 → 1.53.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/README.md +16 -25
- package/benchmark/report.html +32 -5
- package/benchmark/report.json +326 -23
- package/dist/cjs/common/index.d.ts +12 -0
- package/dist/cjs/common/index.js +24 -0
- package/dist/cjs/common/index.js.map +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/cjs/data-structures/binary-tree/bst.js +185 -66
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/cjs/data-structures/heap/heap.js +6 -6
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/cjs/data-structures/trie/trie.js +122 -12
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/cjs/types/utils/utils.d.ts +10 -6
- package/dist/cjs/utils/utils.js +4 -2
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/mjs/common/index.d.ts +12 -0
- package/dist/mjs/common/index.js +24 -0
- package/dist/mjs/common/index.js.map +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +8 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +3 -2
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +95 -61
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/mjs/data-structures/binary-tree/bst.js +187 -66
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/heap/heap.d.ts +6 -6
- package/dist/mjs/data-structures/heap/heap.js +6 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/trie/trie.d.ts +110 -4
- package/dist/mjs/data-structures/trie/trie.js +122 -12
- package/dist/mjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/index.d.ts +1 -1
- package/dist/mjs/index.js +1 -1
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/mjs/types/utils/utils.d.ts +10 -6
- package/dist/mjs/utils/utils.js +4 -2
- package/dist/mjs/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +299 -156
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +7 -7
- package/src/common/index.ts +19 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -9
- package/src/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/data-structures/binary-tree/binary-tree.ts +108 -64
- package/src/data-structures/binary-tree/bst.ts +190 -69
- package/src/data-structures/binary-tree/rb-tree.ts +6 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +39 -39
- package/src/data-structures/linked-list/doubly-linked-list.ts +111 -97
- package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
- package/src/data-structures/trie/trie.ts +116 -11
- package/src/index.ts +1 -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/test/performance/data-structures/binary-tree/avl-tree.test.ts +3 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -1
- package/test/performance/reportor.ts +38 -33
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/bst.test.ts +79 -3
- package/test/unit/data-structures/binary-tree/overall.test.ts +14 -22
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +100 -1
- package/test/unit/data-structures/trie/trie.test.ts +151 -0
- package/test/unit/utils/utils.test.ts +6 -6
- package/dist/cjs/constants/index.d.ts +0 -4
- package/dist/cjs/constants/index.js +0 -9
- package/dist/cjs/constants/index.js.map +0 -1
- package/dist/mjs/constants/index.d.ts +0 -4
- package/dist/mjs/constants/index.js +0 -6
- package/dist/mjs/constants/index.js.map +0 -1
- package/src/constants/index.ts +0 -4
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, ToEntryFn } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
|
+
import { Range } from '../../common';
|
|
11
12
|
/**
|
|
12
13
|
* Represents a node in a binary tree.
|
|
13
14
|
* @template V - The type of data stored in the node.
|
|
@@ -120,6 +121,13 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
120
121
|
* is not a node.
|
|
121
122
|
*/
|
|
122
123
|
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
124
|
+
/**
|
|
125
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
126
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
127
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
128
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
129
|
+
* indicating that it is of type `R`.
|
|
130
|
+
*/
|
|
123
131
|
isRaw(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is R;
|
|
124
132
|
/**
|
|
125
133
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -150,6 +158,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
150
158
|
* property of the current object and returning a boolean value based on that comparison.
|
|
151
159
|
*/
|
|
152
160
|
isNIL(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): boolean;
|
|
161
|
+
isRange(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE> | Range<K>): keyNodeEntryRawOrPredicate is Range<K>;
|
|
153
162
|
/**
|
|
154
163
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
155
164
|
* tree.
|
|
@@ -221,6 +230,15 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
221
230
|
* corresponds to the success of adding the corresponding key or value in the input iterable.
|
|
222
231
|
*/
|
|
223
232
|
addMany(keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, NODE> | R>, values?: Iterable<V | undefined>): boolean[];
|
|
233
|
+
/**
|
|
234
|
+
* Time Complexity: O(k * n)
|
|
235
|
+
* Space Complexity: O(1)
|
|
236
|
+
*
|
|
237
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
238
|
+
* elements from the other tree.
|
|
239
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
240
|
+
*/
|
|
241
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>): void;
|
|
224
242
|
/**
|
|
225
243
|
* Time Complexity: O(k * n)
|
|
226
244
|
* Space Complexity: O(1)
|
|
@@ -250,6 +268,31 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
250
268
|
* need to be balanced (`needBalanced`).
|
|
251
269
|
*/
|
|
252
270
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
271
|
+
/**
|
|
272
|
+
* Time Complexity: O(n)
|
|
273
|
+
* Space Complexity: O(k + log n)
|
|
274
|
+
*
|
|
275
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
276
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
277
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
278
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
279
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
280
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
281
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
282
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
283
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
284
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
285
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
286
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
287
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
288
|
+
* provided, the search operation will start from the root
|
|
289
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
290
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
291
|
+
* two possible values:
|
|
292
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
293
|
+
* on the search algorithm implemented within the function.
|
|
294
|
+
*/
|
|
295
|
+
search<C extends NodeCallback<NODE>>(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, onlyOne?: boolean, callback?: C, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): ReturnType<C>[];
|
|
253
296
|
/**
|
|
254
297
|
* Time Complexity: O(n)
|
|
255
298
|
* Space Complexity: O(k + log n)
|
|
@@ -293,20 +336,6 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
293
336
|
* or `null` if no matching node is found.
|
|
294
337
|
*/
|
|
295
338
|
getNode(keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>, startNode?: BTNRep<K, V, NODE> | R, iterationType?: IterationType): OptNodeOrNull<NODE>;
|
|
296
|
-
/**
|
|
297
|
-
* Time Complexity: O(n)
|
|
298
|
-
* Space Complexity: O(log n)
|
|
299
|
-
*
|
|
300
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
301
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
302
|
-
* structure.
|
|
303
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
304
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
305
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
306
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
307
|
-
* (`OptNodeOrNull<NODE>`).
|
|
308
|
-
*/
|
|
309
|
-
getNodeByKey(key: K, iterationType?: IterationType): OptNodeOrNull<NODE>;
|
|
310
339
|
/**
|
|
311
340
|
* Time Complexity: O(n)
|
|
312
341
|
* Space Complexity: O(log n)
|
|
@@ -478,7 +507,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
478
507
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
479
508
|
* parameter.
|
|
480
509
|
*/
|
|
481
|
-
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(
|
|
510
|
+
getPathToRoot<C extends NodeCallback<OptNodeOrNull<NODE>>>(beginNode: BTNRep<K, V, NODE> | R, callback?: C, isReverse?: boolean): ReturnType<C>[];
|
|
482
511
|
/**
|
|
483
512
|
* Time Complexity: O(log n)
|
|
484
513
|
* Space Complexity: O(1)
|
|
@@ -825,16 +854,16 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
825
854
|
* Time Complexity: O(1)
|
|
826
855
|
* Space Complexity: O(1)
|
|
827
856
|
*
|
|
828
|
-
* The function `
|
|
857
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
829
858
|
* entry, raw data, or null/undefined.
|
|
830
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
859
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
831
860
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
832
861
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
833
|
-
* @returns The `
|
|
862
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
834
863
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
835
864
|
* the conditions checked in the method.
|
|
836
865
|
*/
|
|
837
|
-
protected
|
|
866
|
+
protected _extractKey(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): K | null | undefined;
|
|
838
867
|
/**
|
|
839
868
|
* Time Complexity: O(1)
|
|
840
869
|
* Space Complexity: O(1)
|
|
@@ -851,10 +880,16 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
851
880
|
*/
|
|
852
881
|
protected _setValue(key: K | null | undefined, value: V | undefined): false | Map<K, V | undefined>;
|
|
853
882
|
/**
|
|
883
|
+
* Time Complexity: O(1)
|
|
884
|
+
* Space Complexity: O(1)
|
|
885
|
+
*
|
|
854
886
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
855
887
|
*/
|
|
856
888
|
protected _clearNodes(): void;
|
|
857
889
|
/**
|
|
890
|
+
* Time Complexity: O(1)
|
|
891
|
+
* Space Complexity: O(1)
|
|
892
|
+
*
|
|
858
893
|
* The _clearValues function clears all values stored in the _store object.
|
|
859
894
|
*/
|
|
860
895
|
protected _clearValues(): void;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { isComparable, trampoline } from '../../utils';
|
|
9
9
|
import { Queue } from '../queue';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
|
-
import { DFSOperation } from '../../
|
|
11
|
+
import { DFSOperation, Range } from '../../common';
|
|
12
12
|
/**
|
|
13
13
|
* Represents a node in a binary tree.
|
|
14
14
|
* @template V - The type of data stored in the node.
|
|
@@ -173,17 +173,14 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
173
173
|
const finalValue = value ?? entryValue;
|
|
174
174
|
return [this.createNode(key, finalValue), finalValue];
|
|
175
175
|
}
|
|
176
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
177
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
178
176
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
184
|
-
}
|
|
185
|
-
return [undefined, undefined];
|
|
177
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
178
|
+
const finalValue = value ?? entryValue;
|
|
179
|
+
if (this.isKey(key))
|
|
180
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
186
181
|
}
|
|
182
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
183
|
+
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
187
184
|
return [undefined, undefined];
|
|
188
185
|
}
|
|
189
186
|
/**
|
|
@@ -216,15 +213,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
216
213
|
return null;
|
|
217
214
|
if (key === undefined)
|
|
218
215
|
return;
|
|
219
|
-
return this.
|
|
216
|
+
return this.getNode(key, this._root, iterationType);
|
|
220
217
|
}
|
|
221
218
|
if (this._toEntryFn) {
|
|
222
219
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
223
220
|
if (this.isKey(key))
|
|
224
|
-
return this.
|
|
221
|
+
return this.getNode(key);
|
|
225
222
|
}
|
|
226
223
|
if (this.isKey(keyNodeEntryOrRaw))
|
|
227
|
-
return this.
|
|
224
|
+
return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
228
225
|
return;
|
|
229
226
|
}
|
|
230
227
|
/**
|
|
@@ -241,8 +238,15 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
241
238
|
isNode(keyNodeEntryOrRaw) {
|
|
242
239
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
243
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
243
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
244
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
245
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
246
|
+
* indicating that it is of type `R`.
|
|
247
|
+
*/
|
|
244
248
|
isRaw(keyNodeEntryOrRaw) {
|
|
245
|
-
return typeof keyNodeEntryOrRaw === 'object';
|
|
249
|
+
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
246
250
|
}
|
|
247
251
|
/**
|
|
248
252
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -281,6 +285,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
281
285
|
isNIL(keyNodeEntryOrRaw) {
|
|
282
286
|
return keyNodeEntryOrRaw === this._NIL;
|
|
283
287
|
}
|
|
288
|
+
isRange(keyNodeEntryRawOrPredicate) {
|
|
289
|
+
return keyNodeEntryRawOrPredicate instanceof Range;
|
|
290
|
+
}
|
|
284
291
|
/**
|
|
285
292
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
286
293
|
* tree.
|
|
@@ -436,6 +443,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
436
443
|
}
|
|
437
444
|
return inserted;
|
|
438
445
|
}
|
|
446
|
+
/**
|
|
447
|
+
* Time Complexity: O(k * n)
|
|
448
|
+
* Space Complexity: O(1)
|
|
449
|
+
*
|
|
450
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
451
|
+
* elements from the other tree.
|
|
452
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
453
|
+
*/
|
|
454
|
+
merge(anotherTree) {
|
|
455
|
+
this.addMany(anotherTree, []);
|
|
456
|
+
}
|
|
439
457
|
/**
|
|
440
458
|
* Time Complexity: O(k * n)
|
|
441
459
|
* Space Complexity: O(1)
|
|
@@ -518,24 +536,27 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
518
536
|
* Time Complexity: O(n)
|
|
519
537
|
* Space Complexity: O(k + log n)
|
|
520
538
|
*
|
|
521
|
-
* The
|
|
522
|
-
* or
|
|
523
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
524
|
-
*
|
|
525
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
526
|
-
* determines whether
|
|
527
|
-
*
|
|
528
|
-
* @param {
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
* @
|
|
536
|
-
*
|
|
539
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
540
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
541
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
542
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
543
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
544
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
545
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
546
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
547
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
548
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
549
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
550
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
551
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
552
|
+
* provided, the search operation will start from the root
|
|
553
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
554
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
555
|
+
* two possible values:
|
|
556
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
557
|
+
* on the search algorithm implemented within the function.
|
|
537
558
|
*/
|
|
538
|
-
|
|
559
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
539
560
|
if (keyNodeEntryRawOrPredicate === undefined)
|
|
540
561
|
return [];
|
|
541
562
|
if (keyNodeEntryRawOrPredicate === null)
|
|
@@ -543,12 +564,12 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
543
564
|
startNode = this.ensureNode(startNode);
|
|
544
565
|
if (!startNode)
|
|
545
566
|
return [];
|
|
546
|
-
const
|
|
567
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
547
568
|
const ans = [];
|
|
548
569
|
if (iterationType === 'RECURSIVE') {
|
|
549
570
|
const dfs = (cur) => {
|
|
550
|
-
if (
|
|
551
|
-
ans.push(cur);
|
|
571
|
+
if (predicate(cur)) {
|
|
572
|
+
ans.push(callback(cur));
|
|
552
573
|
if (onlyOne)
|
|
553
574
|
return;
|
|
554
575
|
}
|
|
@@ -566,8 +587,8 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
566
587
|
while (stack.length > 0) {
|
|
567
588
|
const cur = stack.pop();
|
|
568
589
|
if (this.isRealNode(cur)) {
|
|
569
|
-
if (
|
|
570
|
-
ans.push(cur);
|
|
590
|
+
if (predicate(cur)) {
|
|
591
|
+
ans.push(callback(cur));
|
|
571
592
|
if (onlyOne)
|
|
572
593
|
return ans;
|
|
573
594
|
}
|
|
@@ -580,6 +601,30 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
580
601
|
}
|
|
581
602
|
return ans;
|
|
582
603
|
}
|
|
604
|
+
/**
|
|
605
|
+
* Time Complexity: O(n)
|
|
606
|
+
* Space Complexity: O(k + log n)
|
|
607
|
+
*
|
|
608
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
609
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
610
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
611
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
612
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
613
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
614
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
615
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
616
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
617
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
618
|
+
* not provided, the default value is set to `this._root
|
|
619
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
620
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
621
|
+
* can have two possible values:
|
|
622
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
623
|
+
* based on the input parameters and the iteration type specified.
|
|
624
|
+
*/
|
|
625
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
626
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
627
|
+
}
|
|
583
628
|
/**
|
|
584
629
|
* Time Complexity: O(n)
|
|
585
630
|
* Space Complexity: O(log n).
|
|
@@ -601,23 +646,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
601
646
|
* or `null` if no matching node is found.
|
|
602
647
|
*/
|
|
603
648
|
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
604
|
-
return this.
|
|
605
|
-
}
|
|
606
|
-
/**
|
|
607
|
-
* Time Complexity: O(n)
|
|
608
|
-
* Space Complexity: O(log n)
|
|
609
|
-
*
|
|
610
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
611
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
612
|
-
* structure.
|
|
613
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
614
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
615
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
616
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
617
|
-
* (`OptNodeOrNull<NODE>`).
|
|
618
|
-
*/
|
|
619
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
620
|
-
return this.getNode(key, this._root, iterationType);
|
|
649
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0] ?? null;
|
|
621
650
|
}
|
|
622
651
|
/**
|
|
623
652
|
* Time Complexity: O(n)
|
|
@@ -643,7 +672,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
643
672
|
*/
|
|
644
673
|
get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
645
674
|
if (this._isMapMode) {
|
|
646
|
-
const key = this.
|
|
675
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
647
676
|
if (key === null || key === undefined)
|
|
648
677
|
return;
|
|
649
678
|
return this._store.get(key);
|
|
@@ -672,7 +701,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
672
701
|
* Otherwise, it returns `false`.
|
|
673
702
|
*/
|
|
674
703
|
has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
675
|
-
return this.
|
|
704
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
676
705
|
}
|
|
677
706
|
/**
|
|
678
707
|
* Time Complexity: O(1)
|
|
@@ -931,7 +960,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
931
960
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
932
961
|
* parameter.
|
|
933
962
|
*/
|
|
934
|
-
getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK,
|
|
963
|
+
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
935
964
|
const result = [];
|
|
936
965
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
937
966
|
if (!beginNodeEnsured)
|
|
@@ -1014,7 +1043,6 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1014
1043
|
getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1015
1044
|
if (this.isNIL(startNode))
|
|
1016
1045
|
return callback(undefined);
|
|
1017
|
-
// TODO support get right most by passing key in
|
|
1018
1046
|
startNode = this.ensureNode(startNode);
|
|
1019
1047
|
if (!startNode)
|
|
1020
1048
|
return callback(startNode);
|
|
@@ -1952,16 +1980,16 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1952
1980
|
* Time Complexity: O(1)
|
|
1953
1981
|
* Space Complexity: O(1)
|
|
1954
1982
|
*
|
|
1955
|
-
* The function `
|
|
1983
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
1956
1984
|
* entry, raw data, or null/undefined.
|
|
1957
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
1985
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
1958
1986
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
1959
1987
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
1960
|
-
* @returns The `
|
|
1988
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
1961
1989
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
1962
1990
|
* the conditions checked in the method.
|
|
1963
1991
|
*/
|
|
1964
|
-
|
|
1992
|
+
_extractKey(keyNodeEntryOrRaw) {
|
|
1965
1993
|
if (keyNodeEntryOrRaw === null)
|
|
1966
1994
|
return null;
|
|
1967
1995
|
if (keyNodeEntryOrRaw === undefined)
|
|
@@ -2003,6 +2031,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
2003
2031
|
return this._store.set(key, value);
|
|
2004
2032
|
}
|
|
2005
2033
|
/**
|
|
2034
|
+
* Time Complexity: O(1)
|
|
2035
|
+
* Space Complexity: O(1)
|
|
2036
|
+
*
|
|
2006
2037
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
2007
2038
|
*/
|
|
2008
2039
|
_clearNodes() {
|
|
@@ -2010,6 +2041,9 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
2010
2041
|
this._size = 0;
|
|
2011
2042
|
}
|
|
2012
2043
|
/**
|
|
2044
|
+
* Time Complexity: O(1)
|
|
2045
|
+
* Space Complexity: O(1)
|
|
2046
|
+
*
|
|
2013
2047
|
* The _clearValues function clears all values stored in the _store object.
|
|
2014
2048
|
*/
|
|
2015
2049
|
_clearValues() {
|