undirected-graph-typed 1.51.5 → 1.51.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/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -12
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -10
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/avl-tree.js +12 -14
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -13
- package/dist/data-structures/binary-tree/binary-tree.js +46 -78
- package/dist/data-structures/binary-tree/bst.d.ts +51 -96
- package/dist/data-structures/binary-tree/bst.js +120 -218
- package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/data-structures/binary-tree/rb-tree.js +4 -2
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/data-structures/heap/heap.d.ts +1 -3
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +6 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/dist/types/utils/utils.d.ts +10 -1
- package/dist/utils/utils.d.ts +2 -1
- package/dist/utils/utils.js +29 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -12
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +56 -76
- package/src/data-structures/binary-tree/bst.ts +132 -224
- package/src/data-structures/binary-tree/rb-tree.ts +9 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +5 -3
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/interfaces/binary-tree.ts +4 -3
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -5
- package/src/types/data-structures/binary-tree/bst.ts +6 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -2
- package/src/types/utils/utils.ts +14 -1
- package/src/utils/utils.ts +20 -1
|
@@ -60,22 +60,29 @@ exports.BSTNode = BSTNode;
|
|
|
60
60
|
*/
|
|
61
61
|
class BST extends binary_tree_1.BinaryTree {
|
|
62
62
|
/**
|
|
63
|
-
* This is the constructor function for a
|
|
64
|
-
*
|
|
65
|
-
* @param keysOrNodesOrEntries -
|
|
66
|
-
* to initialize the binary search tree with the provided
|
|
63
|
+
* This is the constructor function for a Binary Search Tree class in TypeScript, which initializes
|
|
64
|
+
* the tree with keys, nodes, or entries and optional options.
|
|
65
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
66
|
+
* contain keys, nodes, or entries. It is used to initialize the binary search tree with the provided
|
|
67
|
+
* keys, nodes, or entries.
|
|
67
68
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
68
69
|
* configuration options for the binary search tree. It can have the following properties:
|
|
69
70
|
*/
|
|
70
71
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
71
72
|
super([], options);
|
|
72
|
-
this.
|
|
73
|
+
this._root = undefined;
|
|
74
|
+
this._comparator = (a, b) => {
|
|
75
|
+
if (a > b)
|
|
76
|
+
return 1;
|
|
77
|
+
if (a < b)
|
|
78
|
+
return -1;
|
|
79
|
+
return 0;
|
|
80
|
+
};
|
|
73
81
|
if (options) {
|
|
74
|
-
const {
|
|
75
|
-
if (
|
|
76
|
-
this.
|
|
82
|
+
const { comparator } = options;
|
|
83
|
+
if (comparator)
|
|
84
|
+
this._comparator = comparator;
|
|
77
85
|
}
|
|
78
|
-
this._root = undefined;
|
|
79
86
|
if (keysOrNodesOrEntries)
|
|
80
87
|
this.addMany(keysOrNodesOrEntries);
|
|
81
88
|
}
|
|
@@ -87,11 +94,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
87
94
|
return this._root;
|
|
88
95
|
}
|
|
89
96
|
/**
|
|
90
|
-
* The function returns the value of the
|
|
91
|
-
* @returns The
|
|
97
|
+
* The function returns the value of the _comparator property.
|
|
98
|
+
* @returns The `_comparator` property is being returned.
|
|
92
99
|
*/
|
|
93
|
-
get
|
|
94
|
-
return this.
|
|
100
|
+
get comparator() {
|
|
101
|
+
return this._comparator;
|
|
95
102
|
}
|
|
96
103
|
/**
|
|
97
104
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
@@ -113,7 +120,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
113
120
|
* options. The returned value is casted as TREE.
|
|
114
121
|
*/
|
|
115
122
|
createTree(options) {
|
|
116
|
-
return new BST([], Object.assign({ iterationType: this.iterationType,
|
|
123
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
117
124
|
}
|
|
118
125
|
/**
|
|
119
126
|
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and returns a node if the keyOrNodeOrEntry is valid,
|
|
@@ -165,19 +172,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
165
172
|
* @returns either a node object (NODE) or undefined.
|
|
166
173
|
*/
|
|
167
174
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
175
|
+
if (keyOrNodeOrEntry === this.NIL)
|
|
176
|
+
return;
|
|
168
177
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
169
178
|
return keyOrNodeOrEntry;
|
|
170
179
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
|
|
180
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
181
|
+
const key = keyOrNodeOrEntry[0];
|
|
182
|
+
if (key === null || key === undefined)
|
|
178
183
|
return;
|
|
179
|
-
return this.getNodeByKey(
|
|
184
|
+
return this.getNodeByKey(key, iterationType);
|
|
180
185
|
}
|
|
186
|
+
const key = keyOrNodeOrEntry;
|
|
187
|
+
if (key === null || key === undefined)
|
|
188
|
+
return;
|
|
189
|
+
return this.getNodeByKey(key, iterationType);
|
|
181
190
|
}
|
|
182
191
|
/**
|
|
183
192
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -195,13 +204,11 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
195
204
|
* Time Complexity: O(log n)
|
|
196
205
|
* Space Complexity: O(1)
|
|
197
206
|
*
|
|
198
|
-
* The `add` function adds a new node to a binary tree
|
|
199
|
-
*
|
|
200
|
-
* @param keyOrNodeOrEntry -
|
|
201
|
-
* @param {V} [value] - The
|
|
202
|
-
*
|
|
203
|
-
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
204
|
-
* node was not added.
|
|
207
|
+
* The `add` function in TypeScript adds a new node to a binary search tree based on the key value,
|
|
208
|
+
* updating the value if the key already exists.
|
|
209
|
+
* @param keyOrNodeOrEntry - It is a parameter that can accept three types of values:
|
|
210
|
+
* @param {V} [value] - The value to be added to the binary search tree.
|
|
211
|
+
* @returns The method returns a boolean value.
|
|
205
212
|
*/
|
|
206
213
|
add(keyOrNodeOrEntry, value) {
|
|
207
214
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
@@ -214,7 +221,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
214
221
|
}
|
|
215
222
|
let current = this.root;
|
|
216
223
|
while (current !== undefined) {
|
|
217
|
-
if (this.
|
|
224
|
+
if (this.comparator(current.key, newNode.key) === 0) {
|
|
218
225
|
// if (current !== newNode) {
|
|
219
226
|
// The key value is the same but the reference is different, update the value of the existing node
|
|
220
227
|
this._replaceNode(current, newNode);
|
|
@@ -225,7 +232,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
225
232
|
// return;
|
|
226
233
|
// }
|
|
227
234
|
}
|
|
228
|
-
else if (this.
|
|
235
|
+
else if (this.comparator(current.key, newNode.key) > 0) {
|
|
229
236
|
if (current.left === undefined) {
|
|
230
237
|
current.left = newNode;
|
|
231
238
|
this._size++;
|
|
@@ -252,21 +259,24 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
252
259
|
* Time Complexity: O(k log n)
|
|
253
260
|
* Space Complexity: O(k + log n)
|
|
254
261
|
*
|
|
255
|
-
* The `addMany` function in TypeScript adds multiple keys or nodes to a
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
*
|
|
262
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a data structure, balancing
|
|
263
|
+
* the structure if specified, and returns an array indicating whether each key or node was
|
|
264
|
+
* successfully inserted.
|
|
265
|
+
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to the
|
|
266
|
+
* data structure.
|
|
259
267
|
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
260
268
|
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
261
269
|
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
262
|
-
* @param [isBalanceAdd=true] - A boolean flag indicating whether the
|
|
263
|
-
*
|
|
264
|
-
* algorithm. If set to false, the
|
|
265
|
-
*
|
|
266
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that
|
|
267
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
268
|
-
* `this.iterationType`, which
|
|
269
|
-
*
|
|
270
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the tree should be balanced after
|
|
271
|
+
* adding the elements. If set to true, the tree will be balanced using a binary search tree
|
|
272
|
+
* algorithm. If set to false, the elements will be added without balancing the tree. The default
|
|
273
|
+
* value is true.
|
|
274
|
+
* @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
|
|
275
|
+
* specifies the type of iteration to use when adding multiple keys or nodes to the binary tree. It
|
|
276
|
+
* has a default value of `this.iterationType`, which means it will use the iteration type specified
|
|
277
|
+
* in the binary tree instance.
|
|
278
|
+
* @returns The function `addMany` returns an array of booleans indicating whether each key or node
|
|
279
|
+
* or entry was successfully inserted into the data structure.
|
|
270
280
|
*/
|
|
271
281
|
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
272
282
|
const inserted = [];
|
|
@@ -293,20 +303,24 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
293
303
|
}
|
|
294
304
|
let sorted = [];
|
|
295
305
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
296
|
-
let
|
|
297
|
-
if (this.isEntry(a))
|
|
298
|
-
|
|
306
|
+
let keyA, keyB;
|
|
307
|
+
if (this.isEntry(a)) {
|
|
308
|
+
keyA = a[0];
|
|
309
|
+
}
|
|
299
310
|
else if (this.isRealNode(a))
|
|
300
|
-
|
|
311
|
+
keyA = a.key;
|
|
301
312
|
else
|
|
302
|
-
|
|
313
|
+
keyA = a;
|
|
303
314
|
if (this.isEntry(b))
|
|
304
|
-
|
|
315
|
+
keyB = b[0];
|
|
305
316
|
else if (this.isRealNode(b))
|
|
306
|
-
|
|
317
|
+
keyB = b.key;
|
|
307
318
|
else
|
|
308
|
-
|
|
309
|
-
|
|
319
|
+
keyB = b;
|
|
320
|
+
if (keyA !== undefined && keyA !== null && keyB !== undefined && keyB !== null) {
|
|
321
|
+
return this.comparator(keyA, keyB);
|
|
322
|
+
}
|
|
323
|
+
return 0;
|
|
310
324
|
});
|
|
311
325
|
const _dfs = (arr) => {
|
|
312
326
|
if (arr.length === 0)
|
|
@@ -342,56 +356,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
342
356
|
}
|
|
343
357
|
return inserted;
|
|
344
358
|
}
|
|
345
|
-
/**
|
|
346
|
-
* Time Complexity: O(log n)
|
|
347
|
-
* Space Complexity: O(1)
|
|
348
|
-
*/
|
|
349
|
-
/**
|
|
350
|
-
* Time Complexity: O(log n)
|
|
351
|
-
* Space Complexity: O(1)
|
|
352
|
-
*
|
|
353
|
-
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
354
|
-
* either recursive or iterative methods.
|
|
355
|
-
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
356
|
-
* It is used to identify the node that we want to retrieve.
|
|
357
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
358
|
-
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
359
|
-
* values:
|
|
360
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
361
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
362
|
-
*/
|
|
363
|
-
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
364
|
-
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
365
|
-
if (!this.isRealNode(this.root))
|
|
366
|
-
return;
|
|
367
|
-
if (iterationType === 'RECURSIVE') {
|
|
368
|
-
const dfs = (cur) => {
|
|
369
|
-
if (cur.key === key)
|
|
370
|
-
return cur;
|
|
371
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
|
-
return;
|
|
373
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
374
|
-
return dfs(cur.left);
|
|
375
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
376
|
-
return dfs(cur.right);
|
|
377
|
-
};
|
|
378
|
-
return dfs(this.root);
|
|
379
|
-
}
|
|
380
|
-
else {
|
|
381
|
-
const stack = [this.root];
|
|
382
|
-
while (stack.length > 0) {
|
|
383
|
-
const cur = stack.pop();
|
|
384
|
-
if (this.isRealNode(cur)) {
|
|
385
|
-
if (this._compare(cur.key, key) === 'EQ')
|
|
386
|
-
return cur;
|
|
387
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
388
|
-
stack.push(cur.left);
|
|
389
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
390
|
-
stack.push(cur.right);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
359
|
/**
|
|
396
360
|
* Time Complexity: O(log n)
|
|
397
361
|
* Space Complexity: O(k + log n)
|
|
@@ -424,6 +388,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
424
388
|
beginRoot = this.ensureNode(beginRoot);
|
|
425
389
|
if (!beginRoot)
|
|
426
390
|
return [];
|
|
391
|
+
callback = this._ensureCallback(identifier, callback);
|
|
427
392
|
const ans = [];
|
|
428
393
|
if (iterationType === 'RECURSIVE') {
|
|
429
394
|
const dfs = (cur) => {
|
|
@@ -437,9 +402,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
437
402
|
return;
|
|
438
403
|
// TODO potential bug
|
|
439
404
|
if (callback === this._DEFAULT_CALLBACK) {
|
|
440
|
-
if (this.isRealNode(cur.left) && this.
|
|
405
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
441
406
|
dfs(cur.left);
|
|
442
|
-
if (this.isRealNode(cur.right) && this.
|
|
407
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
443
408
|
dfs(cur.right);
|
|
444
409
|
}
|
|
445
410
|
else {
|
|
@@ -453,30 +418,28 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
418
|
const stack = [beginRoot];
|
|
454
419
|
while (stack.length > 0) {
|
|
455
420
|
const cur = stack.pop();
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
479
|
-
}
|
|
421
|
+
const callbackResult = callback(cur);
|
|
422
|
+
if (callbackResult === identifier) {
|
|
423
|
+
ans.push(cur);
|
|
424
|
+
if (onlyOne)
|
|
425
|
+
return ans;
|
|
426
|
+
}
|
|
427
|
+
// TODO potential bug
|
|
428
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
429
|
+
if (this.isRealNode(cur.right) && this.comparator(cur.key, identifier) < 0)
|
|
430
|
+
stack.push(cur.right);
|
|
431
|
+
if (this.isRealNode(cur.left) && this.comparator(cur.key, identifier) > 0)
|
|
432
|
+
stack.push(cur.left);
|
|
433
|
+
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
434
|
+
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
435
|
+
// // @ts-ignore
|
|
436
|
+
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
437
|
+
// // @ts-ignore
|
|
438
|
+
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
442
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
480
443
|
}
|
|
481
444
|
}
|
|
482
445
|
}
|
|
@@ -511,6 +474,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
511
474
|
var _a;
|
|
512
475
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
513
476
|
}
|
|
477
|
+
/**
|
|
478
|
+
* Time Complexity: O(log n)
|
|
479
|
+
* Space Complexity: O(1)
|
|
480
|
+
*/
|
|
481
|
+
/**
|
|
482
|
+
* Time Complexity: O(log n)
|
|
483
|
+
* Space Complexity: O(1)
|
|
484
|
+
*
|
|
485
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
486
|
+
* either recursive or iterative methods.
|
|
487
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
488
|
+
* It is used to identify the node that we want to retrieve.
|
|
489
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
490
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
491
|
+
* values:
|
|
492
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
493
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
494
|
+
*/
|
|
495
|
+
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
496
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
497
|
+
}
|
|
514
498
|
/**
|
|
515
499
|
* Time complexity: O(n)
|
|
516
500
|
* Space complexity: O(n)
|
|
@@ -586,41 +570,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
586
570
|
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
587
571
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
588
572
|
}
|
|
589
|
-
/**
|
|
590
|
-
* Time Complexity: O(log n)
|
|
591
|
-
* Space Complexity: O(1)
|
|
592
|
-
*/
|
|
593
|
-
/**
|
|
594
|
-
* Time Complexity: O(log n)
|
|
595
|
-
* Space Complexity: O(1)
|
|
596
|
-
*
|
|
597
|
-
* The `lastKey` function returns the key of the rightmost node in a binary tree, or the key of the
|
|
598
|
-
* leftmost node if the comparison result is greater than.
|
|
599
|
-
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is optional and can be of
|
|
600
|
-
* type `K`, `NODE`, or `undefined`. It represents the starting point for finding the last key in
|
|
601
|
-
* the binary tree. If not provided, it defaults to the root of the binary tree (`this.root`).
|
|
602
|
-
* @returns the key of the rightmost node in the binary tree if the comparison result is less than,
|
|
603
|
-
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
604
|
-
* rightmost node otherwise. If no node is found, it returns 0.
|
|
605
|
-
*/
|
|
606
|
-
lastKey(beginRoot = this.root) {
|
|
607
|
-
let current = this.ensureNode(beginRoot);
|
|
608
|
-
if (!current)
|
|
609
|
-
return undefined;
|
|
610
|
-
if (this._variant === 'STANDARD') {
|
|
611
|
-
// For 'STANDARD', find the rightmost node
|
|
612
|
-
while (current.right !== undefined) {
|
|
613
|
-
current = current.right;
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
else {
|
|
617
|
-
// For BSTVariant.MAX, find the leftmost node
|
|
618
|
-
while (current.left !== undefined) {
|
|
619
|
-
current = current.left;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
return current.key;
|
|
623
|
-
}
|
|
624
573
|
/**
|
|
625
574
|
* Time Complexity: O(log n)
|
|
626
575
|
* Space Complexity: O(log n)
|
|
@@ -646,18 +595,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
646
595
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
647
596
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
648
597
|
*/
|
|
649
|
-
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater =
|
|
650
|
-
|
|
598
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = -1, targetNode = this.root, iterationType = this.iterationType) {
|
|
599
|
+
const targetNodeEnsured = this.ensureNode(targetNode);
|
|
651
600
|
const ans = [];
|
|
652
|
-
if (!
|
|
601
|
+
if (!targetNodeEnsured)
|
|
653
602
|
return ans;
|
|
654
603
|
if (!this.root)
|
|
655
604
|
return ans;
|
|
656
|
-
const targetKey =
|
|
605
|
+
const targetKey = targetNodeEnsured.key;
|
|
657
606
|
if (iterationType === 'RECURSIVE') {
|
|
658
607
|
const dfs = (cur) => {
|
|
659
|
-
const compared = this.
|
|
660
|
-
if (compared === lesserOrGreater)
|
|
608
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
609
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
661
610
|
ans.push(callback(cur));
|
|
662
611
|
if (this.isRealNode(cur.left))
|
|
663
612
|
dfs(cur.left);
|
|
@@ -672,8 +621,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
672
621
|
while (queue.size > 0) {
|
|
673
622
|
const cur = queue.shift();
|
|
674
623
|
if (this.isRealNode(cur)) {
|
|
675
|
-
const compared = this.
|
|
676
|
-
if (compared === lesserOrGreater)
|
|
624
|
+
const compared = this.comparator(cur.key, targetKey);
|
|
625
|
+
if (Math.sign(compared) === lesserOrGreater)
|
|
677
626
|
ans.push(callback(cur));
|
|
678
627
|
if (this.isRealNode(cur.left))
|
|
679
628
|
queue.push(cur.left);
|
|
@@ -814,52 +763,5 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
814
763
|
}
|
|
815
764
|
this._root = v;
|
|
816
765
|
}
|
|
817
|
-
/**
|
|
818
|
-
* The function compares two values using a comparator function and returns whether the first value
|
|
819
|
-
* is greater than, less than, or equal to the second value.
|
|
820
|
-
* @param {K} a - The parameter "a" is of type K.
|
|
821
|
-
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
822
|
-
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
823
|
-
* than), 'LT' (less than), or 'EQ' (equal).
|
|
824
|
-
*/
|
|
825
|
-
_compare(a, b) {
|
|
826
|
-
const extractedA = this.extractor(a);
|
|
827
|
-
const extractedB = this.extractor(b);
|
|
828
|
-
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
829
|
-
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
|
|
830
|
-
}
|
|
831
|
-
/**
|
|
832
|
-
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
833
|
-
* `a` is less than `b` based on the specified variant.
|
|
834
|
-
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
835
|
-
* first value to be compared in the function.
|
|
836
|
-
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
837
|
-
* of the arguments for the comparison in the `_lt` function.
|
|
838
|
-
* @returns a boolean value.
|
|
839
|
-
*/
|
|
840
|
-
_lt(a, b) {
|
|
841
|
-
const extractedA = this.extractor(a);
|
|
842
|
-
const extractedB = this.extractor(b);
|
|
843
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
|
|
844
|
-
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
845
|
-
// return extractedA < extractedB;
|
|
846
|
-
// return a < b;
|
|
847
|
-
}
|
|
848
|
-
/**
|
|
849
|
-
* The function compares two values using a custom extractor function and returns true if the first
|
|
850
|
-
* value is greater than the second value.
|
|
851
|
-
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
852
|
-
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
853
|
-
* of the arguments for the comparison in the function.
|
|
854
|
-
* @returns a boolean value.
|
|
855
|
-
*/
|
|
856
|
-
_gt(a, b) {
|
|
857
|
-
const extractedA = this.extractor(a);
|
|
858
|
-
const extractedB = this.extractor(b);
|
|
859
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
|
|
860
|
-
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
861
|
-
// return extractedA > extractedB;
|
|
862
|
-
// return a > b;
|
|
863
|
-
}
|
|
864
766
|
}
|
|
865
767
|
exports.BST = BST;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
|
-
import { CRUD, RBTNColor } from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNCallback, Comparable, CRUD, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
3
2
|
import { BST, BSTNode } from './bst';
|
|
4
3
|
import { IBinaryTree } from '../../interfaces';
|
|
5
|
-
export declare class RedBlackTreeNode<K
|
|
4
|
+
export declare class RedBlackTreeNode<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
6
5
|
/**
|
|
7
6
|
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
8
7
|
* color.
|
|
@@ -27,7 +26,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
27
26
|
*/
|
|
28
27
|
set color(value: RBTNColor);
|
|
29
28
|
}
|
|
30
|
-
export declare class RedBlackTree<K
|
|
29
|
+
export declare class RedBlackTree<K extends Comparable, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>, TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
31
30
|
/**
|
|
32
31
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
33
32
|
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
@@ -214,6 +214,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
214
214
|
if (identifier === null)
|
|
215
215
|
return [];
|
|
216
216
|
const results = [];
|
|
217
|
+
callback = this._ensureCallback(identifier, callback);
|
|
217
218
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
218
219
|
if (!nodeToDelete) {
|
|
219
220
|
return results;
|
|
@@ -313,10 +314,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
313
314
|
let parent = undefined;
|
|
314
315
|
while (this.isRealNode(current)) {
|
|
315
316
|
parent = current;
|
|
316
|
-
|
|
317
|
+
const compared = this.comparator(node.key, current.key);
|
|
318
|
+
if (compared < 0) {
|
|
317
319
|
current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
|
|
318
320
|
}
|
|
319
|
-
else if (
|
|
321
|
+
else if (compared > 0) {
|
|
320
322
|
current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
|
|
321
323
|
}
|
|
322
324
|
else {
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
-
import { RBTNColor } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, IterationType, KeyOrNodeOrEntry, RBTNColor, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
10
9
|
import { IBinaryTree } from '../../interfaces';
|
|
11
10
|
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
12
|
-
export declare class TreeMultiMapNode<K
|
|
11
|
+
export declare class TreeMultiMapNode<K extends Comparable, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
13
12
|
/**
|
|
14
13
|
* The constructor function initializes a Red-Black Tree node with a key, value, count, and color.
|
|
15
14
|
* @param {K} key - The key parameter represents the key of the node in the Red-Black Tree. It is
|
|
@@ -36,7 +35,7 @@ export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMa
|
|
|
36
35
|
*/
|
|
37
36
|
set count(value: number);
|
|
38
37
|
}
|
|
39
|
-
export declare class TreeMultiMap<K
|
|
38
|
+
export declare class TreeMultiMap<K extends Comparable, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>> extends RedBlackTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
40
39
|
/**
|
|
41
40
|
* The constructor function initializes a new instance of the TreeMultiMap class with optional
|
|
42
41
|
* initial keys, nodes, or entries.
|
|
@@ -210,6 +210,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
210
210
|
if (identifier === null)
|
|
211
211
|
return [];
|
|
212
212
|
const results = [];
|
|
213
|
+
callback = this._ensureCallback(identifier, callback);
|
|
213
214
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
214
215
|
if (!nodeToDelete) {
|
|
215
216
|
return results;
|
|
@@ -57,9 +57,7 @@ export declare class Heap<E = any> extends IterableElementBase<E> {
|
|
|
57
57
|
* @param elements
|
|
58
58
|
* @param options
|
|
59
59
|
*/
|
|
60
|
-
static heapify<E>(elements: Iterable<E>, options:
|
|
61
|
-
comparator: Comparator<E>;
|
|
62
|
-
}): Heap<E>;
|
|
60
|
+
static heapify<E>(elements: Iterable<E>, options: HeapOptions<E>): Heap<E>;
|
|
63
61
|
/**
|
|
64
62
|
* Time Complexity: O(log n)
|
|
65
63
|
* Space Complexity: O(1)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } from '../types';
|
|
3
|
-
export interface IBinaryTree<K
|
|
2
|
+
import type { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, Comparable, KeyOrNodeOrEntry } from '../types';
|
|
3
|
+
export interface IBinaryTree<K extends Comparable, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
|
-
createTree(options?: Partial<BinaryTreeOptions
|
|
5
|
+
createTree(options?: Partial<BinaryTreeOptions>): TREE;
|
|
6
6
|
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
7
7
|
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
|
|
2
2
|
import type { AVLTreeOptions } from './avl-tree';
|
|
3
|
-
|
|
4
|
-
export type
|
|
3
|
+
import { Comparable } from "../../utils";
|
|
4
|
+
export type AVLTreeMultiMapNodeNested<K extends Comparable, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type AVLTreeMultiMapNested<K extends Comparable, V, N extends AVLTreeMultiMapNode<K, V, N>> = AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
6
|
export type AVLTreeMultiMapOptions<K> = AVLTreeOptions<K> & {};
|