data-structure-typed 1.51.0 → 1.51.2
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 +390 -12
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +2 -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 +17 -11
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +68 -39
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +8 -63
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- 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 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +67 -39
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +8 -62
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/dist/umd/data-structure-typed.js +178 -198
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +98 -93
- package/src/data-structures/binary-tree/bst.ts +69 -34
- package/src/data-structures/binary-tree/rb-tree.ts +8 -74
- package/src/data-structures/binary-tree/tree-multi-map.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +1 -0
- package/test/utils/big-o.ts +12 -0
|
@@ -169,19 +169,19 @@ export class BST extends BinaryTree {
|
|
|
169
169
|
* @returns either a node object (NODE) or undefined.
|
|
170
170
|
*/
|
|
171
171
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
172
|
-
let res;
|
|
173
172
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
174
|
-
|
|
173
|
+
return keyOrNodeOrEntry;
|
|
175
174
|
}
|
|
176
175
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
177
|
-
if (keyOrNodeOrEntry[0])
|
|
178
|
-
|
|
176
|
+
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined)
|
|
177
|
+
return;
|
|
178
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
179
179
|
}
|
|
180
180
|
else {
|
|
181
|
-
if (keyOrNodeOrEntry)
|
|
182
|
-
|
|
181
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
|
|
182
|
+
return;
|
|
183
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
183
184
|
}
|
|
184
|
-
return res;
|
|
185
185
|
}
|
|
186
186
|
/**
|
|
187
187
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -365,33 +365,33 @@ export class BST extends BinaryTree {
|
|
|
365
365
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
366
366
|
*/
|
|
367
367
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
368
|
-
// return this.getNodes(key, this.
|
|
368
|
+
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
369
369
|
if (!this.isRealNode(this.root))
|
|
370
|
-
return
|
|
370
|
+
return;
|
|
371
371
|
if (iterationType === 'RECURSIVE') {
|
|
372
|
-
const
|
|
372
|
+
const dfs = (cur) => {
|
|
373
373
|
if (cur.key === key)
|
|
374
374
|
return cur;
|
|
375
375
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
376
376
|
return;
|
|
377
|
-
if (this._compare(cur.key, key) === 'GT'
|
|
378
|
-
return
|
|
379
|
-
if (this._compare(cur.key, key) === 'LT'
|
|
380
|
-
return
|
|
377
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
378
|
+
return dfs(cur.left);
|
|
379
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
380
|
+
return dfs(cur.right);
|
|
381
381
|
};
|
|
382
|
-
return
|
|
382
|
+
return dfs(this.root);
|
|
383
383
|
}
|
|
384
384
|
else {
|
|
385
|
-
const
|
|
386
|
-
while (
|
|
387
|
-
const cur =
|
|
385
|
+
const stack = [this.root];
|
|
386
|
+
while (stack.length > 0) {
|
|
387
|
+
const cur = stack.pop();
|
|
388
388
|
if (this.isRealNode(cur)) {
|
|
389
389
|
if (this._compare(cur.key, key) === 'EQ')
|
|
390
390
|
return cur;
|
|
391
|
-
if (this._compare(cur.key, key) === 'GT')
|
|
392
|
-
|
|
393
|
-
if (this._compare(cur.key, key) === 'LT')
|
|
394
|
-
|
|
391
|
+
if (this.isRealNode(cur.left) && this._compare(cur.key, key) === 'GT')
|
|
392
|
+
stack.push(cur.left);
|
|
393
|
+
if (this.isRealNode(cur.right) && this._compare(cur.key, key) === 'LT')
|
|
394
|
+
stack.push(cur.right);
|
|
395
395
|
}
|
|
396
396
|
}
|
|
397
397
|
}
|
|
@@ -424,13 +424,13 @@ export class BST extends BinaryTree {
|
|
|
424
424
|
* performed on the binary tree. It can have two possible values:
|
|
425
425
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
426
426
|
*/
|
|
427
|
-
getNodes(identifier, callback = this.
|
|
427
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
428
428
|
beginRoot = this.ensureNode(beginRoot);
|
|
429
429
|
if (!beginRoot)
|
|
430
430
|
return [];
|
|
431
431
|
const ans = [];
|
|
432
432
|
if (iterationType === 'RECURSIVE') {
|
|
433
|
-
const
|
|
433
|
+
const dfs = (cur) => {
|
|
434
434
|
const callbackResult = callback(cur);
|
|
435
435
|
if (callbackResult === identifier) {
|
|
436
436
|
ans.push(cur);
|
|
@@ -440,18 +440,18 @@ export class BST extends BinaryTree {
|
|
|
440
440
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
441
441
|
return;
|
|
442
442
|
// TODO potential bug
|
|
443
|
-
if (callback === this.
|
|
443
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
444
444
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
445
|
-
|
|
445
|
+
dfs(cur.left);
|
|
446
446
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
447
|
-
|
|
447
|
+
dfs(cur.right);
|
|
448
448
|
}
|
|
449
449
|
else {
|
|
450
|
-
this.isRealNode(cur.left) &&
|
|
451
|
-
this.isRealNode(cur.right) &&
|
|
450
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
451
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
452
452
|
}
|
|
453
453
|
};
|
|
454
|
-
|
|
454
|
+
dfs(beginRoot);
|
|
455
455
|
}
|
|
456
456
|
else {
|
|
457
457
|
const stack = [beginRoot];
|
|
@@ -465,7 +465,7 @@ export class BST extends BinaryTree {
|
|
|
465
465
|
return ans;
|
|
466
466
|
}
|
|
467
467
|
// TODO potential bug
|
|
468
|
-
if (callback === this.
|
|
468
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
469
469
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
470
470
|
stack.push(cur.right);
|
|
471
471
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
@@ -486,6 +486,34 @@ export class BST extends BinaryTree {
|
|
|
486
486
|
}
|
|
487
487
|
return ans;
|
|
488
488
|
}
|
|
489
|
+
/**
|
|
490
|
+
* Time Complexity: O(log n)
|
|
491
|
+
* Space Complexity: O(1)
|
|
492
|
+
*/
|
|
493
|
+
/**
|
|
494
|
+
* Time Complexity: O(log n)
|
|
495
|
+
* Space Complexity: O(1)
|
|
496
|
+
*
|
|
497
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
498
|
+
* callback function.
|
|
499
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
500
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
501
|
+
* with the type of nodes in the tree.
|
|
502
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
503
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
504
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
505
|
+
* `identifier` parameter.
|
|
506
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
507
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
508
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
509
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
510
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
511
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
512
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
513
|
+
*/
|
|
514
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
515
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
516
|
+
}
|
|
489
517
|
/**
|
|
490
518
|
* Time complexity: O(n)
|
|
491
519
|
* Space complexity: O(n)
|
|
@@ -509,7 +537,7 @@ export class BST extends BinaryTree {
|
|
|
509
537
|
* following values:
|
|
510
538
|
* @returns The method is returning an array of the return type of the callback function.
|
|
511
539
|
*/
|
|
512
|
-
dfs(callback = this.
|
|
540
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
513
541
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
514
542
|
}
|
|
515
543
|
/**
|
|
@@ -533,7 +561,7 @@ export class BST extends BinaryTree {
|
|
|
533
561
|
* nodes are visited.
|
|
534
562
|
* @returns The method is returning an array of the return type of the callback function.
|
|
535
563
|
*/
|
|
536
|
-
bfs(callback = this.
|
|
564
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
537
565
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
538
566
|
}
|
|
539
567
|
/**
|
|
@@ -558,7 +586,7 @@ export class BST extends BinaryTree {
|
|
|
558
586
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
559
587
|
* function.
|
|
560
588
|
*/
|
|
561
|
-
listLevels(callback = this.
|
|
589
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
562
590
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
563
591
|
}
|
|
564
592
|
/**
|
|
@@ -621,7 +649,7 @@ export class BST extends BinaryTree {
|
|
|
621
649
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
622
650
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
623
651
|
*/
|
|
624
|
-
lesserOrGreaterTraverse(callback = this.
|
|
652
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
|
|
625
653
|
targetNode = this.ensureNode(targetNode);
|
|
626
654
|
const ans = [];
|
|
627
655
|
if (!targetNode)
|
|
@@ -630,16 +658,16 @@ export class BST extends BinaryTree {
|
|
|
630
658
|
return ans;
|
|
631
659
|
const targetKey = targetNode.key;
|
|
632
660
|
if (iterationType === 'RECURSIVE') {
|
|
633
|
-
const
|
|
661
|
+
const dfs = (cur) => {
|
|
634
662
|
const compared = this._compare(cur.key, targetKey);
|
|
635
663
|
if (compared === lesserOrGreater)
|
|
636
664
|
ans.push(callback(cur));
|
|
637
665
|
if (this.isRealNode(cur.left))
|
|
638
|
-
|
|
666
|
+
dfs(cur.left);
|
|
639
667
|
if (this.isRealNode(cur.right))
|
|
640
|
-
|
|
668
|
+
dfs(cur.right);
|
|
641
669
|
};
|
|
642
|
-
|
|
670
|
+
dfs(this.root);
|
|
643
671
|
return ans;
|
|
644
672
|
}
|
|
645
673
|
else {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult,
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { CRUD, RBTNColor } from '../../types';
|
|
3
3
|
import { BST, BSTNode } from './bst';
|
|
4
4
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -39,12 +39,6 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
39
39
|
* should compare keys and
|
|
40
40
|
*/
|
|
41
41
|
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: RBTreeOptions<K>);
|
|
42
|
-
protected _SENTINEL: NODE;
|
|
43
|
-
/**
|
|
44
|
-
* The function returns the value of the _SENTINEL property.
|
|
45
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
46
|
-
*/
|
|
47
|
-
get SENTINEL(): NODE;
|
|
48
42
|
protected _root: NODE | undefined;
|
|
49
43
|
/**
|
|
50
44
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -101,46 +95,6 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
101
95
|
* @returns {boolean} - `true` if the object is a Red-Black Tree node, `false` otherwise.
|
|
102
96
|
*/
|
|
103
97
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
104
|
-
/**
|
|
105
|
-
* Time Complexity: O(1)
|
|
106
|
-
* Space Complexity: O(1)
|
|
107
|
-
*/
|
|
108
|
-
/**
|
|
109
|
-
* Time Complexity: O(1)
|
|
110
|
-
* Space Complexity: O(1)
|
|
111
|
-
*
|
|
112
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
113
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
114
|
-
* it can either be of type `NODE` or `undefined`.
|
|
115
|
-
* @returns a boolean value.
|
|
116
|
-
*/
|
|
117
|
-
isRealNode(node: NODE | undefined): node is NODE;
|
|
118
|
-
/**
|
|
119
|
-
* Time Complexity: O(log n)
|
|
120
|
-
* Space Complexity: O(1)
|
|
121
|
-
*/
|
|
122
|
-
/**
|
|
123
|
-
* Time Complexity: O(log n)
|
|
124
|
-
* Space Complexity: O(1)
|
|
125
|
-
*
|
|
126
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
127
|
-
* callback function.
|
|
128
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
129
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
130
|
-
* with the type of nodes in the tree.
|
|
131
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
132
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
133
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
134
|
-
* `identifier` parameter.
|
|
135
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
136
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
137
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
138
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
139
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
140
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
141
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
142
|
-
*/
|
|
143
|
-
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | null | undefined;
|
|
144
98
|
/**
|
|
145
99
|
* Time Complexity: O(1)
|
|
146
100
|
* Space Complexity: O(1)
|
|
@@ -187,7 +141,7 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
187
141
|
* deleted is not found.
|
|
188
142
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
189
143
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
190
|
-
* `
|
|
144
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
191
145
|
* return the identifier of the node to
|
|
192
146
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
193
147
|
*/
|
|
@@ -44,19 +44,11 @@ export class RedBlackTree extends BST {
|
|
|
44
44
|
*/
|
|
45
45
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
46
46
|
super([], options);
|
|
47
|
-
this._root = this.
|
|
47
|
+
this._root = this.NIL;
|
|
48
48
|
if (keysOrNodesOrEntries) {
|
|
49
49
|
this.addMany(keysOrNodesOrEntries);
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
_SENTINEL = new RedBlackTreeNode(NaN);
|
|
53
|
-
/**
|
|
54
|
-
* The function returns the value of the _SENTINEL property.
|
|
55
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
56
|
-
*/
|
|
57
|
-
get SENTINEL() {
|
|
58
|
-
return this._SENTINEL;
|
|
59
|
-
}
|
|
60
52
|
_root;
|
|
61
53
|
/**
|
|
62
54
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -148,52 +140,6 @@ export class RedBlackTree extends BST {
|
|
|
148
140
|
isNode(keyOrNodeOrEntry) {
|
|
149
141
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
150
142
|
}
|
|
151
|
-
/**
|
|
152
|
-
* Time Complexity: O(1)
|
|
153
|
-
* Space Complexity: O(1)
|
|
154
|
-
*/
|
|
155
|
-
/**
|
|
156
|
-
* Time Complexity: O(1)
|
|
157
|
-
* Space Complexity: O(1)
|
|
158
|
-
*
|
|
159
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
160
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
161
|
-
* it can either be of type `NODE` or `undefined`.
|
|
162
|
-
* @returns a boolean value.
|
|
163
|
-
*/
|
|
164
|
-
isRealNode(node) {
|
|
165
|
-
if (node === this.SENTINEL || node === undefined)
|
|
166
|
-
return false;
|
|
167
|
-
return node instanceof RedBlackTreeNode;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Time Complexity: O(log n)
|
|
171
|
-
* Space Complexity: O(1)
|
|
172
|
-
*/
|
|
173
|
-
/**
|
|
174
|
-
* Time Complexity: O(log n)
|
|
175
|
-
* Space Complexity: O(1)
|
|
176
|
-
*
|
|
177
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
178
|
-
* callback function.
|
|
179
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
180
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
181
|
-
* with the type of nodes in the tree.
|
|
182
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
183
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
184
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
185
|
-
* `identifier` parameter.
|
|
186
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
187
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
188
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
189
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
190
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
191
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
192
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
193
|
-
*/
|
|
194
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
195
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
196
|
-
}
|
|
197
143
|
/**
|
|
198
144
|
* Time Complexity: O(1)
|
|
199
145
|
* Space Complexity: O(1)
|
|
@@ -207,7 +153,7 @@ export class RedBlackTree extends BST {
|
|
|
207
153
|
*/
|
|
208
154
|
clear() {
|
|
209
155
|
super.clear();
|
|
210
|
-
this._root = this.
|
|
156
|
+
this._root = this.NIL;
|
|
211
157
|
}
|
|
212
158
|
/**
|
|
213
159
|
* Time Complexity: O(log n)
|
|
@@ -261,11 +207,11 @@ export class RedBlackTree extends BST {
|
|
|
261
207
|
* deleted is not found.
|
|
262
208
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
263
209
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
264
|
-
* `
|
|
210
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
265
211
|
* return the identifier of the node to
|
|
266
212
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
267
213
|
*/
|
|
268
|
-
delete(identifier, callback = this.
|
|
214
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
269
215
|
if (identifier === null)
|
|
270
216
|
return [];
|
|
271
217
|
const results = [];
|
|
@@ -368,10 +314,10 @@ export class RedBlackTree extends BST {
|
|
|
368
314
|
while (this.isRealNode(current)) {
|
|
369
315
|
parent = current;
|
|
370
316
|
if (node.key < current.key) {
|
|
371
|
-
current = current.left ?? this.
|
|
317
|
+
current = current.left ?? this.NIL;
|
|
372
318
|
}
|
|
373
319
|
else if (node.key > current.key) {
|
|
374
|
-
current = current.right ?? this.
|
|
320
|
+
current = current.right ?? this.NIL;
|
|
375
321
|
}
|
|
376
322
|
else {
|
|
377
323
|
this._replaceNode(current, node);
|
|
@@ -388,8 +334,8 @@ export class RedBlackTree extends BST {
|
|
|
388
334
|
else {
|
|
389
335
|
parent.right = node;
|
|
390
336
|
}
|
|
391
|
-
node.left = this.
|
|
392
|
-
node.right = this.
|
|
337
|
+
node.left = this.NIL;
|
|
338
|
+
node.right = this.NIL;
|
|
393
339
|
node.color = 'RED';
|
|
394
340
|
this._insertFixup(node);
|
|
395
341
|
return 'CREATED';
|
|
@@ -145,7 +145,7 @@ export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNod
|
|
|
145
145
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
146
146
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
147
147
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
148
|
-
* identifier for deletion. If no callback is provided, the `
|
|
148
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
149
149
|
* used
|
|
150
150
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
151
151
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -197,7 +197,7 @@ export class TreeMultiMap extends RedBlackTree {
|
|
|
197
197
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
198
198
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
199
199
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
200
|
-
* identifier for deletion. If no callback is provided, the `
|
|
200
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
201
201
|
* used
|
|
202
202
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
203
203
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -205,7 +205,7 @@ export class TreeMultiMap extends RedBlackTree {
|
|
|
205
205
|
* target node will be decremented
|
|
206
206
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
207
207
|
*/
|
|
208
|
-
delete(identifier, callback = this.
|
|
208
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
209
209
|
if (identifier === null)
|
|
210
210
|
return [];
|
|
211
211
|
const results = [];
|