directed-graph-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/dist/common/index.d.ts +12 -0
- package/dist/common/index.js +23 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +7 -10
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +54 -19
- package/dist/data-structures/binary-tree/binary-tree.js +100 -66
- package/dist/data-structures/binary-tree/bst.d.ts +100 -36
- package/dist/data-structures/binary-tree/bst.js +185 -66
- package/dist/data-structures/binary-tree/rb-tree.d.ts +4 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -2
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/data-structures/heap/heap.d.ts +6 -6
- package/dist/data-structures/heap/heap.js +6 -6
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +18 -8
- package/dist/data-structures/linked-list/doubly-linked-list.js +24 -10
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -1
- package/dist/data-structures/trie/trie.d.ts +104 -4
- package/dist/data-structures/trie/trie.js +116 -12
- 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 +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 +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
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare enum DFSOperation {
|
|
2
|
+
VISIT = 0,
|
|
3
|
+
PROCESS = 1
|
|
4
|
+
}
|
|
5
|
+
export declare class Range<K> {
|
|
6
|
+
low: K;
|
|
7
|
+
high: K;
|
|
8
|
+
includeLow: boolean;
|
|
9
|
+
includeHigh: boolean;
|
|
10
|
+
constructor(low: K, high: K, includeLow?: boolean, includeHigh?: boolean);
|
|
11
|
+
isInRange(key: K, comparator: (a: K, b: K) => number): boolean;
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Range = exports.DFSOperation = void 0;
|
|
4
|
+
var DFSOperation;
|
|
5
|
+
(function (DFSOperation) {
|
|
6
|
+
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
7
|
+
DFSOperation[DFSOperation["PROCESS"] = 1] = "PROCESS";
|
|
8
|
+
})(DFSOperation = exports.DFSOperation || (exports.DFSOperation = {}));
|
|
9
|
+
class Range {
|
|
10
|
+
constructor(low, high, includeLow = true, includeHigh = true) {
|
|
11
|
+
this.low = low;
|
|
12
|
+
this.high = high;
|
|
13
|
+
this.includeLow = includeLow;
|
|
14
|
+
this.includeHigh = includeHigh;
|
|
15
|
+
}
|
|
16
|
+
// Determine whether a key is within the range
|
|
17
|
+
isInRange(key, comparator) {
|
|
18
|
+
const lowCheck = this.includeLow ? comparator(key, this.low) >= 0 : comparator(key, this.low) > 0;
|
|
19
|
+
const highCheck = this.includeHigh ? comparator(key, this.high) <= 0 : comparator(key, this.high) < 0;
|
|
20
|
+
return lowCheck && highCheck;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Range = Range;
|
|
@@ -96,7 +96,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
96
96
|
* object.
|
|
97
97
|
*/
|
|
98
98
|
createTree(options) {
|
|
99
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
99
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
102
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
@@ -132,17 +132,14 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
132
132
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
133
133
|
return [this.createNode(key, finalValue, count), finalValue];
|
|
134
134
|
}
|
|
135
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
136
|
-
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
137
135
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
143
|
-
}
|
|
144
|
-
return [undefined, undefined];
|
|
136
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
137
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
138
|
+
if (this.isKey(key))
|
|
139
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
145
140
|
}
|
|
141
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
142
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
146
143
|
return [undefined, undefined];
|
|
147
144
|
}
|
|
148
145
|
/**
|
|
@@ -85,7 +85,7 @@ class AVLTree extends bst_1.BST {
|
|
|
85
85
|
* @returns a new AVLTree object.
|
|
86
86
|
*/
|
|
87
87
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
88
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, extractComparable: this._extractComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
89
89
|
}
|
|
90
90
|
/**
|
|
91
91
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
@@ -409,7 +409,7 @@ class AVLTree extends bst_1.BST {
|
|
|
409
409
|
*/
|
|
410
410
|
_balancePath(node) {
|
|
411
411
|
node = this.ensureNode(node);
|
|
412
|
-
const path = this.getPathToRoot(node => node,
|
|
412
|
+
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
413
413
|
for (let i = 0; i < path.length; i++) {
|
|
414
414
|
// second O(log n)
|
|
415
415
|
const A = path[i];
|
|
@@ -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;
|
|
@@ -11,7 +11,7 @@ exports.BinaryTree = exports.BinaryTreeNode = void 0;
|
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
12
|
const queue_1 = require("../queue");
|
|
13
13
|
const base_1 = require("../base");
|
|
14
|
-
const
|
|
14
|
+
const common_1 = require("../../common");
|
|
15
15
|
/**
|
|
16
16
|
* Represents a node in a binary tree.
|
|
17
17
|
* @template V - The type of data stored in the node.
|
|
@@ -166,17 +166,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
166
166
|
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
167
167
|
return [this.createNode(key, finalValue), finalValue];
|
|
168
168
|
}
|
|
169
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
170
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
171
169
|
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
177
|
-
}
|
|
178
|
-
return [undefined, undefined];
|
|
170
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
171
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
172
|
+
if (this.isKey(key))
|
|
173
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
179
174
|
}
|
|
175
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
176
|
+
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
180
177
|
return [undefined, undefined];
|
|
181
178
|
}
|
|
182
179
|
/**
|
|
@@ -209,15 +206,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
209
206
|
return null;
|
|
210
207
|
if (key === undefined)
|
|
211
208
|
return;
|
|
212
|
-
return this.
|
|
209
|
+
return this.getNode(key, this._root, iterationType);
|
|
213
210
|
}
|
|
214
211
|
if (this._toEntryFn) {
|
|
215
212
|
const [key] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
216
213
|
if (this.isKey(key))
|
|
217
|
-
return this.
|
|
214
|
+
return this.getNode(key);
|
|
218
215
|
}
|
|
219
216
|
if (this.isKey(keyNodeEntryOrRaw))
|
|
220
|
-
return this.
|
|
217
|
+
return this.getNode(keyNodeEntryOrRaw, this._root, iterationType);
|
|
221
218
|
return;
|
|
222
219
|
}
|
|
223
220
|
/**
|
|
@@ -234,8 +231,15 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
234
231
|
isNode(keyNodeEntryOrRaw) {
|
|
235
232
|
return keyNodeEntryOrRaw instanceof BinaryTreeNode;
|
|
236
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* The function `isRaw` checks if the input parameter is of type `R` by verifying if it is an object.
|
|
236
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - BTNRep<K, V, NODE> | R
|
|
237
|
+
* @returns The function `isRaw` is checking if the `keyNodeEntryOrRaw` parameter is of type `R` by
|
|
238
|
+
* checking if it is an object. If the parameter is an object, the function will return `true`,
|
|
239
|
+
* indicating that it is of type `R`.
|
|
240
|
+
*/
|
|
237
241
|
isRaw(keyNodeEntryOrRaw) {
|
|
238
|
-
return typeof keyNodeEntryOrRaw === 'object';
|
|
242
|
+
return this._toEntryFn !== undefined && typeof keyNodeEntryOrRaw === 'object';
|
|
239
243
|
}
|
|
240
244
|
/**
|
|
241
245
|
* The function `isRealNode` checks if a given input is a valid node in a binary tree.
|
|
@@ -274,6 +278,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
274
278
|
isNIL(keyNodeEntryOrRaw) {
|
|
275
279
|
return keyNodeEntryOrRaw === this._NIL;
|
|
276
280
|
}
|
|
281
|
+
isRange(keyNodeEntryRawOrPredicate) {
|
|
282
|
+
return keyNodeEntryRawOrPredicate instanceof common_1.Range;
|
|
283
|
+
}
|
|
277
284
|
/**
|
|
278
285
|
* The function determines whether a given key, node, entry, or raw data is a leaf node in a binary
|
|
279
286
|
* tree.
|
|
@@ -429,6 +436,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
429
436
|
}
|
|
430
437
|
return inserted;
|
|
431
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* Time Complexity: O(k * n)
|
|
441
|
+
* Space Complexity: O(1)
|
|
442
|
+
*
|
|
443
|
+
* The `merge` function in TypeScript merges another binary tree into the current tree by adding all
|
|
444
|
+
* elements from the other tree.
|
|
445
|
+
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
446
|
+
*/
|
|
447
|
+
merge(anotherTree) {
|
|
448
|
+
this.addMany(anotherTree, []);
|
|
449
|
+
}
|
|
432
450
|
/**
|
|
433
451
|
* Time Complexity: O(k * n)
|
|
434
452
|
* Space Complexity: O(1)
|
|
@@ -511,24 +529,27 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
511
529
|
* Time Complexity: O(n)
|
|
512
530
|
* Space Complexity: O(k + log n)
|
|
513
531
|
*
|
|
514
|
-
* The
|
|
515
|
-
* or
|
|
516
|
-
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
517
|
-
*
|
|
518
|
-
* @param [onlyOne=false] - The `onlyOne` parameter in the `
|
|
519
|
-
* determines whether
|
|
520
|
-
*
|
|
521
|
-
* @param {
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
* @
|
|
529
|
-
*
|
|
532
|
+
* The `search` function in TypeScript performs a depth-first or breadth-first search on a tree
|
|
533
|
+
* structure based on a given predicate or key, with options to return multiple results or just one.
|
|
534
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The
|
|
535
|
+
* `keyNodeEntryRawOrPredicate` parameter in the `search` function can accept three types of values:
|
|
536
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `search` function is a boolean flag that
|
|
537
|
+
* determines whether the search should stop after finding the first matching node. If `onlyOne` is
|
|
538
|
+
* set to `true`, the search will return as soon as a matching node is found. If `onlyOne` is
|
|
539
|
+
* @param {C} callback - The `callback` parameter in the `search` function is a callback function
|
|
540
|
+
* that will be called on each node that matches the search criteria. It is of type `C`, which
|
|
541
|
+
* extends `NodeCallback<NODE>`. The default value for `callback` is `this._DEFAULT_NODE_CALLBACK` if
|
|
542
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the `search` function is
|
|
543
|
+
* used to specify the node from which the search operation should begin. It represents the starting
|
|
544
|
+
* point in the binary tree where the search will be performed. If no specific `startNode` is
|
|
545
|
+
* provided, the search operation will start from the root
|
|
546
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `search` function
|
|
547
|
+
* specifies the type of iteration to be used when searching for nodes in a binary tree. It can have
|
|
548
|
+
* two possible values:
|
|
549
|
+
* @returns The `search` function returns an array of values that match the provided criteria based
|
|
550
|
+
* on the search algorithm implemented within the function.
|
|
530
551
|
*/
|
|
531
|
-
|
|
552
|
+
search(keyNodeEntryRawOrPredicate, onlyOne = false, callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
532
553
|
if (keyNodeEntryRawOrPredicate === undefined)
|
|
533
554
|
return [];
|
|
534
555
|
if (keyNodeEntryRawOrPredicate === null)
|
|
@@ -536,12 +557,12 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
536
557
|
startNode = this.ensureNode(startNode);
|
|
537
558
|
if (!startNode)
|
|
538
559
|
return [];
|
|
539
|
-
const
|
|
560
|
+
const predicate = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
540
561
|
const ans = [];
|
|
541
562
|
if (iterationType === 'RECURSIVE') {
|
|
542
563
|
const dfs = (cur) => {
|
|
543
|
-
if (
|
|
544
|
-
ans.push(cur);
|
|
564
|
+
if (predicate(cur)) {
|
|
565
|
+
ans.push(callback(cur));
|
|
545
566
|
if (onlyOne)
|
|
546
567
|
return;
|
|
547
568
|
}
|
|
@@ -559,8 +580,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
559
580
|
while (stack.length > 0) {
|
|
560
581
|
const cur = stack.pop();
|
|
561
582
|
if (this.isRealNode(cur)) {
|
|
562
|
-
if (
|
|
563
|
-
ans.push(cur);
|
|
583
|
+
if (predicate(cur)) {
|
|
584
|
+
ans.push(callback(cur));
|
|
564
585
|
if (onlyOne)
|
|
565
586
|
return ans;
|
|
566
587
|
}
|
|
@@ -573,6 +594,30 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
573
594
|
}
|
|
574
595
|
return ans;
|
|
575
596
|
}
|
|
597
|
+
/**
|
|
598
|
+
* Time Complexity: O(n)
|
|
599
|
+
* Space Complexity: O(k + log n)
|
|
600
|
+
*
|
|
601
|
+
* The function `getNodes` retrieves nodes from a binary tree based on a key, node, entry, raw data,
|
|
602
|
+
* or predicate, with options for recursive or iterative traversal.
|
|
603
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate
|
|
604
|
+
* - The `getNodes` function you provided takes several parameters:
|
|
605
|
+
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` function is a boolean flag that
|
|
606
|
+
* determines whether to return only the first node that matches the criteria specified by the
|
|
607
|
+
* `keyNodeEntryRawOrPredicate` parameter. If `onlyOne` is set to `true`, the function will
|
|
608
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
609
|
+
* `getNodes` function is used to specify the starting point for traversing the binary tree. It
|
|
610
|
+
* represents the root node of the binary tree or the node from which the traversal should begin. If
|
|
611
|
+
* not provided, the default value is set to `this._root
|
|
612
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` function
|
|
613
|
+
* determines the type of iteration to be performed when traversing the nodes of a binary tree. It
|
|
614
|
+
* can have two possible values:
|
|
615
|
+
* @returns The `getNodes` function returns an array of nodes that satisfy the provided condition
|
|
616
|
+
* based on the input parameters and the iteration type specified.
|
|
617
|
+
*/
|
|
618
|
+
getNodes(keyNodeEntryRawOrPredicate, onlyOne = false, startNode = this._root, iterationType = this.iterationType) {
|
|
619
|
+
return this.search(keyNodeEntryRawOrPredicate, onlyOne, node => node, startNode, iterationType);
|
|
620
|
+
}
|
|
576
621
|
/**
|
|
577
622
|
* Time Complexity: O(n)
|
|
578
623
|
* Space Complexity: O(log n).
|
|
@@ -595,23 +640,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
595
640
|
*/
|
|
596
641
|
getNode(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
597
642
|
var _a;
|
|
598
|
-
return (_a = this.
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Time Complexity: O(n)
|
|
602
|
-
* Space Complexity: O(log n)
|
|
603
|
-
*
|
|
604
|
-
* The function `getNodeByKey` retrieves a node by its key from a binary tree structure.
|
|
605
|
-
* @param {K} key - The `key` parameter is the value used to search for a specific node in a data
|
|
606
|
-
* structure.
|
|
607
|
-
* @param {IterationType} iterationType - The `iterationType` parameter is a type of iteration that
|
|
608
|
-
* specifies how the tree nodes should be traversed when searching for a node with the given key. It
|
|
609
|
-
* is an optional parameter with a default value of `this.iterationType`.
|
|
610
|
-
* @returns The `getNodeByKey` function is returning an optional binary tree node
|
|
611
|
-
* (`OptNodeOrNull<NODE>`).
|
|
612
|
-
*/
|
|
613
|
-
getNodeByKey(key, iterationType = this.iterationType) {
|
|
614
|
-
return this.getNode(key, this._root, iterationType);
|
|
643
|
+
return (_a = this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
|
|
615
644
|
}
|
|
616
645
|
/**
|
|
617
646
|
* Time Complexity: O(n)
|
|
@@ -638,7 +667,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
638
667
|
get(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
639
668
|
var _a;
|
|
640
669
|
if (this._isMapMode) {
|
|
641
|
-
const key = this.
|
|
670
|
+
const key = this._extractKey(keyNodeEntryRawOrPredicate);
|
|
642
671
|
if (key === null || key === undefined)
|
|
643
672
|
return;
|
|
644
673
|
return this._store.get(key);
|
|
@@ -667,7 +696,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
667
696
|
* Otherwise, it returns `false`.
|
|
668
697
|
*/
|
|
669
698
|
has(keyNodeEntryRawOrPredicate, startNode = this._root, iterationType = this.iterationType) {
|
|
670
|
-
return this.
|
|
699
|
+
return this.search(keyNodeEntryRawOrPredicate, true, node => node, startNode, iterationType).length > 0;
|
|
671
700
|
}
|
|
672
701
|
/**
|
|
673
702
|
* Time Complexity: O(1)
|
|
@@ -926,7 +955,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
926
955
|
* array is either in reverse order or in the original order based on the value of the `isReverse`
|
|
927
956
|
* parameter.
|
|
928
957
|
*/
|
|
929
|
-
getPathToRoot(callback = this._DEFAULT_NODE_CALLBACK,
|
|
958
|
+
getPathToRoot(beginNode, callback = this._DEFAULT_NODE_CALLBACK, isReverse = false) {
|
|
930
959
|
const result = [];
|
|
931
960
|
let beginNodeEnsured = this.ensureNode(beginNode);
|
|
932
961
|
if (!beginNodeEnsured)
|
|
@@ -1009,7 +1038,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1009
1038
|
getRightMost(callback = this._DEFAULT_NODE_CALLBACK, startNode = this._root, iterationType = this.iterationType) {
|
|
1010
1039
|
if (this.isNIL(startNode))
|
|
1011
1040
|
return callback(undefined);
|
|
1012
|
-
// TODO support get right most by passing key in
|
|
1013
1041
|
startNode = this.ensureNode(startNode);
|
|
1014
1042
|
if (!startNode)
|
|
1015
1043
|
return callback(startNode);
|
|
@@ -1655,20 +1683,20 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1655
1683
|
dfs(startNode);
|
|
1656
1684
|
}
|
|
1657
1685
|
else {
|
|
1658
|
-
const stack = [{ opt:
|
|
1686
|
+
const stack = [{ opt: common_1.DFSOperation.VISIT, node: startNode }];
|
|
1659
1687
|
const pushLeft = (cur) => {
|
|
1660
1688
|
var _a;
|
|
1661
1689
|
if (shouldVisitLeft(cur.node))
|
|
1662
|
-
stack.push({ opt:
|
|
1690
|
+
stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.left });
|
|
1663
1691
|
};
|
|
1664
1692
|
const pushRight = (cur) => {
|
|
1665
1693
|
var _a;
|
|
1666
1694
|
if (shouldVisitRight(cur.node))
|
|
1667
|
-
stack.push({ opt:
|
|
1695
|
+
stack.push({ opt: common_1.DFSOperation.VISIT, node: (_a = cur.node) === null || _a === void 0 ? void 0 : _a.right });
|
|
1668
1696
|
};
|
|
1669
1697
|
const pushRoot = (cur) => {
|
|
1670
1698
|
if (shouldVisitRoot(cur.node))
|
|
1671
|
-
stack.push({ opt:
|
|
1699
|
+
stack.push({ opt: common_1.DFSOperation.PROCESS, node: cur.node });
|
|
1672
1700
|
};
|
|
1673
1701
|
while (stack.length > 0) {
|
|
1674
1702
|
const cur = stack.pop();
|
|
@@ -1676,7 +1704,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1676
1704
|
continue;
|
|
1677
1705
|
if (!shouldVisitRoot(cur.node))
|
|
1678
1706
|
continue;
|
|
1679
|
-
if (cur.opt ===
|
|
1707
|
+
if (cur.opt === common_1.DFSOperation.PROCESS) {
|
|
1680
1708
|
if (shouldProcessRoot(cur.node))
|
|
1681
1709
|
ans.push(callback(cur.node));
|
|
1682
1710
|
}
|
|
@@ -1948,16 +1976,16 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1948
1976
|
* Time Complexity: O(1)
|
|
1949
1977
|
* Space Complexity: O(1)
|
|
1950
1978
|
*
|
|
1951
|
-
* The function `
|
|
1979
|
+
* The function `_extractKey` in TypeScript returns the key from a given input, which can be a node,
|
|
1952
1980
|
* entry, raw data, or null/undefined.
|
|
1953
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `
|
|
1981
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The `_extractKey` method you provided is a
|
|
1954
1982
|
* TypeScript method that takes in a parameter `keyNodeEntryOrRaw` of type `BTNRep<K, V, NODE> | R`,
|
|
1955
1983
|
* where `BTNRep` is a generic type with keys `K`, `V`, and `NODE`, and `
|
|
1956
|
-
* @returns The `
|
|
1984
|
+
* @returns The `_extractKey` method returns the key value extracted from the `keyNodeEntryOrRaw`
|
|
1957
1985
|
* parameter. The return value can be a key value of type `K`, `null`, or `undefined`, depending on
|
|
1958
1986
|
* the conditions checked in the method.
|
|
1959
1987
|
*/
|
|
1960
|
-
|
|
1988
|
+
_extractKey(keyNodeEntryOrRaw) {
|
|
1961
1989
|
if (keyNodeEntryOrRaw === null)
|
|
1962
1990
|
return null;
|
|
1963
1991
|
if (keyNodeEntryOrRaw === undefined)
|
|
@@ -1999,6 +2027,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1999
2027
|
return this._store.set(key, value);
|
|
2000
2028
|
}
|
|
2001
2029
|
/**
|
|
2030
|
+
* Time Complexity: O(1)
|
|
2031
|
+
* Space Complexity: O(1)
|
|
2032
|
+
*
|
|
2002
2033
|
* The _clearNodes function sets the root node to undefined and resets the size to 0.
|
|
2003
2034
|
*/
|
|
2004
2035
|
_clearNodes() {
|
|
@@ -2006,6 +2037,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
2006
2037
|
this._size = 0;
|
|
2007
2038
|
}
|
|
2008
2039
|
/**
|
|
2040
|
+
* Time Complexity: O(1)
|
|
2041
|
+
* Space Complexity: O(1)
|
|
2042
|
+
*
|
|
2009
2043
|
* The _clearValues function clears all values stored in the _store object.
|
|
2010
2044
|
*/
|
|
2011
2045
|
_clearValues() {
|