avl-tree-typed 1.49.5 → 1.49.6
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.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
|
@@ -7,13 +7,11 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type {
|
|
9
9
|
BSTNested,
|
|
10
|
-
BSTNKeyOrNode,
|
|
11
10
|
BSTNodeNested,
|
|
12
11
|
BSTOptions,
|
|
13
12
|
BTNCallback,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
BTNodePureExemplar
|
|
13
|
+
BTNodePureExemplar,
|
|
14
|
+
KeyOrNodeOrEntry
|
|
17
15
|
} from '../../types';
|
|
18
16
|
import { BSTVariant, CP, IterationType } from '../../types';
|
|
19
17
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
@@ -94,13 +92,13 @@ export class BST<
|
|
|
94
92
|
implements IBinaryTree<K, V, N, TREE> {
|
|
95
93
|
/**
|
|
96
94
|
* This is the constructor function for a binary search tree class in TypeScript, which initializes
|
|
97
|
-
* the tree with optional
|
|
98
|
-
* @param [
|
|
95
|
+
* the tree with optional nodes and options.
|
|
96
|
+
* @param [nodes] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
|
|
99
97
|
* binary search tree.
|
|
100
98
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
101
99
|
* configuration options for the binary search tree. It can have the following properties:
|
|
102
100
|
*/
|
|
103
|
-
constructor(
|
|
101
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<BSTOptions<K>>) {
|
|
104
102
|
super([], options);
|
|
105
103
|
|
|
106
104
|
if (options) {
|
|
@@ -112,7 +110,7 @@ export class BST<
|
|
|
112
110
|
|
|
113
111
|
this._root = undefined;
|
|
114
112
|
|
|
115
|
-
if (
|
|
113
|
+
if (nodes) this.addMany(nodes);
|
|
116
114
|
}
|
|
117
115
|
|
|
118
116
|
protected override _root?: N;
|
|
@@ -121,7 +119,7 @@ export class BST<
|
|
|
121
119
|
return this._root;
|
|
122
120
|
}
|
|
123
121
|
|
|
124
|
-
protected _variant = BSTVariant.
|
|
122
|
+
protected _variant = BSTVariant.STANDARD;
|
|
125
123
|
|
|
126
124
|
get variant() {
|
|
127
125
|
return this._variant;
|
|
@@ -155,37 +153,28 @@ export class BST<
|
|
|
155
153
|
}
|
|
156
154
|
|
|
157
155
|
/**
|
|
158
|
-
* The function
|
|
159
|
-
* @param exemplar - The `exemplar` parameter is a variable of type `BTNExemplar<K, V, N>`.
|
|
160
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the BSTNode class.
|
|
161
|
-
*/
|
|
162
|
-
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
163
|
-
return exemplar instanceof BSTNode;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
156
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
168
157
|
* otherwise it returns undefined.
|
|
169
|
-
* @param
|
|
158
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
170
159
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
171
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
160
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node.
|
|
172
161
|
* @returns a node of type N or undefined.
|
|
173
162
|
*/
|
|
174
|
-
override exemplarToNode(
|
|
163
|
+
override exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined {
|
|
175
164
|
let node: N | undefined;
|
|
176
|
-
if (
|
|
165
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
177
166
|
return;
|
|
178
|
-
} else if (this.isNode(
|
|
179
|
-
node =
|
|
180
|
-
} else if (this.isEntry(
|
|
181
|
-
const [key, value] =
|
|
167
|
+
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
168
|
+
node = keyOrNodeOrEntry;
|
|
169
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
170
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
182
171
|
if (key === undefined || key === null) {
|
|
183
172
|
return;
|
|
184
173
|
} else {
|
|
185
174
|
node = this.createNode(key, value);
|
|
186
175
|
}
|
|
187
|
-
} else if (this.isNotNodeInstance(
|
|
188
|
-
node = this.createNode(
|
|
176
|
+
} else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
177
|
+
node = this.createNode(keyOrNodeOrEntry, value);
|
|
189
178
|
} else {
|
|
190
179
|
return;
|
|
191
180
|
}
|
|
@@ -193,13 +182,66 @@ export class BST<
|
|
|
193
182
|
}
|
|
194
183
|
|
|
195
184
|
/**
|
|
196
|
-
* Time Complexity: O(log n)
|
|
197
|
-
* Space Complexity: O(
|
|
185
|
+
* Time Complexity: O(log n)
|
|
186
|
+
* Space Complexity: O(log n)
|
|
187
|
+
* Average case for a balanced tree. Space for the recursive call stack in the worst case.
|
|
198
188
|
*/
|
|
199
189
|
|
|
200
190
|
/**
|
|
201
|
-
* Time Complexity: O(log n)
|
|
202
|
-
* Space Complexity: O(
|
|
191
|
+
* Time Complexity: O(log n)
|
|
192
|
+
* Space Complexity: O(log n)
|
|
193
|
+
*
|
|
194
|
+
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
195
|
+
* otherwise it returns the key itself.
|
|
196
|
+
* @param {K | N | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `N`, or
|
|
197
|
+
* `undefined`.
|
|
198
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
199
|
+
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
200
|
+
* @returns either a node object (N) or undefined.
|
|
201
|
+
*/
|
|
202
|
+
override ensureNode(
|
|
203
|
+
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>,
|
|
204
|
+
iterationType = IterationType.ITERATIVE
|
|
205
|
+
): N | undefined {
|
|
206
|
+
let res: N | undefined;
|
|
207
|
+
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
208
|
+
res = keyOrNodeOrEntry;
|
|
209
|
+
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
210
|
+
if (keyOrNodeOrEntry[0]) res = this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
211
|
+
} else {
|
|
212
|
+
if (keyOrNodeOrEntry) res = this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
213
|
+
}
|
|
214
|
+
return res;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
219
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
220
|
+
* data type.
|
|
221
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
222
|
+
*/
|
|
223
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
224
|
+
return !(potentialKey instanceof BSTNode);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
229
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
230
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the BSTNode class.
|
|
231
|
+
*/
|
|
232
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
233
|
+
return keyOrNodeOrEntry instanceof BSTNode;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Time Complexity: O(log n)
|
|
238
|
+
* Space Complexity: O(1)
|
|
239
|
+
* - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Time Complexity: O(log n)
|
|
244
|
+
* Space Complexity: O(1)
|
|
203
245
|
*
|
|
204
246
|
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
205
247
|
* or inserting a new node if the key is unique.
|
|
@@ -209,14 +251,14 @@ export class BST<
|
|
|
209
251
|
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
210
252
|
* node was not added.
|
|
211
253
|
*/
|
|
212
|
-
override add(keyOrNodeOrEntry:
|
|
254
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
213
255
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
214
|
-
if (newNode === undefined) return;
|
|
256
|
+
if (newNode === undefined) return false;
|
|
215
257
|
|
|
216
258
|
if (this.root === undefined) {
|
|
217
259
|
this._setRoot(newNode);
|
|
218
260
|
this._size++;
|
|
219
|
-
return
|
|
261
|
+
return true;
|
|
220
262
|
}
|
|
221
263
|
|
|
222
264
|
let current = this.root;
|
|
@@ -225,7 +267,7 @@ export class BST<
|
|
|
225
267
|
// if (current !== newNode) {
|
|
226
268
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
227
269
|
this._replaceNode(current, newNode);
|
|
228
|
-
return
|
|
270
|
+
return true;
|
|
229
271
|
|
|
230
272
|
// } else {
|
|
231
273
|
// The key value is the same and the reference is the same, replace the entire node
|
|
@@ -238,7 +280,7 @@ export class BST<
|
|
|
238
280
|
current.left = newNode;
|
|
239
281
|
newNode.parent = current;
|
|
240
282
|
this._size++;
|
|
241
|
-
return
|
|
283
|
+
return true;
|
|
242
284
|
}
|
|
243
285
|
current = current.left;
|
|
244
286
|
} else {
|
|
@@ -246,23 +288,24 @@ export class BST<
|
|
|
246
288
|
current.right = newNode;
|
|
247
289
|
newNode.parent = current;
|
|
248
290
|
this._size++;
|
|
249
|
-
return
|
|
291
|
+
return true;
|
|
250
292
|
}
|
|
251
293
|
current = current.right;
|
|
252
294
|
}
|
|
253
295
|
}
|
|
254
296
|
|
|
255
|
-
return
|
|
297
|
+
return false;
|
|
256
298
|
}
|
|
257
299
|
|
|
258
300
|
/**
|
|
259
|
-
* Time Complexity: O(k log n)
|
|
260
|
-
* Space Complexity: O(k)
|
|
301
|
+
* Time Complexity: O(k log n)
|
|
302
|
+
* Space Complexity: O(k)
|
|
303
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
261
304
|
*/
|
|
262
305
|
|
|
263
306
|
/**
|
|
264
|
-
* Time Complexity: O(k log n)
|
|
265
|
-
* Space Complexity: O(k)
|
|
307
|
+
* Time Complexity: O(k log n)
|
|
308
|
+
* Space Complexity: O(k)
|
|
266
309
|
*
|
|
267
310
|
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
268
311
|
* balancing the tree after each addition.
|
|
@@ -273,7 +316,7 @@ export class BST<
|
|
|
273
316
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
274
317
|
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
275
318
|
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
276
|
-
* algorithm. If set to false, the add operation will not be balanced and the
|
|
319
|
+
* algorithm. If set to false, the add operation will not be balanced and the nodes will be added
|
|
277
320
|
* in the order they appear in the input.
|
|
278
321
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
279
322
|
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
@@ -281,12 +324,12 @@ export class BST<
|
|
|
281
324
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
282
325
|
*/
|
|
283
326
|
override addMany(
|
|
284
|
-
keysOrNodesOrEntries: Iterable<
|
|
327
|
+
keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>,
|
|
285
328
|
values?: Iterable<V | undefined>,
|
|
286
329
|
isBalanceAdd = true,
|
|
287
330
|
iterationType = this.iterationType
|
|
288
|
-
):
|
|
289
|
-
const inserted:
|
|
331
|
+
): boolean[] {
|
|
332
|
+
const inserted: boolean[] = [];
|
|
290
333
|
|
|
291
334
|
let valuesIterator: Iterator<V | undefined> | undefined;
|
|
292
335
|
|
|
@@ -305,7 +348,7 @@ export class BST<
|
|
|
305
348
|
|
|
306
349
|
const realBTNExemplars: BTNodePureExemplar<K, V, N>[] = [];
|
|
307
350
|
|
|
308
|
-
const isRealBTNExemplar = (kve:
|
|
351
|
+
const isRealBTNExemplar = (kve: KeyOrNodeOrEntry<K, V, N>): kve is BTNodePureExemplar<K, V, N> => {
|
|
309
352
|
if (kve === undefined || kve === null) return false;
|
|
310
353
|
return !(this.isEntry(kve) && (kve[0] === undefined || kve[0] === null));
|
|
311
354
|
};
|
|
@@ -367,30 +410,29 @@ export class BST<
|
|
|
367
410
|
}
|
|
368
411
|
|
|
369
412
|
/**
|
|
370
|
-
* Time Complexity: O(n log n)
|
|
371
|
-
* Space Complexity: O(n)
|
|
413
|
+
* Time Complexity: O(n log n)
|
|
414
|
+
* Space Complexity: O(n)
|
|
415
|
+
* Adding each element individually in a balanced tree. Additional space is required for the sorted array.
|
|
372
416
|
*/
|
|
373
417
|
|
|
374
418
|
/**
|
|
375
|
-
* Time Complexity: O(log n)
|
|
376
|
-
* Space Complexity: O(
|
|
419
|
+
* Time Complexity: O(n log n)
|
|
420
|
+
* Space Complexity: O(n)
|
|
377
421
|
*
|
|
378
422
|
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
379
423
|
* leftmost node if the comparison result is greater than.
|
|
380
424
|
* @param {K | N | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
381
425
|
* type `K`, `N`, or `undefined`. It represents the starting point for finding the last key in
|
|
382
426
|
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
383
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
384
|
-
* be performed. It can have one of the following values:
|
|
385
427
|
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
386
428
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
387
429
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
388
430
|
*/
|
|
389
|
-
lastKey(beginRoot:
|
|
431
|
+
lastKey(beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root): K | undefined {
|
|
390
432
|
let current = this.ensureNode(beginRoot);
|
|
391
433
|
if (!current) return undefined;
|
|
392
434
|
|
|
393
|
-
if (this._variant === BSTVariant.
|
|
435
|
+
if (this._variant === BSTVariant.STANDARD) {
|
|
394
436
|
// For BSTVariant.MIN, find the rightmost node
|
|
395
437
|
while (current.right !== undefined) {
|
|
396
438
|
current = current.right;
|
|
@@ -405,13 +447,13 @@ export class BST<
|
|
|
405
447
|
}
|
|
406
448
|
|
|
407
449
|
/**
|
|
408
|
-
* Time Complexity: O(log n)
|
|
409
|
-
* Space Complexity: O(1)
|
|
450
|
+
* Time Complexity: O(log n)
|
|
451
|
+
* Space Complexity: O(1)
|
|
410
452
|
*/
|
|
411
453
|
|
|
412
454
|
/**
|
|
413
|
-
* Time Complexity: O(log n)
|
|
414
|
-
* Space Complexity: O(
|
|
455
|
+
* Time Complexity: O(log n)
|
|
456
|
+
* Space Complexity: O(1)
|
|
415
457
|
*
|
|
416
458
|
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
417
459
|
* either recursive or iterative methods.
|
|
@@ -449,36 +491,14 @@ export class BST<
|
|
|
449
491
|
}
|
|
450
492
|
|
|
451
493
|
/**
|
|
452
|
-
*
|
|
453
|
-
*
|
|
454
|
-
*
|
|
455
|
-
*
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
463
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
464
|
-
*/
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* The function `ensureNode` returns the node corresponding to the given key if it is a node key,
|
|
468
|
-
* otherwise it returns the key itself.
|
|
469
|
-
* @param {K | N | undefined} key - The `key` parameter can be of type `K`, `N`, or
|
|
470
|
-
* `undefined`.
|
|
471
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
472
|
-
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
|
473
|
-
* @returns either a node object (N) or undefined.
|
|
474
|
-
*/
|
|
475
|
-
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
|
476
|
-
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
481
|
-
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
494
|
+
* Time Complexity: O(log n)
|
|
495
|
+
* Space Complexity: O(log n)
|
|
496
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
497
|
+
* /
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Time Complexity: O(log n)
|
|
501
|
+
* Space Complexity: O(log n)
|
|
482
502
|
*
|
|
483
503
|
* The function `getNodes` returns an array of nodes that match a given identifier, using either a
|
|
484
504
|
* recursive or iterative approach.
|
|
@@ -503,7 +523,7 @@ export class BST<
|
|
|
503
523
|
identifier: ReturnType<C> | undefined,
|
|
504
524
|
callback: C = this._defaultOneParamCallback as C,
|
|
505
525
|
onlyOne = false,
|
|
506
|
-
beginRoot:
|
|
526
|
+
beginRoot: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
507
527
|
iterationType = this.iterationType
|
|
508
528
|
): N[] {
|
|
509
529
|
beginRoot = this.ensureNode(beginRoot);
|
|
@@ -556,13 +576,14 @@ export class BST<
|
|
|
556
576
|
}
|
|
557
577
|
|
|
558
578
|
/**
|
|
559
|
-
* Time Complexity: O(log n)
|
|
560
|
-
* Space Complexity: O(log n)
|
|
579
|
+
* Time Complexity: O(log n)
|
|
580
|
+
* Space Complexity: O(log n)
|
|
581
|
+
* Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key. Space for the recursive call stack in the worst case.
|
|
561
582
|
*/
|
|
562
583
|
|
|
563
584
|
/**
|
|
564
|
-
* Time Complexity: O(log n)
|
|
565
|
-
* Space Complexity: O(log n)
|
|
585
|
+
* Time Complexity: O(log n)
|
|
586
|
+
* Space Complexity: O(log n)
|
|
566
587
|
*
|
|
567
588
|
* The `lesserOrGreaterTraverse` function traverses a binary tree and returns an array of nodes that
|
|
568
589
|
* are either lesser or greater than a target node, depending on the specified comparison type.
|
|
@@ -584,7 +605,7 @@ export class BST<
|
|
|
584
605
|
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
|
585
606
|
callback: C = this._defaultOneParamCallback as C,
|
|
586
607
|
lesserOrGreater: CP = CP.lt,
|
|
587
|
-
targetNode:
|
|
608
|
+
targetNode: KeyOrNodeOrEntry<K, V, N> = this.root,
|
|
588
609
|
iterationType = this.iterationType
|
|
589
610
|
): ReturnType<C>[] {
|
|
590
611
|
targetNode = this.ensureNode(targetNode);
|
|
@@ -623,13 +644,13 @@ export class BST<
|
|
|
623
644
|
}
|
|
624
645
|
|
|
625
646
|
/**
|
|
626
|
-
* Time Complexity: O(log n)
|
|
627
|
-
* Space Complexity: O(log n)
|
|
647
|
+
* Time Complexity: O(log n)
|
|
648
|
+
* Space Complexity: O(log n)
|
|
628
649
|
*/
|
|
629
650
|
|
|
630
651
|
/**
|
|
631
|
-
* Time Complexity: O(n)
|
|
632
|
-
* Space Complexity: O(n)
|
|
652
|
+
* Time Complexity: O(log n)
|
|
653
|
+
* Space Complexity: O(log n)
|
|
633
654
|
*
|
|
634
655
|
* The `perfectlyBalance` function balances a binary search tree by adding nodes in a way that
|
|
635
656
|
* ensures the tree is perfectly balanced.
|
|
@@ -691,8 +712,8 @@ export class BST<
|
|
|
691
712
|
*/
|
|
692
713
|
|
|
693
714
|
/**
|
|
694
|
-
* Time Complexity: O(n)
|
|
695
|
-
* Space Complexity: O(log n)
|
|
715
|
+
* Time Complexity: O(n)
|
|
716
|
+
* Space Complexity: O(log n)
|
|
696
717
|
*
|
|
697
718
|
* The function checks if a binary tree is AVL balanced using either recursive or iterative approach.
|
|
698
719
|
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
@@ -761,7 +782,7 @@ export class BST<
|
|
|
761
782
|
protected _compare(a: K, b: K): CP {
|
|
762
783
|
const extractedA = this.extractor(a);
|
|
763
784
|
const extractedB = this.extractor(b);
|
|
764
|
-
const compared = this.variant === BSTVariant.
|
|
785
|
+
const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
|
|
765
786
|
|
|
766
787
|
return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
|
|
767
788
|
}
|