linked-list-typed 1.52.9 → 1.53.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +63 -46
- package/dist/data-structures/binary-tree/avl-tree.d.ts +20 -20
- package/dist/data-structures/binary-tree/avl-tree.js +28 -26
- package/dist/data-structures/binary-tree/binary-tree.d.ts +186 -144
- package/dist/data-structures/binary-tree/binary-tree.js +375 -264
- package/dist/data-structures/binary-tree/bst.d.ts +56 -56
- package/dist/data-structures/binary-tree/bst.js +105 -77
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -13
- package/dist/data-structures/binary-tree/rb-tree.js +35 -33
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +21 -21
- package/dist/data-structures/binary-tree/tree-multi-map.js +58 -48
- package/dist/data-structures/trie/trie.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +13 -13
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +59 -53
- package/src/data-structures/binary-tree/avl-tree.ts +31 -34
- package/src/data-structures/binary-tree/binary-tree.ts +439 -359
- package/src/data-structures/binary-tree/bst.ts +142 -112
- package/src/data-structures/binary-tree/rb-tree.ts +37 -41
- package/src/data-structures/binary-tree/tree-multi-map.ts +56 -60
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/interfaces/binary-tree.ts +6 -6
- package/src/types/data-structures/binary-tree/binary-tree.ts +14 -15
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
|
@@ -7,18 +7,17 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BSTNested,
|
|
10
|
-
BSTNKeyOrNode,
|
|
11
10
|
BSTNodeNested,
|
|
11
|
+
BSTNOptKeyOrNode,
|
|
12
12
|
BSTOptions,
|
|
13
|
-
|
|
14
|
-
BTNEntry,
|
|
15
|
-
BTNKeyOrNodeOrEntry,
|
|
16
|
-
BTNPredicate,
|
|
13
|
+
BTNRep,
|
|
17
14
|
Comparator,
|
|
18
15
|
CP,
|
|
19
16
|
DFSOrderPattern,
|
|
20
17
|
IterationType,
|
|
21
|
-
|
|
18
|
+
NodeCallback,
|
|
19
|
+
NodePredicate,
|
|
20
|
+
OptNode
|
|
22
21
|
} from '../../types';
|
|
23
22
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
24
23
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -45,16 +44,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
45
44
|
* The function returns the value of the `_left` property.
|
|
46
45
|
* @returns The `_left` property of the current object is being returned.
|
|
47
46
|
*/
|
|
48
|
-
override get left():
|
|
47
|
+
override get left(): OptNode<NODE> {
|
|
49
48
|
return this._left;
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
/**
|
|
53
52
|
* The function sets the left child of a node and updates the parent reference of the child.
|
|
54
|
-
* @param {
|
|
53
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be an
|
|
55
54
|
* instance of the `NODE` class or `undefined`.
|
|
56
55
|
*/
|
|
57
|
-
override set left(v:
|
|
56
|
+
override set left(v: OptNode<NODE>) {
|
|
58
57
|
if (v) {
|
|
59
58
|
v.parent = this as unknown as NODE;
|
|
60
59
|
}
|
|
@@ -68,16 +67,16 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
68
67
|
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
69
68
|
* `undefined`.
|
|
70
69
|
*/
|
|
71
|
-
override get right():
|
|
70
|
+
override get right(): OptNode<NODE> {
|
|
72
71
|
return this._right;
|
|
73
72
|
}
|
|
74
73
|
|
|
75
74
|
/**
|
|
76
75
|
* The function sets the right child of a node and updates the parent reference of the child.
|
|
77
|
-
* @param {
|
|
76
|
+
* @param {OptNode<NODE>} v - The parameter `v` is of type `OptNode<NODE>`. It can either be a
|
|
78
77
|
* `NODE` object or `undefined`.
|
|
79
78
|
*/
|
|
80
|
-
override set right(v:
|
|
79
|
+
override set right(v: OptNode<NODE>) {
|
|
81
80
|
if (v) {
|
|
82
81
|
v.parent = this as unknown as NODE;
|
|
83
82
|
}
|
|
@@ -97,7 +96,7 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
97
96
|
export class BST<
|
|
98
97
|
K = any,
|
|
99
98
|
V = any,
|
|
100
|
-
R =
|
|
99
|
+
R = object,
|
|
101
100
|
NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>,
|
|
102
101
|
TREE extends BST<K, V, R, NODE, TREE> = BST<K, V, R, NODE, BSTNested<K, V, R, NODE>>
|
|
103
102
|
>
|
|
@@ -106,16 +105,13 @@ export class BST<
|
|
|
106
105
|
{
|
|
107
106
|
/**
|
|
108
107
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
109
|
-
* @param
|
|
108
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
110
109
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
111
110
|
* added to the binary search tree during the construction of the object.
|
|
112
111
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
113
112
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
114
113
|
*/
|
|
115
|
-
constructor(
|
|
116
|
-
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
117
|
-
options?: BSTOptions<K, V, R>
|
|
118
|
-
) {
|
|
114
|
+
constructor(keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>> = [], options?: BSTOptions<K, V, R>) {
|
|
119
115
|
super([], options);
|
|
120
116
|
|
|
121
117
|
if (options) {
|
|
@@ -123,7 +119,7 @@ export class BST<
|
|
|
123
119
|
if (comparator) this._comparator = comparator;
|
|
124
120
|
}
|
|
125
121
|
|
|
126
|
-
if (
|
|
122
|
+
if (keysNodesEntriesOrRaws) this.addMany(keysNodesEntriesOrRaws);
|
|
127
123
|
}
|
|
128
124
|
|
|
129
125
|
protected override _root?: NODE = undefined;
|
|
@@ -132,7 +128,7 @@ export class BST<
|
|
|
132
128
|
* The function returns the root node of a tree structure.
|
|
133
129
|
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
134
130
|
*/
|
|
135
|
-
override get root():
|
|
131
|
+
override get root(): OptNode<NODE> {
|
|
136
132
|
return this._root;
|
|
137
133
|
}
|
|
138
134
|
|
|
@@ -158,6 +154,7 @@ export class BST<
|
|
|
158
154
|
override createTree(options?: BSTOptions<K, V, R>): TREE {
|
|
159
155
|
return new BST<K, V, R, NODE, TREE>([], {
|
|
160
156
|
iterationType: this.iterationType,
|
|
157
|
+
isMapMode: this._isMapMode,
|
|
161
158
|
comparator: this._comparator,
|
|
162
159
|
toEntryFn: this._toEntryFn,
|
|
163
160
|
...options
|
|
@@ -166,18 +163,20 @@ export class BST<
|
|
|
166
163
|
|
|
167
164
|
/**
|
|
168
165
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
169
|
-
* @param {
|
|
170
|
-
* type R or
|
|
166
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
167
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
171
168
|
* element.
|
|
172
169
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
173
170
|
* value associated with a key in a key-value pair.
|
|
174
171
|
* @returns either a NODE object or undefined.
|
|
175
172
|
*/
|
|
176
|
-
override
|
|
177
|
-
|
|
173
|
+
override keyValueNodeEntryRawToNodeAndValue(
|
|
174
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
178
175
|
value?: V
|
|
179
|
-
):
|
|
180
|
-
|
|
176
|
+
): [OptNode<NODE>, V | undefined] {
|
|
177
|
+
const [node, tValue] = super.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
178
|
+
if (node === null) return [undefined, undefined];
|
|
179
|
+
return [node, tValue ?? value];
|
|
181
180
|
}
|
|
182
181
|
|
|
183
182
|
/**
|
|
@@ -186,8 +185,8 @@ export class BST<
|
|
|
186
185
|
*
|
|
187
186
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
188
187
|
* it doesn't exist.
|
|
189
|
-
* @param {
|
|
190
|
-
* `
|
|
188
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
189
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
191
190
|
* entry, or raw element that needs to be ensured in the tree.
|
|
192
191
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
193
192
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -196,21 +195,21 @@ export class BST<
|
|
|
196
195
|
* not be ensured.
|
|
197
196
|
*/
|
|
198
197
|
override ensureNode(
|
|
199
|
-
|
|
198
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
200
199
|
iterationType: IterationType = this.iterationType
|
|
201
|
-
):
|
|
202
|
-
return super.ensureNode(
|
|
200
|
+
): OptNode<NODE> {
|
|
201
|
+
return super.ensureNode(keyNodeEntryOrRaw, iterationType) ?? undefined;
|
|
203
202
|
}
|
|
204
203
|
|
|
205
204
|
/**
|
|
206
205
|
* The function checks if the input is an instance of the BSTNode class.
|
|
207
|
-
* @param {
|
|
208
|
-
* `
|
|
209
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
206
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
207
|
+
* `keyNodeEntryOrRaw` can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
208
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeEntryOrRaw` is
|
|
210
209
|
* an instance of the `BSTNode` class.
|
|
211
210
|
*/
|
|
212
|
-
override isNode(
|
|
213
|
-
return
|
|
211
|
+
override isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE {
|
|
212
|
+
return keyNodeEntryOrRaw instanceof BSTNode;
|
|
214
213
|
}
|
|
215
214
|
|
|
216
215
|
/**
|
|
@@ -230,18 +229,19 @@ export class BST<
|
|
|
230
229
|
* Space Complexity: O(1)
|
|
231
230
|
*
|
|
232
231
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
233
|
-
* @param {
|
|
234
|
-
* `
|
|
232
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
233
|
+
* `keyNodeEntryOrRaw` can accept a value of type `R` or `BTNRep<K, V, NODE>`.
|
|
235
234
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
236
235
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
237
236
|
* @returns a boolean value.
|
|
238
237
|
*/
|
|
239
|
-
override add(
|
|
240
|
-
const newNode = this.
|
|
238
|
+
override add(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): boolean {
|
|
239
|
+
const [newNode, newValue] = this.keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
241
240
|
if (newNode === undefined) return false;
|
|
242
241
|
|
|
243
242
|
if (this._root === undefined) {
|
|
244
243
|
this._setRoot(newNode);
|
|
244
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
245
245
|
this._size++;
|
|
246
246
|
return true;
|
|
247
247
|
}
|
|
@@ -254,6 +254,7 @@ export class BST<
|
|
|
254
254
|
} else if (this.comparator(current.key, newNode.key) > 0) {
|
|
255
255
|
if (current.left === undefined) {
|
|
256
256
|
current.left = newNode;
|
|
257
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
257
258
|
this._size++;
|
|
258
259
|
return true;
|
|
259
260
|
}
|
|
@@ -261,6 +262,7 @@ export class BST<
|
|
|
261
262
|
} else {
|
|
262
263
|
if (current.right === undefined) {
|
|
263
264
|
current.right = newNode;
|
|
265
|
+
if (this._isMapMode) this._setValue(newNode?.key, newValue);
|
|
264
266
|
this._size++;
|
|
265
267
|
return true;
|
|
266
268
|
}
|
|
@@ -277,7 +279,7 @@ export class BST<
|
|
|
277
279
|
*
|
|
278
280
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
279
281
|
* an array indicating whether each key or node was successfully inserted.
|
|
280
|
-
* @param
|
|
282
|
+
* @param keysNodesEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
281
283
|
* elements to be added to the data structure.
|
|
282
284
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
283
285
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -293,7 +295,7 @@ export class BST<
|
|
|
293
295
|
* successfully inserted into the data structure.
|
|
294
296
|
*/
|
|
295
297
|
override addMany(
|
|
296
|
-
|
|
298
|
+
keysNodesEntriesOrRaws: Iterable<R | BTNRep<K, V, NODE>>,
|
|
297
299
|
values?: Iterable<V | undefined>,
|
|
298
300
|
isBalanceAdd = true,
|
|
299
301
|
iterationType: IterationType = this.iterationType
|
|
@@ -307,7 +309,7 @@ export class BST<
|
|
|
307
309
|
}
|
|
308
310
|
|
|
309
311
|
if (!isBalanceAdd) {
|
|
310
|
-
for (const kve of
|
|
312
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
311
313
|
const value = valuesIterator?.next().value;
|
|
312
314
|
inserted.push(this.add(kve, value));
|
|
313
315
|
}
|
|
@@ -315,18 +317,18 @@ export class BST<
|
|
|
315
317
|
}
|
|
316
318
|
|
|
317
319
|
const realBTNExemplars: {
|
|
318
|
-
key: R |
|
|
320
|
+
key: R | BTNRep<K, V, NODE>;
|
|
319
321
|
value: V | undefined;
|
|
320
322
|
orgIndex: number;
|
|
321
323
|
}[] = [];
|
|
322
324
|
|
|
323
325
|
let i = 0;
|
|
324
|
-
for (const kve of
|
|
326
|
+
for (const kve of keysNodesEntriesOrRaws) {
|
|
325
327
|
realBTNExemplars.push({ key: kve, value: valuesIterator?.next().value, orgIndex: i });
|
|
326
328
|
i++;
|
|
327
329
|
}
|
|
328
330
|
|
|
329
|
-
let sorted: { key: R |
|
|
331
|
+
let sorted: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[] = [];
|
|
330
332
|
|
|
331
333
|
sorted = realBTNExemplars.sort(({ key: a }, { key: b }) => {
|
|
332
334
|
let keyA: K | undefined | null, keyB: K | undefined | null;
|
|
@@ -352,7 +354,7 @@ export class BST<
|
|
|
352
354
|
return 0;
|
|
353
355
|
});
|
|
354
356
|
|
|
355
|
-
const _dfs = (arr: { key: R |
|
|
357
|
+
const _dfs = (arr: { key: R | BTNRep<K, V, NODE>; value: V | undefined; orgIndex: number }[]) => {
|
|
356
358
|
if (arr.length === 0) return;
|
|
357
359
|
|
|
358
360
|
const mid = Math.floor((arr.length - 1) / 2);
|
|
@@ -394,35 +396,35 @@ export class BST<
|
|
|
394
396
|
* Space Complexity: O(k + log n)
|
|
395
397
|
*
|
|
396
398
|
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
397
|
-
* given
|
|
398
|
-
* @param {
|
|
399
|
+
* given keyNodeEntryRawOrPredicate and iteration type.
|
|
400
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
399
401
|
* parameter in the `getNodes` method is used to filter the nodes that will be returned. It can be a
|
|
400
|
-
* key, a node, an entry, or a custom
|
|
402
|
+
* key, a node, an entry, or a custom keyNodeEntryRawOrPredicate function that determines whether a node should be
|
|
401
403
|
* included in the result.
|
|
402
404
|
* @param [onlyOne=false] - The `onlyOne` parameter in the `getNodes` method is a boolean flag that
|
|
403
|
-
* determines whether to return only the first node that matches the
|
|
404
|
-
* that match the
|
|
405
|
+
* determines whether to return only the first node that matches the keyNodeEntryRawOrPredicate (`true`) or all nodes
|
|
406
|
+
* that match the keyNodeEntryRawOrPredicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
405
407
|
* and
|
|
406
|
-
* @param {
|
|
408
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter in the
|
|
407
409
|
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
408
|
-
* nodes that match a given
|
|
410
|
+
* nodes that match a given keyNodeEntryRawOrPredicate. It represents the root node of the subtree where the search
|
|
409
411
|
* should begin. If not explicitly provided, the default value for `begin
|
|
410
412
|
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
411
413
|
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
412
414
|
* have two possible values:
|
|
413
|
-
* @returns The `getNodes` method returns an array of nodes that satisfy the given
|
|
415
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given keyNodeEntryRawOrPredicate.
|
|
414
416
|
*/
|
|
415
417
|
override getNodes(
|
|
416
|
-
|
|
418
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
417
419
|
onlyOne = false,
|
|
418
|
-
|
|
420
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
419
421
|
iterationType: IterationType = this.iterationType
|
|
420
422
|
): NODE[] {
|
|
421
|
-
if (
|
|
422
|
-
if (
|
|
423
|
-
|
|
424
|
-
if (!
|
|
425
|
-
const callback = this._ensurePredicate(
|
|
423
|
+
if (keyNodeEntryRawOrPredicate === undefined) return [];
|
|
424
|
+
if (keyNodeEntryRawOrPredicate === null) return [];
|
|
425
|
+
startNode = this.ensureNode(startNode);
|
|
426
|
+
if (!startNode) return [];
|
|
427
|
+
const callback = this._ensurePredicate(keyNodeEntryRawOrPredicate);
|
|
426
428
|
const ans: NODE[] = [];
|
|
427
429
|
|
|
428
430
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -433,27 +435,53 @@ export class BST<
|
|
|
433
435
|
}
|
|
434
436
|
|
|
435
437
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
436
|
-
if (this.
|
|
437
|
-
|
|
438
|
-
if (
|
|
438
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
439
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
440
|
+
if (
|
|
441
|
+
this.isRealNode(cur.left) &&
|
|
442
|
+
benchmarkKey !== null &&
|
|
443
|
+
benchmarkKey !== undefined &&
|
|
444
|
+
this.comparator(cur.key, benchmarkKey) > 0
|
|
445
|
+
)
|
|
446
|
+
dfs(cur.left);
|
|
447
|
+
if (
|
|
448
|
+
this.isRealNode(cur.right) &&
|
|
449
|
+
benchmarkKey !== null &&
|
|
450
|
+
benchmarkKey !== undefined &&
|
|
451
|
+
this.comparator(cur.key, benchmarkKey) < 0
|
|
452
|
+
)
|
|
453
|
+
dfs(cur.right);
|
|
439
454
|
} else {
|
|
440
455
|
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
441
456
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
442
457
|
}
|
|
443
458
|
};
|
|
444
459
|
|
|
445
|
-
dfs(
|
|
460
|
+
dfs(startNode);
|
|
446
461
|
} else {
|
|
447
|
-
const stack = [
|
|
462
|
+
const stack = [startNode];
|
|
448
463
|
while (stack.length > 0) {
|
|
449
464
|
const cur = stack.pop()!;
|
|
450
465
|
if (callback(cur)) {
|
|
451
466
|
ans.push(cur);
|
|
452
467
|
if (onlyOne) return ans;
|
|
453
468
|
}
|
|
454
|
-
if (this.
|
|
455
|
-
|
|
456
|
-
if (
|
|
469
|
+
if (!this._isPredicate(keyNodeEntryRawOrPredicate)) {
|
|
470
|
+
const benchmarkKey = this._getKey(keyNodeEntryRawOrPredicate);
|
|
471
|
+
if (
|
|
472
|
+
this.isRealNode(cur.right) &&
|
|
473
|
+
benchmarkKey !== null &&
|
|
474
|
+
benchmarkKey !== undefined &&
|
|
475
|
+
this.comparator(cur.key, benchmarkKey) < 0
|
|
476
|
+
)
|
|
477
|
+
stack.push(cur.right);
|
|
478
|
+
if (
|
|
479
|
+
this.isRealNode(cur.left) &&
|
|
480
|
+
benchmarkKey !== null &&
|
|
481
|
+
benchmarkKey !== undefined &&
|
|
482
|
+
this.comparator(cur.key, benchmarkKey) > 0
|
|
483
|
+
)
|
|
484
|
+
stack.push(cur.left);
|
|
457
485
|
} else {
|
|
458
486
|
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
459
487
|
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
@@ -468,10 +496,10 @@ export class BST<
|
|
|
468
496
|
* Time Complexity: O(log n)
|
|
469
497
|
* Space Complexity: O(1)
|
|
470
498
|
*
|
|
471
|
-
* This function retrieves a node based on a given
|
|
472
|
-
* @param {
|
|
473
|
-
* parameter can be of type `
|
|
474
|
-
* @param {R |
|
|
499
|
+
* This function retrieves a node based on a given keyNodeEntryRawOrPredicate within a binary search tree structure.
|
|
500
|
+
* @param {BTNRep<K, V, NODE> | R | NodePredicate<NODE>} keyNodeEntryRawOrPredicate - The `keyNodeEntryRawOrPredicate`
|
|
501
|
+
* parameter can be of type `BTNRep<K, V, NODE>`, `R`, or `NodePredicate<NODE>`.
|
|
502
|
+
* @param {R | BSTNOptKeyOrNode<K, NODE>} startNode - The `startNode` parameter in the `getNode` method
|
|
475
503
|
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
476
504
|
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
477
505
|
* node of the binary search tree.
|
|
@@ -479,17 +507,17 @@ export class BST<
|
|
|
479
507
|
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
480
508
|
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
481
509
|
* no value is provided when calling the method.
|
|
482
|
-
* @returns The `getNode` method is returning an optional binary search tree node (`
|
|
483
|
-
* It is using the `getNodes` method to find the node based on the provided
|
|
484
|
-
* the specified root node (`
|
|
510
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptNode<NODE>`).
|
|
511
|
+
* It is using the `getNodes` method to find the node based on the provided keyNodeEntryRawOrPredicate, beginning at
|
|
512
|
+
* the specified root node (`startNode`) and using the specified iteration type. The method then
|
|
485
513
|
* returns the first node found or `undefined` if no node is found.
|
|
486
514
|
*/
|
|
487
515
|
override getNode(
|
|
488
|
-
|
|
489
|
-
|
|
516
|
+
keyNodeEntryRawOrPredicate: BTNRep<K, V, NODE> | R | NodePredicate<NODE>,
|
|
517
|
+
startNode: R | BSTNOptKeyOrNode<K, NODE> = this._root,
|
|
490
518
|
iterationType: IterationType = this.iterationType
|
|
491
|
-
):
|
|
492
|
-
return this.getNodes(
|
|
519
|
+
): OptNode<NODE> {
|
|
520
|
+
return this.getNodes(keyNodeEntryRawOrPredicate, true, startNode, iterationType)[0] ?? undefined;
|
|
493
521
|
}
|
|
494
522
|
|
|
495
523
|
/**
|
|
@@ -505,7 +533,7 @@ export class BST<
|
|
|
505
533
|
* It has a default value of `'ITERATIVE'`.
|
|
506
534
|
* @returns The method is returning a NODE object or undefined.
|
|
507
535
|
*/
|
|
508
|
-
override getNodeByKey(key: K, iterationType: IterationType = this.iterationType):
|
|
536
|
+
override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptNode<NODE> {
|
|
509
537
|
return this.getNode(key, this._root, iterationType);
|
|
510
538
|
}
|
|
511
539
|
|
|
@@ -517,11 +545,11 @@ export class BST<
|
|
|
517
545
|
* the callback function.
|
|
518
546
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
519
547
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
520
|
-
* `this.
|
|
548
|
+
* `this._DEFAULT_NODE_CALLBACK`. The type `C` represents the type of the callback function.
|
|
521
549
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
522
550
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
523
551
|
* take one of the following values:
|
|
524
|
-
* @param {
|
|
552
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
525
553
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
526
554
|
* node entry. If not specified, the default value is the root of the tree.
|
|
527
555
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -529,13 +557,13 @@ export class BST<
|
|
|
529
557
|
* following values:
|
|
530
558
|
* @returns The method is returning an array of the return type of the callback function.
|
|
531
559
|
*/
|
|
532
|
-
override dfs<C extends
|
|
533
|
-
callback: C = this.
|
|
560
|
+
override dfs<C extends NodeCallback<NODE>>(
|
|
561
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
534
562
|
pattern: DFSOrderPattern = 'IN',
|
|
535
|
-
|
|
563
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
536
564
|
iterationType: IterationType = this.iterationType
|
|
537
565
|
): ReturnType<C>[] {
|
|
538
|
-
return super.dfs(callback, pattern,
|
|
566
|
+
return super.dfs(callback, pattern, startNode, iterationType);
|
|
539
567
|
}
|
|
540
568
|
|
|
541
569
|
/**
|
|
@@ -547,7 +575,7 @@ export class BST<
|
|
|
547
575
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
548
576
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
549
577
|
* node being visited, and it can return a value of any type.
|
|
550
|
-
* @param {
|
|
578
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
551
579
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
552
580
|
* object. If no value is provided, the default value is the root of the tree.
|
|
553
581
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -555,12 +583,12 @@ export class BST<
|
|
|
555
583
|
* the following values:
|
|
556
584
|
* @returns an array of the return type of the callback function.
|
|
557
585
|
*/
|
|
558
|
-
override bfs<C extends
|
|
559
|
-
callback: C = this.
|
|
560
|
-
|
|
586
|
+
override bfs<C extends NodeCallback<NODE>>(
|
|
587
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
588
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
561
589
|
iterationType: IterationType = this.iterationType
|
|
562
590
|
): ReturnType<C>[] {
|
|
563
|
-
return super.bfs(callback,
|
|
591
|
+
return super.bfs(callback, startNode, iterationType, false);
|
|
564
592
|
}
|
|
565
593
|
|
|
566
594
|
/**
|
|
@@ -570,9 +598,9 @@ export class BST<
|
|
|
570
598
|
* The function overrides the listLevels method from the superclass and returns an array of arrays
|
|
571
599
|
* containing the results of the callback function applied to each level of the tree.
|
|
572
600
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
573
|
-
* `
|
|
601
|
+
* `NodeCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
574
602
|
* tree during the iteration process.
|
|
575
|
-
* @param {
|
|
603
|
+
* @param {BTNRep<K, V, NODE> | R} startNode - The `startNode` parameter is the starting
|
|
576
604
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
577
605
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
578
606
|
* value is provided, the root of
|
|
@@ -581,12 +609,12 @@ export class BST<
|
|
|
581
609
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
582
610
|
* function.
|
|
583
611
|
*/
|
|
584
|
-
override listLevels<C extends
|
|
585
|
-
callback: C = this.
|
|
586
|
-
|
|
612
|
+
override listLevels<C extends NodeCallback<NODE>>(
|
|
613
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
614
|
+
startNode: BTNRep<K, V, NODE> | R = this._root,
|
|
587
615
|
iterationType: IterationType = this.iterationType
|
|
588
616
|
): ReturnType<C>[][] {
|
|
589
|
-
return super.listLevels(callback,
|
|
617
|
+
return super.listLevels(callback, startNode, iterationType, false);
|
|
590
618
|
}
|
|
591
619
|
|
|
592
620
|
/**
|
|
@@ -601,7 +629,7 @@ export class BST<
|
|
|
601
629
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
602
630
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
603
631
|
* 0, or 1, where:
|
|
604
|
-
* @param {
|
|
632
|
+
* @param {BTNRep<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
605
633
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
606
634
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
607
635
|
* `targetNode` is provided,
|
|
@@ -610,14 +638,14 @@ export class BST<
|
|
|
610
638
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
611
639
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
612
640
|
*/
|
|
613
|
-
lesserOrGreaterTraverse<C extends
|
|
614
|
-
callback: C = this.
|
|
641
|
+
lesserOrGreaterTraverse<C extends NodeCallback<NODE>>(
|
|
642
|
+
callback: C = this._DEFAULT_NODE_CALLBACK as C,
|
|
615
643
|
lesserOrGreater: CP = -1,
|
|
616
|
-
targetNode:
|
|
644
|
+
targetNode: BTNRep<K, V, NODE> | R = this._root,
|
|
617
645
|
iterationType: IterationType = this.iterationType
|
|
618
646
|
): ReturnType<C>[] {
|
|
619
647
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
620
|
-
const ans: ReturnType<
|
|
648
|
+
const ans: ReturnType<NodeCallback<NODE>>[] = [];
|
|
621
649
|
if (!this._root) return ans;
|
|
622
650
|
if (!targetNodeEnsured) return ans;
|
|
623
651
|
|
|
@@ -665,7 +693,7 @@ export class BST<
|
|
|
665
693
|
perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
|
|
666
694
|
const sorted = this.dfs(node => node, 'IN'),
|
|
667
695
|
n = sorted.length;
|
|
668
|
-
this.
|
|
696
|
+
this._clearNodes();
|
|
669
697
|
|
|
670
698
|
if (sorted.length < 1) return false;
|
|
671
699
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -673,7 +701,8 @@ export class BST<
|
|
|
673
701
|
if (l > r) return;
|
|
674
702
|
const m = l + Math.floor((r - l) / 2);
|
|
675
703
|
const midNode = sorted[m];
|
|
676
|
-
this.add(
|
|
704
|
+
if (this._isMapMode) this.add(midNode.key);
|
|
705
|
+
else this.add([midNode.key, midNode.value]);
|
|
677
706
|
buildBalanceBST(l, m - 1);
|
|
678
707
|
buildBalanceBST(m + 1, r);
|
|
679
708
|
};
|
|
@@ -689,7 +718,8 @@ export class BST<
|
|
|
689
718
|
if (l <= r) {
|
|
690
719
|
const m = l + Math.floor((r - l) / 2);
|
|
691
720
|
const midNode = sorted[m];
|
|
692
|
-
this.add(
|
|
721
|
+
if (this._isMapMode) this.add(midNode.key);
|
|
722
|
+
else this.add([midNode.key, midNode.value]);
|
|
693
723
|
stack.push([m + 1, r]);
|
|
694
724
|
stack.push([l, m - 1]);
|
|
695
725
|
}
|
|
@@ -717,7 +747,7 @@ export class BST<
|
|
|
717
747
|
let balanced = true;
|
|
718
748
|
|
|
719
749
|
if (iterationType === 'RECURSIVE') {
|
|
720
|
-
const _height = (cur:
|
|
750
|
+
const _height = (cur: OptNode<NODE>): number => {
|
|
721
751
|
if (!cur) return 0;
|
|
722
752
|
const leftHeight = _height(cur.left),
|
|
723
753
|
rightHeight = _height(cur.right);
|
|
@@ -727,8 +757,8 @@ export class BST<
|
|
|
727
757
|
_height(this._root);
|
|
728
758
|
} else {
|
|
729
759
|
const stack: NODE[] = [];
|
|
730
|
-
let node:
|
|
731
|
-
last:
|
|
760
|
+
let node: OptNode<NODE> = this._root,
|
|
761
|
+
last: OptNode<NODE> = undefined;
|
|
732
762
|
const depths: Map<NODE, number> = new Map();
|
|
733
763
|
|
|
734
764
|
while (stack.length > 0 || node) {
|
|
@@ -779,9 +809,9 @@ export class BST<
|
|
|
779
809
|
/**
|
|
780
810
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
781
811
|
* root.
|
|
782
|
-
* @param {
|
|
812
|
+
* @param {OptNode<NODE>} v - v is a parameter of type NODE or undefined.
|
|
783
813
|
*/
|
|
784
|
-
protected override _setRoot(v:
|
|
814
|
+
protected override _setRoot(v: OptNode<NODE>) {
|
|
785
815
|
if (v) {
|
|
786
816
|
v.parent = undefined;
|
|
787
817
|
}
|