avl-tree-typed 1.52.6 → 1.52.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants/index.d.ts +4 -0
- package/dist/constants/index.js +8 -0
- package/dist/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-element-base.js +10 -1
- package/dist/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/data-structures/binary-tree/binary-tree.js +668 -597
- package/dist/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/data-structures/binary-tree/bst.js +115 -113
- package/dist/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/data-structures/graph/abstract-graph.js +7 -4
- package/dist/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/data-structures/graph/directed-graph.js +4 -2
- package/dist/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/data-structures/hash/hash-map.js +1 -1
- package/dist/data-structures/heap/heap.js +3 -3
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/data-structures/matrix/navigator.js +4 -2
- package/dist/data-structures/queue/deque.d.ts +3 -3
- package/dist/data-structures/queue/deque.js +29 -29
- package/dist/data-structures/queue/queue.d.ts +1 -1
- package/dist/data-structures/stack/stack.d.ts +2 -2
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/package.json +2 -2
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +697 -725
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +1 -0
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
@@ -11,7 +11,9 @@ import type {
|
|
|
11
11
|
BSTNodeNested,
|
|
12
12
|
BSTOptions,
|
|
13
13
|
BTNCallback,
|
|
14
|
+
BTNEntry,
|
|
14
15
|
BTNKeyOrNodeOrEntry,
|
|
16
|
+
BTNPredicate,
|
|
15
17
|
BTNPureKeyOrNodeOrEntry,
|
|
16
18
|
Comparator,
|
|
17
19
|
CP,
|
|
@@ -19,7 +21,6 @@ import type {
|
|
|
19
21
|
IterationType,
|
|
20
22
|
OptBSTN
|
|
21
23
|
} from '../../types';
|
|
22
|
-
import { BTNEntry } from '../../types';
|
|
23
24
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
24
25
|
import { IBinaryTree } from '../../interfaces';
|
|
25
26
|
import { Queue } from '../queue';
|
|
@@ -106,14 +107,14 @@ export class BST<
|
|
|
106
107
|
{
|
|
107
108
|
/**
|
|
108
109
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
109
|
-
* @param
|
|
110
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
110
111
|
* iterable that can contain either keys, nodes, entries, or raw elements. These elements will be
|
|
111
112
|
* added to the binary search tree during the construction of the object.
|
|
112
113
|
* @param [options] - An optional object that contains additional options for the Binary Search Tree.
|
|
113
114
|
* It can include a comparator function that defines the order of the elements in the tree.
|
|
114
115
|
*/
|
|
115
116
|
constructor(
|
|
116
|
-
|
|
117
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
117
118
|
options?: BSTOptions<K, V, R>
|
|
118
119
|
) {
|
|
119
120
|
super([], options);
|
|
@@ -123,7 +124,7 @@ export class BST<
|
|
|
123
124
|
if (comparator) this._comparator = comparator;
|
|
124
125
|
}
|
|
125
126
|
|
|
126
|
-
if (
|
|
127
|
+
if (keysOrNodesOrEntriesOrRaws) this.addMany(keysOrNodesOrEntriesOrRaws);
|
|
127
128
|
}
|
|
128
129
|
|
|
129
130
|
protected override _root?: NODE = undefined;
|
|
@@ -155,17 +156,18 @@ export class BST<
|
|
|
155
156
|
* following properties:
|
|
156
157
|
* @returns a new instance of the BST class with the provided options.
|
|
157
158
|
*/
|
|
158
|
-
override createTree(options?:
|
|
159
|
+
override createTree(options?: BSTOptions<K, V, R>): TREE {
|
|
159
160
|
return new BST<K, V, R, NODE, TREE>([], {
|
|
160
161
|
iterationType: this.iterationType,
|
|
161
|
-
comparator: this.
|
|
162
|
+
comparator: this._comparator,
|
|
163
|
+
toEntryFn: this._toEntryFn,
|
|
162
164
|
...options
|
|
163
165
|
}) as TREE;
|
|
164
166
|
}
|
|
165
167
|
|
|
166
168
|
/**
|
|
167
169
|
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
168
|
-
* @param {
|
|
170
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - A variable that can be of
|
|
169
171
|
* type R or BTNKeyOrNodeOrEntry<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
170
172
|
* element.
|
|
171
173
|
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
@@ -173,10 +175,10 @@ export class BST<
|
|
|
173
175
|
* @returns either a NODE object or undefined.
|
|
174
176
|
*/
|
|
175
177
|
override keyValueOrEntryOrRawElementToNode(
|
|
176
|
-
|
|
178
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
177
179
|
value?: V
|
|
178
180
|
): OptBSTN<NODE> {
|
|
179
|
-
return super.keyValueOrEntryOrRawElementToNode(
|
|
181
|
+
return super.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value) ?? undefined;
|
|
180
182
|
}
|
|
181
183
|
|
|
182
184
|
/**
|
|
@@ -185,8 +187,8 @@ export class BST<
|
|
|
185
187
|
*
|
|
186
188
|
* The function ensures the existence of a node in a data structure and returns it, or undefined if
|
|
187
189
|
* it doesn't exist.
|
|
188
|
-
* @param {
|
|
189
|
-
* `
|
|
190
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
191
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R`, which represents the key, node,
|
|
190
192
|
* entry, or raw element that needs to be ensured in the tree.
|
|
191
193
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter is an optional
|
|
192
194
|
* parameter that specifies the type of iteration to be used when ensuring a node. It has a default
|
|
@@ -195,25 +197,31 @@ export class BST<
|
|
|
195
197
|
* not be ensured.
|
|
196
198
|
*/
|
|
197
199
|
override ensureNode(
|
|
198
|
-
|
|
200
|
+
keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R,
|
|
199
201
|
iterationType: IterationType = this.iterationType
|
|
200
202
|
): OptBSTN<NODE> {
|
|
201
|
-
return super.ensureNode(
|
|
203
|
+
return super.ensureNode(keyOrNodeOrEntryOrRaw, iterationType) ?? undefined;
|
|
202
204
|
}
|
|
203
205
|
|
|
204
206
|
/**
|
|
205
207
|
* The function checks if the input is an instance of the BSTNode class.
|
|
206
|
-
* @param {
|
|
207
|
-
* `
|
|
208
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
208
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
209
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
210
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
209
211
|
* an instance of the `BSTNode` class.
|
|
210
212
|
*/
|
|
211
|
-
override isNode(
|
|
212
|
-
|
|
213
|
-
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
214
|
-
return keyOrNodeOrEntryOrRawElement instanceof BSTNode;
|
|
213
|
+
override isNode(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R): keyOrNodeOrEntryOrRaw is NODE {
|
|
214
|
+
return keyOrNodeOrEntryOrRaw instanceof BSTNode;
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
+
/**
|
|
218
|
+
* The function "override isKey" checks if a key is comparable based on a given comparator.
|
|
219
|
+
* @param {any} key - The `key` parameter is a value that will be checked to determine if it is of
|
|
220
|
+
* type `K`.
|
|
221
|
+
* @returns The `override isKey(key: any): key is K` function is returning a boolean value based on
|
|
222
|
+
* the result of the `isComparable` function with the condition `this.comparator !==
|
|
223
|
+
* this._DEFAULT_COMPARATOR`.
|
|
224
|
+
*/
|
|
217
225
|
override isKey(key: any): key is K {
|
|
218
226
|
return isComparable(key, this.comparator !== this._DEFAULT_COMPARATOR);
|
|
219
227
|
}
|
|
@@ -223,23 +231,23 @@ export class BST<
|
|
|
223
231
|
* Space Complexity: O(1)
|
|
224
232
|
*
|
|
225
233
|
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value.
|
|
226
|
-
* @param {
|
|
227
|
-
* `
|
|
234
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
235
|
+
* `keyOrNodeOrEntryOrRaw` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
228
236
|
* @param {V} [value] - The `value` parameter is an optional value that can be associated with the
|
|
229
237
|
* key in the binary search tree. If provided, it will be stored in the node along with the key.
|
|
230
238
|
* @returns a boolean value.
|
|
231
239
|
*/
|
|
232
|
-
override add(
|
|
233
|
-
const newNode = this.keyValueOrEntryOrRawElementToNode(
|
|
240
|
+
override add(keyOrNodeOrEntryOrRaw: BTNKeyOrNodeOrEntry<K, V, NODE> | R, value?: V): boolean {
|
|
241
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRaw, value);
|
|
234
242
|
if (newNode === undefined) return false;
|
|
235
243
|
|
|
236
|
-
if (this.
|
|
244
|
+
if (this._root === undefined) {
|
|
237
245
|
this._setRoot(newNode);
|
|
238
246
|
this._size++;
|
|
239
247
|
return true;
|
|
240
248
|
}
|
|
241
249
|
|
|
242
|
-
let current = this.
|
|
250
|
+
let current = this._root;
|
|
243
251
|
while (current !== undefined) {
|
|
244
252
|
if (this.comparator(current.key, newNode.key) === 0) {
|
|
245
253
|
this._replaceNode(current, newNode);
|
|
@@ -270,7 +278,7 @@ export class BST<
|
|
|
270
278
|
*
|
|
271
279
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure and returns
|
|
272
280
|
* an array indicating whether each key or node was successfully inserted.
|
|
273
|
-
* @param
|
|
281
|
+
* @param keysOrNodesOrEntriesOrRaws - An iterable containing keys, nodes, entries, or raw
|
|
274
282
|
* elements to be added to the data structure.
|
|
275
283
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
276
284
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
@@ -286,7 +294,7 @@ export class BST<
|
|
|
286
294
|
* successfully inserted into the data structure.
|
|
287
295
|
*/
|
|
288
296
|
override addMany(
|
|
289
|
-
|
|
297
|
+
keysOrNodesOrEntriesOrRaws: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>>,
|
|
290
298
|
values?: Iterable<V | undefined>,
|
|
291
299
|
isBalanceAdd = true,
|
|
292
300
|
iterationType: IterationType = this.iterationType
|
|
@@ -300,7 +308,7 @@ export class BST<
|
|
|
300
308
|
}
|
|
301
309
|
|
|
302
310
|
if (!isBalanceAdd) {
|
|
303
|
-
for (const kve of
|
|
311
|
+
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
304
312
|
const value = valuesIterator?.next().value;
|
|
305
313
|
const nn = this.add(kve, value);
|
|
306
314
|
inserted.push(nn);
|
|
@@ -311,14 +319,14 @@ export class BST<
|
|
|
311
319
|
const realBTNExemplars: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
312
320
|
|
|
313
321
|
const isRealBTNExemplar = (
|
|
314
|
-
kve:
|
|
322
|
+
kve: BTNKeyOrNodeOrEntry<K, V, NODE> | R
|
|
315
323
|
): kve is BTNPureKeyOrNodeOrEntry<K, V, NODE> => {
|
|
316
324
|
if (kve === undefined || kve === null) return false;
|
|
317
325
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
318
326
|
};
|
|
319
327
|
|
|
320
|
-
for (const kve of
|
|
321
|
-
isRealBTNExemplar(kve)
|
|
328
|
+
for (const kve of keysOrNodesOrEntriesOrRaws) {
|
|
329
|
+
if (isRealBTNExemplar(kve)) realBTNExemplars.push(kve);
|
|
322
330
|
}
|
|
323
331
|
|
|
324
332
|
let sorted: (R | BTNPureKeyOrNodeOrEntry<K, V, NODE>)[] = [];
|
|
@@ -327,16 +335,16 @@ export class BST<
|
|
|
327
335
|
let keyA: K | undefined | null, keyB: K | undefined | null;
|
|
328
336
|
if (this.isEntry(a)) keyA = a[0];
|
|
329
337
|
else if (this.isRealNode(a)) keyA = a.key;
|
|
330
|
-
else if (this.
|
|
331
|
-
keyA = this.
|
|
338
|
+
else if (this._toEntryFn) {
|
|
339
|
+
keyA = this._toEntryFn(a as R)[0];
|
|
332
340
|
} else {
|
|
333
341
|
keyA = a as K;
|
|
334
342
|
}
|
|
335
343
|
|
|
336
344
|
if (this.isEntry(b)) keyB = b[0];
|
|
337
345
|
else if (this.isRealNode(b)) keyB = b.key;
|
|
338
|
-
else if (this.
|
|
339
|
-
keyB = this.
|
|
346
|
+
else if (this._toEntryFn) {
|
|
347
|
+
keyB = this._toEntryFn(b as R)[0];
|
|
340
348
|
} else {
|
|
341
349
|
keyB = b as K;
|
|
342
350
|
}
|
|
@@ -388,54 +396,52 @@ export class BST<
|
|
|
388
396
|
* Time Complexity: O(log n)
|
|
389
397
|
* Space Complexity: O(k + log n)
|
|
390
398
|
*
|
|
391
|
-
* The `getNodes`
|
|
392
|
-
*
|
|
393
|
-
* @param {
|
|
394
|
-
*
|
|
395
|
-
* function
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
* @param {IterationType} iterationType - The `iterationType` parameter
|
|
406
|
-
* iteration to be performed
|
|
407
|
-
*
|
|
399
|
+
* The function `getNodes` in TypeScript overrides the base class method to retrieve nodes based on a
|
|
400
|
+
* given predicate and iteration type.
|
|
401
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
402
|
+
* 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 predicate function that determines whether a node should be
|
|
404
|
+
* included in the result.
|
|
405
|
+
* @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 predicate (`true`) or all nodes
|
|
407
|
+
* that match the predicate (`false`). If `onlyOne` is set to `true`, the method will stop iterating
|
|
408
|
+
* and
|
|
409
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter in the
|
|
410
|
+
* `getNodes` method is used to specify the starting point for traversing the tree when searching for
|
|
411
|
+
* nodes that match a given predicate. It represents the root node of the subtree where the search
|
|
412
|
+
* should begin. If not explicitly provided, the default value for `begin
|
|
413
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNodes` method
|
|
414
|
+
* specifies the type of iteration to be performed when traversing the nodes of a binary tree. It can
|
|
415
|
+
* have two possible values:
|
|
416
|
+
* @returns The `getNodes` method returns an array of nodes that satisfy the given predicate.
|
|
408
417
|
*/
|
|
409
|
-
override getNodes
|
|
410
|
-
|
|
411
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
418
|
+
override getNodes(
|
|
419
|
+
predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
412
420
|
onlyOne = false,
|
|
413
|
-
beginRoot:
|
|
421
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
414
422
|
iterationType: IterationType = this.iterationType
|
|
415
423
|
): NODE[] {
|
|
416
|
-
if (
|
|
417
|
-
if (
|
|
424
|
+
if (predicate === undefined) return [];
|
|
425
|
+
if (predicate === null) return [];
|
|
418
426
|
beginRoot = this.ensureNode(beginRoot);
|
|
419
427
|
if (!beginRoot) return [];
|
|
420
|
-
callback = this.
|
|
428
|
+
const callback = this._ensurePredicate(predicate);
|
|
421
429
|
const ans: NODE[] = [];
|
|
422
430
|
|
|
423
431
|
if (iterationType === 'RECURSIVE') {
|
|
424
432
|
const dfs = (cur: NODE) => {
|
|
425
|
-
|
|
426
|
-
if (callbackResult === identifier) {
|
|
433
|
+
if (callback(cur)) {
|
|
427
434
|
ans.push(cur);
|
|
428
435
|
if (onlyOne) return;
|
|
429
436
|
}
|
|
430
437
|
|
|
431
438
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
if (this.isRealNode(cur.
|
|
435
|
-
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier as K) < 0) dfs(cur.right);
|
|
439
|
+
if (this.isKey(predicate)) {
|
|
440
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) dfs(cur.left);
|
|
441
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) dfs(cur.right);
|
|
436
442
|
} else {
|
|
437
|
-
this.isRealNode(cur.left)
|
|
438
|
-
this.isRealNode(cur.right)
|
|
443
|
+
if (this.isRealNode(cur.left)) dfs(cur.left);
|
|
444
|
+
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
439
445
|
}
|
|
440
446
|
};
|
|
441
447
|
|
|
@@ -444,26 +450,16 @@ export class BST<
|
|
|
444
450
|
const stack = [beginRoot];
|
|
445
451
|
while (stack.length > 0) {
|
|
446
452
|
const cur = stack.pop()!;
|
|
447
|
-
|
|
448
|
-
if (callbackResult === identifier) {
|
|
453
|
+
if (callback(cur)) {
|
|
449
454
|
ans.push(cur);
|
|
450
455
|
if (onlyOne) return ans;
|
|
451
456
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
if (this.isRealNode(cur.
|
|
455
|
-
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier as K) > 0) stack.push(cur.left);
|
|
456
|
-
|
|
457
|
-
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
458
|
-
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
459
|
-
|
|
460
|
-
// // @ts-ignore
|
|
461
|
-
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
462
|
-
// // @ts-ignore
|
|
463
|
-
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
457
|
+
if (this.isKey(predicate)) {
|
|
458
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, predicate) < 0) stack.push(cur.right);
|
|
459
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, predicate) > 0) stack.push(cur.left);
|
|
464
460
|
} else {
|
|
465
|
-
this.isRealNode(cur.right)
|
|
466
|
-
this.isRealNode(cur.left)
|
|
461
|
+
if (this.isRealNode(cur.right)) stack.push(cur.right);
|
|
462
|
+
if (this.isRealNode(cur.left)) stack.push(cur.left);
|
|
467
463
|
}
|
|
468
464
|
}
|
|
469
465
|
}
|
|
@@ -475,30 +471,28 @@ export class BST<
|
|
|
475
471
|
* Time Complexity: O(log n)
|
|
476
472
|
* Space Complexity: O(1)
|
|
477
473
|
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* node
|
|
485
|
-
*
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* @returns The method is returning a NODE object or undefined.
|
|
474
|
+
* This function retrieves a node based on a given predicate within a binary search tree structure.
|
|
475
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
476
|
+
* parameter can be of type `BTNKeyOrNodeOrEntry<K, V, NODE>`, `R`, or `BTNPredicate<NODE>`.
|
|
477
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} beginRoot - The `beginRoot` parameter in the `getNode` method
|
|
478
|
+
* is used to specify the starting point for searching nodes in the binary search tree. If no
|
|
479
|
+
* specific starting point is provided, the default value is set to `this._root`, which is the root
|
|
480
|
+
* node of the binary search tree.
|
|
481
|
+
* @param {IterationType} iterationType - The `iterationType` parameter in the `getNode` method is a
|
|
482
|
+
* parameter that specifies the type of iteration to be used. It has a default value of
|
|
483
|
+
* `this.iterationType`, which means it will use the iteration type defined in the class instance if
|
|
484
|
+
* no value is provided when calling the method.
|
|
485
|
+
* @returns The `getNode` method is returning an optional binary search tree node (`OptBSTN<NODE>`).
|
|
486
|
+
* It is using the `getNodes` method to find the node based on the provided predicate, beginning at
|
|
487
|
+
* the specified root node (`beginRoot`) and using the specified iteration type. The method then
|
|
488
|
+
* returns the first node found or `undefined` if no node is found.
|
|
494
489
|
*/
|
|
495
|
-
override getNode
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
beginRoot: R | BSTNKeyOrNode<K, NODE> = this.root,
|
|
490
|
+
override getNode(
|
|
491
|
+
predicate: BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>,
|
|
492
|
+
beginRoot: R | BSTNKeyOrNode<K, NODE> = this._root,
|
|
499
493
|
iterationType: IterationType = this.iterationType
|
|
500
494
|
): OptBSTN<NODE> {
|
|
501
|
-
return this.getNodes(
|
|
495
|
+
return this.getNodes(predicate, true, beginRoot, iterationType)[0] ?? undefined;
|
|
502
496
|
}
|
|
503
497
|
|
|
504
498
|
/**
|
|
@@ -515,7 +509,7 @@ export class BST<
|
|
|
515
509
|
* @returns The method is returning a NODE object or undefined.
|
|
516
510
|
*/
|
|
517
511
|
override getNodeByKey(key: K, iterationType: IterationType = this.iterationType): OptBSTN<NODE> {
|
|
518
|
-
return this.getNode(key, this.
|
|
512
|
+
return this.getNode(key, this._root, iterationType);
|
|
519
513
|
}
|
|
520
514
|
|
|
521
515
|
/**
|
|
@@ -526,11 +520,11 @@ export class BST<
|
|
|
526
520
|
* the callback function.
|
|
527
521
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
528
522
|
* during the depth-first search traversal. It is an optional parameter and defaults to
|
|
529
|
-
* `this.
|
|
523
|
+
* `this._DEFAULT_BTN_CALLBACK`. The type `C` represents the type of the callback function.
|
|
530
524
|
* @param {DFSOrderPattern} [pattern=IN] - The "pattern" parameter in the code snippet refers to the
|
|
531
525
|
* order in which the Depth-First Search (DFS) algorithm visits the nodes in a tree or graph. It can
|
|
532
526
|
* take one of the following values:
|
|
533
|
-
* @param {
|
|
527
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
534
528
|
* point for the depth-first search traversal. It can be either a root node, a key-value pair, or a
|
|
535
529
|
* node entry. If not specified, the default value is the root of the tree.
|
|
536
530
|
* @param {IterationType} [iterationType=ITERATIVE] - The `iterationType` parameter specifies the
|
|
@@ -539,9 +533,9 @@ export class BST<
|
|
|
539
533
|
* @returns The method is returning an array of the return type of the callback function.
|
|
540
534
|
*/
|
|
541
535
|
override dfs<C extends BTNCallback<NODE>>(
|
|
542
|
-
callback: C = this.
|
|
536
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
543
537
|
pattern: DFSOrderPattern = 'IN',
|
|
544
|
-
beginRoot:
|
|
538
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
545
539
|
iterationType: IterationType = this.iterationType
|
|
546
540
|
): ReturnType<C>[] {
|
|
547
541
|
return super.dfs(callback, pattern, beginRoot, iterationType);
|
|
@@ -556,7 +550,7 @@ export class BST<
|
|
|
556
550
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
557
551
|
* visited during the breadth-first search. It should take a single argument, which is the current
|
|
558
552
|
* node being visited, and it can return a value of any type.
|
|
559
|
-
* @param {
|
|
553
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
560
554
|
* point for the breadth-first search. It can be either a root node, a key-value pair, or an entry
|
|
561
555
|
* object. If no value is provided, the default value is the root of the tree.
|
|
562
556
|
* @param {IterationType} iterationType - The `iterationType` parameter is used to specify the type
|
|
@@ -565,8 +559,8 @@ export class BST<
|
|
|
565
559
|
* @returns an array of the return type of the callback function.
|
|
566
560
|
*/
|
|
567
561
|
override bfs<C extends BTNCallback<NODE>>(
|
|
568
|
-
callback: C = this.
|
|
569
|
-
beginRoot:
|
|
562
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
563
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
570
564
|
iterationType: IterationType = this.iterationType
|
|
571
565
|
): ReturnType<C>[] {
|
|
572
566
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
@@ -581,7 +575,7 @@ export class BST<
|
|
|
581
575
|
* @param {C} callback - The `callback` parameter is a generic type `C` that extends
|
|
582
576
|
* `BTNCallback<NODE>`. It represents a callback function that will be called for each node in the
|
|
583
577
|
* tree during the iteration process.
|
|
584
|
-
* @param {
|
|
578
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} beginRoot - The `beginRoot` parameter is the starting
|
|
585
579
|
* point for listing the levels of the binary tree. It can be either a root node of the tree, a
|
|
586
580
|
* key-value pair representing a node in the tree, or a key representing a node in the tree. If no
|
|
587
581
|
* value is provided, the root of
|
|
@@ -591,8 +585,8 @@ export class BST<
|
|
|
591
585
|
* function.
|
|
592
586
|
*/
|
|
593
587
|
override listLevels<C extends BTNCallback<NODE>>(
|
|
594
|
-
callback: C = this.
|
|
595
|
-
beginRoot:
|
|
588
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
589
|
+
beginRoot: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
596
590
|
iterationType: IterationType = this.iterationType
|
|
597
591
|
): ReturnType<C>[][] {
|
|
598
592
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
@@ -610,7 +604,7 @@ export class BST<
|
|
|
610
604
|
* @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
|
|
611
605
|
* traverse nodes that are lesser, greater, or both than the `targetNode`. It accepts the values -1,
|
|
612
606
|
* 0, or 1, where:
|
|
613
|
-
* @param {
|
|
607
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} targetNode - The `targetNode` parameter is the node in
|
|
614
608
|
* the binary tree that you want to start traversing from. It can be specified either by providing
|
|
615
609
|
* the key of the node, the node itself, or an entry containing the key and value of the node. If no
|
|
616
610
|
* `targetNode` is provided,
|
|
@@ -620,15 +614,15 @@ export class BST<
|
|
|
620
614
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
621
615
|
*/
|
|
622
616
|
lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
|
|
623
|
-
callback: C = this.
|
|
617
|
+
callback: C = this._DEFAULT_BTN_CALLBACK as C,
|
|
624
618
|
lesserOrGreater: CP = -1,
|
|
625
|
-
targetNode:
|
|
619
|
+
targetNode: BTNKeyOrNodeOrEntry<K, V, NODE> | R = this._root,
|
|
626
620
|
iterationType: IterationType = this.iterationType
|
|
627
621
|
): ReturnType<C>[] {
|
|
628
622
|
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
629
623
|
const ans: ReturnType<BTNCallback<NODE>>[] = [];
|
|
624
|
+
if (!this._root) return ans;
|
|
630
625
|
if (!targetNodeEnsured) return ans;
|
|
631
|
-
if (!this.root) return ans;
|
|
632
626
|
|
|
633
627
|
const targetKey = targetNodeEnsured.key;
|
|
634
628
|
|
|
@@ -641,10 +635,10 @@ export class BST<
|
|
|
641
635
|
if (this.isRealNode(cur.right)) dfs(cur.right);
|
|
642
636
|
};
|
|
643
637
|
|
|
644
|
-
dfs(this.
|
|
638
|
+
dfs(this._root);
|
|
645
639
|
return ans;
|
|
646
640
|
} else {
|
|
647
|
-
const queue = new Queue<NODE>([this.
|
|
641
|
+
const queue = new Queue<NODE>([this._root]);
|
|
648
642
|
while (queue.size > 0) {
|
|
649
643
|
const cur = queue.shift();
|
|
650
644
|
if (this.isRealNode(cur)) {
|
|
@@ -721,7 +715,7 @@ export class BST<
|
|
|
721
715
|
* @returns a boolean value.
|
|
722
716
|
*/
|
|
723
717
|
isAVLBalanced(iterationType: IterationType = this.iterationType): boolean {
|
|
724
|
-
if (!this.
|
|
718
|
+
if (!this._root) return true;
|
|
725
719
|
|
|
726
720
|
let balanced = true;
|
|
727
721
|
|
|
@@ -733,10 +727,10 @@ export class BST<
|
|
|
733
727
|
if (Math.abs(leftHeight - rightHeight) > 1) balanced = false;
|
|
734
728
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
735
729
|
};
|
|
736
|
-
_height(this.
|
|
730
|
+
_height(this._root);
|
|
737
731
|
} else {
|
|
738
732
|
const stack: NODE[] = [];
|
|
739
|
-
let node: OptBSTN<NODE> = this.
|
|
733
|
+
let node: OptBSTN<NODE> = this._root,
|
|
740
734
|
last: OptBSTN<NODE> = undefined;
|
|
741
735
|
const depths: Map<NODE, number> = new Map();
|
|
742
736
|
|
|
@@ -749,8 +743,8 @@ export class BST<
|
|
|
749
743
|
if (!node.right || last === node.right) {
|
|
750
744
|
node = stack.pop();
|
|
751
745
|
if (node) {
|
|
752
|
-
const left = node.left ?
|
|
753
|
-
const right = node.right ?
|
|
746
|
+
const left = node.left ? depths.get(node.left)! : -1;
|
|
747
|
+
const right = node.right ? depths.get(node.right)! : -1;
|
|
754
748
|
if (Math.abs(left - right) > 1) return false;
|
|
755
749
|
depths.set(node, 1 + Math.max(left, right));
|
|
756
750
|
last = node;
|