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