data-structure-typed 1.51.6 → 1.51.7
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/CHANGELOG.md +1 -1
- package/README.md +13 -13
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +405 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +24 -46
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +19 -19
- package/dist/cjs/data-structures/binary-tree/bst.js +59 -89
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +19 -0
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +23 -45
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +19 -19
- package/dist/mjs/data-structures/binary-tree/bst.js +59 -89
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +10 -1
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +19 -0
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +1 -0
- package/dist/umd/data-structure-typed.js +98 -118
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -0
- package/src/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/data-structures/binary-tree/binary-tree.ts +27 -38
- package/src/data-structures/binary-tree/bst.ts +59 -81
- package/src/data-structures/binary-tree/rb-tree.ts +19 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -0
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +7 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.51.
|
|
3
|
+
"version": "1.51.7",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -66,11 +66,11 @@
|
|
|
66
66
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
67
67
|
"@typescript-eslint/parser": "^6.7.4",
|
|
68
68
|
"auto-changelog": "^2.4.0",
|
|
69
|
-
"avl-tree-typed": "^1.51.
|
|
69
|
+
"avl-tree-typed": "^1.51.6",
|
|
70
70
|
"benchmark": "^2.1.4",
|
|
71
|
-
"binary-tree-typed": "^1.51.
|
|
71
|
+
"binary-tree-typed": "^1.51.6",
|
|
72
72
|
"bst-typed": "^1.51.5",
|
|
73
|
-
"data-structure-typed": "^1.51.
|
|
73
|
+
"data-structure-typed": "^1.51.6",
|
|
74
74
|
"dependency-cruiser": "^14.1.0",
|
|
75
75
|
"doctoc": "^2.2.1",
|
|
76
76
|
"eslint": "^8.50.0",
|
|
@@ -245,6 +245,7 @@ export class AVLTreeMultiMap<
|
|
|
245
245
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
246
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
247
247
|
if (!this.root) return deletedResult;
|
|
248
|
+
callback = this._ensureCallback(identifier, callback);
|
|
248
249
|
|
|
249
250
|
const curr: NODE | undefined = this.getNode(identifier, callback) ?? undefined;
|
|
250
251
|
if (!curr) return deletedResult;
|
|
@@ -172,7 +172,6 @@ export class AVLTree<
|
|
|
172
172
|
identifier: ReturnType<C>,
|
|
173
173
|
callback: C = this._DEFAULT_CALLBACK as C
|
|
174
174
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
175
|
-
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
176
175
|
const deletedResults = super.delete(identifier, callback);
|
|
177
176
|
for (const { needBalanced } of deletedResults) {
|
|
178
177
|
if (needBalanced) {
|
|
@@ -278,17 +278,19 @@ export class BinaryTree<
|
|
|
278
278
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
279
279
|
iterationType: IterationType = 'ITERATIVE'
|
|
280
280
|
): NODE | null | undefined {
|
|
281
|
+
if (keyOrNodeOrEntry === this.NIL) return;
|
|
281
282
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
282
283
|
return keyOrNodeOrEntry;
|
|
283
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
284
|
-
if (keyOrNodeOrEntry[0] === null) return null;
|
|
285
|
-
if (keyOrNodeOrEntry[0] === undefined) return;
|
|
286
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
287
|
-
} else {
|
|
288
|
-
if (keyOrNodeOrEntry === null) return null;
|
|
289
|
-
if (keyOrNodeOrEntry === undefined) return;
|
|
290
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
291
284
|
}
|
|
285
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
286
|
+
const key = keyOrNodeOrEntry[0];
|
|
287
|
+
if (key === null) return null;
|
|
288
|
+
if (key === undefined) return;
|
|
289
|
+
return this.getNodeByKey(key, iterationType);
|
|
290
|
+
}
|
|
291
|
+
if (keyOrNodeOrEntry === null) return null;
|
|
292
|
+
if (keyOrNodeOrEntry === undefined) return;
|
|
293
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
292
294
|
}
|
|
293
295
|
|
|
294
296
|
/**
|
|
@@ -506,8 +508,7 @@ export class BinaryTree<
|
|
|
506
508
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
507
509
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
508
510
|
if (!this.root) return deletedResult;
|
|
509
|
-
|
|
510
|
-
callback = (node => node) as C;
|
|
511
|
+
callback = this._ensureCallback(identifier, callback);
|
|
511
512
|
|
|
512
513
|
const curr = this.getNode(identifier, callback);
|
|
513
514
|
if (!curr) return deletedResult;
|
|
@@ -610,10 +611,9 @@ export class BinaryTree<
|
|
|
610
611
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
611
612
|
iterationType: IterationType = this.iterationType
|
|
612
613
|
): NODE[] {
|
|
613
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
614
|
-
callback = (node => node) as C;
|
|
615
614
|
beginRoot = this.ensureNode(beginRoot);
|
|
616
615
|
if (!beginRoot) return [];
|
|
616
|
+
callback = this._ensureCallback(identifier, callback);
|
|
617
617
|
|
|
618
618
|
const ans: NODE[] = [];
|
|
619
619
|
|
|
@@ -722,29 +722,8 @@ export class BinaryTree<
|
|
|
722
722
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
723
723
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
724
724
|
*/
|
|
725
|
-
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
726
|
-
|
|
727
|
-
if (iterationType === 'RECURSIVE') {
|
|
728
|
-
const dfs = (cur: NODE): NODE | undefined => {
|
|
729
|
-
if (cur.key === key) return cur;
|
|
730
|
-
|
|
731
|
-
if (!cur.left && !cur.right) return;
|
|
732
|
-
if (cur.left) return dfs(cur.left);
|
|
733
|
-
if (cur.right) return dfs(cur.right);
|
|
734
|
-
};
|
|
735
|
-
|
|
736
|
-
return dfs(this.root);
|
|
737
|
-
} else {
|
|
738
|
-
const stack = [this.root];
|
|
739
|
-
while (stack.length > 0) {
|
|
740
|
-
const cur = stack.pop();
|
|
741
|
-
if (cur) {
|
|
742
|
-
if (cur.key === key) return cur;
|
|
743
|
-
cur.left && stack.push(cur.left);
|
|
744
|
-
cur.right && stack.push(cur.right);
|
|
745
|
-
}
|
|
746
|
-
}
|
|
747
|
-
}
|
|
725
|
+
getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | null | undefined {
|
|
726
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
748
727
|
}
|
|
749
728
|
|
|
750
729
|
override get<C extends BTNCallback<NODE, K>>(
|
|
@@ -801,7 +780,7 @@ export class BinaryTree<
|
|
|
801
780
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
802
781
|
iterationType: IterationType = this.iterationType
|
|
803
782
|
): V | undefined {
|
|
804
|
-
return this.getNode(identifier, callback, beginRoot, iterationType)?.value
|
|
783
|
+
return this.getNode(identifier, callback, beginRoot, iterationType)?.value;
|
|
805
784
|
}
|
|
806
785
|
|
|
807
786
|
override has<C extends BTNCallback<NODE, K>>(
|
|
@@ -857,8 +836,7 @@ export class BinaryTree<
|
|
|
857
836
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
858
837
|
iterationType: IterationType = this.iterationType
|
|
859
838
|
): boolean {
|
|
860
|
-
|
|
861
|
-
callback = (node => node) as C;
|
|
839
|
+
callback = this._ensureCallback(identifier, callback);
|
|
862
840
|
|
|
863
841
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType).length > 0;
|
|
864
842
|
}
|
|
@@ -2070,4 +2048,15 @@ export class BinaryTree<
|
|
|
2070
2048
|
}
|
|
2071
2049
|
this._root = v;
|
|
2072
2050
|
}
|
|
2051
|
+
|
|
2052
|
+
protected _ensureCallback<C extends BTNCallback<NODE>>(
|
|
2053
|
+
identifier: ReturnType<C> | null | undefined,
|
|
2054
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
2055
|
+
): C {
|
|
2056
|
+
if ((!callback || callback === this._DEFAULT_CALLBACK) && this.isNode(identifier)) {
|
|
2057
|
+
callback = (node => node) as C;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
return callback;
|
|
2061
|
+
}
|
|
2073
2062
|
}
|
|
@@ -214,15 +214,20 @@ export class BST<
|
|
|
214
214
|
keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
|
|
215
215
|
iterationType: IterationType = 'ITERATIVE'
|
|
216
216
|
): NODE | undefined {
|
|
217
|
+
if (keyOrNodeOrEntry === this.NIL) return;
|
|
217
218
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
218
219
|
return keyOrNodeOrEntry;
|
|
219
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
220
|
-
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined) return;
|
|
221
|
-
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
222
|
-
} else {
|
|
223
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) return;
|
|
224
|
-
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
225
220
|
}
|
|
221
|
+
|
|
222
|
+
if (this.isEntry(keyOrNodeOrEntry)) {
|
|
223
|
+
const key = keyOrNodeOrEntry[0];
|
|
224
|
+
if (key === null || key === undefined) return;
|
|
225
|
+
return this.getNodeByKey(key, iterationType);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const key = keyOrNodeOrEntry;
|
|
229
|
+
if (key === null || key === undefined) return;
|
|
230
|
+
return this.getNodeByKey(key, iterationType);
|
|
226
231
|
}
|
|
227
232
|
|
|
228
233
|
/**
|
|
@@ -406,51 +411,6 @@ export class BST<
|
|
|
406
411
|
return inserted;
|
|
407
412
|
}
|
|
408
413
|
|
|
409
|
-
/**
|
|
410
|
-
* Time Complexity: O(log n)
|
|
411
|
-
* Space Complexity: O(1)
|
|
412
|
-
*/
|
|
413
|
-
|
|
414
|
-
/**
|
|
415
|
-
* Time Complexity: O(log n)
|
|
416
|
-
* Space Complexity: O(1)
|
|
417
|
-
*
|
|
418
|
-
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
419
|
-
* either recursive or iterative methods.
|
|
420
|
-
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
421
|
-
* It is used to identify the node that we want to retrieve.
|
|
422
|
-
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
423
|
-
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
424
|
-
* values:
|
|
425
|
-
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
426
|
-
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
427
|
-
*/
|
|
428
|
-
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
429
|
-
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
430
|
-
if (!this.isRealNode(this.root)) return;
|
|
431
|
-
if (iterationType === 'RECURSIVE') {
|
|
432
|
-
const dfs = (cur: NODE): NODE | undefined => {
|
|
433
|
-
if (cur.key === key) return cur;
|
|
434
|
-
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
435
|
-
|
|
436
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') return dfs(cur.left);
|
|
437
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') return dfs(cur.right);
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
return dfs(this.root);
|
|
441
|
-
} else {
|
|
442
|
-
const stack = [this.root];
|
|
443
|
-
while (stack.length > 0) {
|
|
444
|
-
const cur = stack.pop();
|
|
445
|
-
if (this.isRealNode(cur)) {
|
|
446
|
-
if (this._compare(cur.key, key) === 'EQ') return cur;
|
|
447
|
-
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT') stack.push(cur.left);
|
|
448
|
-
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT') stack.push(cur.right);
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
414
|
/**
|
|
455
415
|
* Time Complexity: O(log n)
|
|
456
416
|
* Space Complexity: O(k + log n)
|
|
@@ -488,6 +448,7 @@ export class BST<
|
|
|
488
448
|
): NODE[] {
|
|
489
449
|
beginRoot = this.ensureNode(beginRoot);
|
|
490
450
|
if (!beginRoot) return [];
|
|
451
|
+
callback = this._ensureCallback(identifier, callback);
|
|
491
452
|
const ans: NODE[] = [];
|
|
492
453
|
|
|
493
454
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -513,29 +474,27 @@ export class BST<
|
|
|
513
474
|
} else {
|
|
514
475
|
const stack = [beginRoot];
|
|
515
476
|
while (stack.length > 0) {
|
|
516
|
-
const cur = stack.pop()
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
if (
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
538
|
-
}
|
|
477
|
+
const cur = stack.pop()!;
|
|
478
|
+
const callbackResult = callback(cur);
|
|
479
|
+
if (callbackResult === identifier) {
|
|
480
|
+
ans.push(cur);
|
|
481
|
+
if (onlyOne) return ans;
|
|
482
|
+
}
|
|
483
|
+
// TODO potential bug
|
|
484
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
485
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
|
|
486
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
|
|
487
|
+
|
|
488
|
+
// if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
|
|
489
|
+
// if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
|
|
490
|
+
|
|
491
|
+
// // @ts-ignore
|
|
492
|
+
// if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
|
|
493
|
+
// // @ts-ignore
|
|
494
|
+
// if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
|
|
495
|
+
} else {
|
|
496
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
497
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
539
498
|
}
|
|
540
499
|
}
|
|
541
500
|
}
|
|
@@ -578,6 +537,29 @@ export class BST<
|
|
|
578
537
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
579
538
|
}
|
|
580
539
|
|
|
540
|
+
/**
|
|
541
|
+
* Time Complexity: O(log n)
|
|
542
|
+
* Space Complexity: O(1)
|
|
543
|
+
*/
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Time Complexity: O(log n)
|
|
547
|
+
* Space Complexity: O(1)
|
|
548
|
+
*
|
|
549
|
+
* The function `getNodeByKey` searches for a node in a binary tree based on a given key, using
|
|
550
|
+
* either recursive or iterative methods.
|
|
551
|
+
* @param {K} key - The `key` parameter is the key value that we are searching for in the tree.
|
|
552
|
+
* It is used to identify the node that we want to retrieve.
|
|
553
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
554
|
+
* type of iteration to use when searching for a node in the binary tree. It can have two possible
|
|
555
|
+
* values:
|
|
556
|
+
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
557
|
+
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
558
|
+
*/
|
|
559
|
+
override getNodeByKey(key: K, iterationType: IterationType = 'ITERATIVE'): NODE | undefined {
|
|
560
|
+
return this.getNode(key, this._DEFAULT_CALLBACK, this.root, iterationType);
|
|
561
|
+
}
|
|
562
|
+
|
|
581
563
|
/**
|
|
582
564
|
* Time complexity: O(n)
|
|
583
565
|
* Space complexity: O(n)
|
|
@@ -920,7 +902,9 @@ export class BST<
|
|
|
920
902
|
const extractedB = this.extractor(b);
|
|
921
903
|
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
922
904
|
|
|
923
|
-
|
|
905
|
+
if (compared > 0) return 'GT';
|
|
906
|
+
if (compared < 0) return 'LT';
|
|
907
|
+
return 'EQ';
|
|
924
908
|
}
|
|
925
909
|
|
|
926
910
|
/**
|
|
@@ -935,10 +919,7 @@ export class BST<
|
|
|
935
919
|
protected _lt(a: K, b: K): boolean {
|
|
936
920
|
const extractedA = this.extractor(a);
|
|
937
921
|
const extractedB = this.extractor(b);
|
|
938
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
|
|
939
922
|
return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
|
|
940
|
-
// return extractedA < extractedB;
|
|
941
|
-
// return a < b;
|
|
942
923
|
}
|
|
943
924
|
|
|
944
925
|
/**
|
|
@@ -952,9 +933,6 @@ export class BST<
|
|
|
952
933
|
protected _gt(a: K, b: K): boolean {
|
|
953
934
|
const extractedA = this.extractor(a);
|
|
954
935
|
const extractedB = this.extractor(b);
|
|
955
|
-
// return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
|
|
956
936
|
return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
|
|
957
|
-
// return extractedA > extractedB;
|
|
958
|
-
// return a > b;
|
|
959
937
|
}
|
|
960
938
|
}
|
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
RedBlackTreeNested,
|
|
7
7
|
RedBlackTreeNodeNested
|
|
8
8
|
} from '../../types';
|
|
9
|
-
import { CRUD, RBTNColor } from '../../types';
|
|
9
|
+
import { CP, CRUD, RBTNColor } from '../../types';
|
|
10
10
|
import { BST, BSTNode } from './bst';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
|
|
@@ -252,7 +252,7 @@ export class RedBlackTree<
|
|
|
252
252
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
253
253
|
if (identifier === null) return [];
|
|
254
254
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
255
|
-
|
|
255
|
+
callback = this._ensureCallback(identifier, callback);
|
|
256
256
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
257
257
|
|
|
258
258
|
if (!nodeToDelete) {
|
|
@@ -656,4 +656,21 @@ export class RedBlackTree<
|
|
|
656
656
|
x.right = y;
|
|
657
657
|
y.parent = x;
|
|
658
658
|
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* The function compares two values using a comparator function and returns whether the first value
|
|
662
|
+
* is greater than, less than, or equal to the second value.
|
|
663
|
+
* @param {K} a - The parameter "a" is of type K.
|
|
664
|
+
* @param {K} b - The parameter "b" in the above code represents a K.
|
|
665
|
+
* @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
|
|
666
|
+
* than), 'LT' (less than), or 'EQ' (equal).
|
|
667
|
+
*/
|
|
668
|
+
protected override _compare(a: K, b: K): CP {
|
|
669
|
+
const extractedA = this.extractor(a);
|
|
670
|
+
const extractedB = this.extractor(b);
|
|
671
|
+
const compared = extractedA - extractedB;
|
|
672
|
+
if (compared > 0) return 'GT';
|
|
673
|
+
if (compared < 0) return 'LT';
|
|
674
|
+
return 'EQ';
|
|
675
|
+
}
|
|
659
676
|
}
|
|
@@ -257,6 +257,7 @@ export class TreeMultiMap<
|
|
|
257
257
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
258
258
|
if (identifier === null) return [];
|
|
259
259
|
const results: BinaryTreeDeleteResult<NODE>[] = [];
|
|
260
|
+
callback = this._ensureCallback(identifier, callback);
|
|
260
261
|
|
|
261
262
|
const nodeToDelete = this.isRealNode(identifier) ? identifier : this.getNode(identifier, callback);
|
|
262
263
|
|
|
@@ -6,26 +6,26 @@ const suite = new Benchmark.Suite();
|
|
|
6
6
|
const rbTree = new RedBlackTree();
|
|
7
7
|
const avlTree = new AVLTree();
|
|
8
8
|
const { TEN_THOUSAND } = magnitude;
|
|
9
|
-
const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND, true);
|
|
9
|
+
const arr = getRandomIntArray(TEN_THOUSAND, 0, TEN_THOUSAND - 1, true);
|
|
10
10
|
|
|
11
11
|
suite
|
|
12
|
-
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add`, () => {
|
|
12
|
+
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add randomly`, () => {
|
|
13
13
|
rbTree.clear();
|
|
14
14
|
for (let i = 0; i < arr.length; i++) rbTree.add(arr[i]);
|
|
15
15
|
})
|
|
16
|
+
.add(`${TEN_THOUSAND.toLocaleString()} RBTree get randomly`, () => {
|
|
17
|
+
for (let i = 0; i < arr.length; i++) rbTree.get(arr[i]);
|
|
18
|
+
})
|
|
16
19
|
.add(`${TEN_THOUSAND.toLocaleString()} RBTree add & delete randomly`, () => {
|
|
17
20
|
rbTree.clear();
|
|
18
21
|
for (let i = 0; i < arr.length; i++) rbTree.add(arr[i]);
|
|
19
22
|
for (let i = 0; i < arr.length; i++) rbTree.delete(arr[i]);
|
|
20
23
|
})
|
|
21
|
-
.add(`${TEN_THOUSAND.toLocaleString()}
|
|
22
|
-
for (let i = 0; i < arr.length; i++) rbTree.get(arr[i]);
|
|
23
|
-
})
|
|
24
|
-
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add`, () => {
|
|
24
|
+
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add randomly`, () => {
|
|
25
25
|
avlTree.clear();
|
|
26
26
|
for (let i = 0; i < arr.length; i++) avlTree.add(arr[i]);
|
|
27
27
|
})
|
|
28
|
-
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get`, () => {
|
|
28
|
+
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree get randomly`, () => {
|
|
29
29
|
for (let i = 0; i < arr.length; i++) avlTree.get(arr[i]);
|
|
30
30
|
})
|
|
31
31
|
.add(`${TEN_THOUSAND.toLocaleString()} AVLTree add & delete randomly`, () => {
|