graph-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/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +17 -11
- package/dist/data-structures/binary-tree/binary-tree.js +98 -92
- package/dist/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/data-structures/binary-tree/bst.js +68 -39
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -48
- package/dist/data-structures/binary-tree/rb-tree.js +8 -63
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multi-map.js +2 -2
- package/package.json +2 -2
- 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
|
@@ -165,19 +165,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
165
165
|
* @returns either a node object (NODE) or undefined.
|
|
166
166
|
*/
|
|
167
167
|
ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
|
|
168
|
-
let res;
|
|
169
168
|
if (this.isRealNode(keyOrNodeOrEntry)) {
|
|
170
|
-
|
|
169
|
+
return keyOrNodeOrEntry;
|
|
171
170
|
}
|
|
172
171
|
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
173
|
-
if (keyOrNodeOrEntry[0])
|
|
174
|
-
|
|
172
|
+
if (keyOrNodeOrEntry[0] === null || keyOrNodeOrEntry[0] === undefined)
|
|
173
|
+
return;
|
|
174
|
+
return this.getNodeByKey(keyOrNodeOrEntry[0], iterationType);
|
|
175
175
|
}
|
|
176
176
|
else {
|
|
177
|
-
if (keyOrNodeOrEntry)
|
|
178
|
-
|
|
177
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined)
|
|
178
|
+
return;
|
|
179
|
+
return this.getNodeByKey(keyOrNodeOrEntry, iterationType);
|
|
179
180
|
}
|
|
180
|
-
return res;
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
@@ -361,33 +361,33 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
361
361
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
362
362
|
*/
|
|
363
363
|
getNodeByKey(key, iterationType = 'ITERATIVE') {
|
|
364
|
-
// return this.getNodes(key, this.
|
|
364
|
+
// return this.getNodes(key, this._DEFAULT_CALLBACK, true, this.root, iterationType)[0];
|
|
365
365
|
if (!this.isRealNode(this.root))
|
|
366
|
-
return
|
|
366
|
+
return;
|
|
367
367
|
if (iterationType === 'RECURSIVE') {
|
|
368
|
-
const
|
|
368
|
+
const dfs = (cur) => {
|
|
369
369
|
if (cur.key === key)
|
|
370
370
|
return cur;
|
|
371
371
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
372
|
return;
|
|
373
|
-
if (this._compare(cur.key, key) === 'GT'
|
|
374
|
-
return
|
|
375
|
-
if (this._compare(cur.key, key) === 'LT'
|
|
376
|
-
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
377
|
};
|
|
378
|
-
return
|
|
378
|
+
return dfs(this.root);
|
|
379
379
|
}
|
|
380
380
|
else {
|
|
381
|
-
const
|
|
382
|
-
while (
|
|
383
|
-
const cur =
|
|
381
|
+
const stack = [this.root];
|
|
382
|
+
while (stack.length > 0) {
|
|
383
|
+
const cur = stack.pop();
|
|
384
384
|
if (this.isRealNode(cur)) {
|
|
385
385
|
if (this._compare(cur.key, key) === 'EQ')
|
|
386
386
|
return cur;
|
|
387
|
-
if (this._compare(cur.key, key) === 'GT')
|
|
388
|
-
|
|
389
|
-
if (this._compare(cur.key, key) === 'LT')
|
|
390
|
-
|
|
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
391
|
}
|
|
392
392
|
}
|
|
393
393
|
}
|
|
@@ -420,13 +420,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
420
420
|
* performed on the binary tree. It can have two possible values:
|
|
421
421
|
* @returns The method returns an array of nodes (`NODE[]`).
|
|
422
422
|
*/
|
|
423
|
-
getNodes(identifier, callback = this.
|
|
423
|
+
getNodes(identifier, callback = this._DEFAULT_CALLBACK, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
424
424
|
beginRoot = this.ensureNode(beginRoot);
|
|
425
425
|
if (!beginRoot)
|
|
426
426
|
return [];
|
|
427
427
|
const ans = [];
|
|
428
428
|
if (iterationType === 'RECURSIVE') {
|
|
429
|
-
const
|
|
429
|
+
const dfs = (cur) => {
|
|
430
430
|
const callbackResult = callback(cur);
|
|
431
431
|
if (callbackResult === identifier) {
|
|
432
432
|
ans.push(cur);
|
|
@@ -436,18 +436,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
436
436
|
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
437
437
|
return;
|
|
438
438
|
// TODO potential bug
|
|
439
|
-
if (callback === this.
|
|
439
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
440
440
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
441
|
-
|
|
441
|
+
dfs(cur.left);
|
|
442
442
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
443
|
-
|
|
443
|
+
dfs(cur.right);
|
|
444
444
|
}
|
|
445
445
|
else {
|
|
446
|
-
this.isRealNode(cur.left) &&
|
|
447
|
-
this.isRealNode(cur.right) &&
|
|
446
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
447
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
448
448
|
}
|
|
449
449
|
};
|
|
450
|
-
|
|
450
|
+
dfs(beginRoot);
|
|
451
451
|
}
|
|
452
452
|
else {
|
|
453
453
|
const stack = [beginRoot];
|
|
@@ -461,7 +461,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
461
461
|
return ans;
|
|
462
462
|
}
|
|
463
463
|
// TODO potential bug
|
|
464
|
-
if (callback === this.
|
|
464
|
+
if (callback === this._DEFAULT_CALLBACK) {
|
|
465
465
|
if (this.isRealNode(cur.right) && this._compare(cur.key, identifier) === 'LT')
|
|
466
466
|
stack.push(cur.right);
|
|
467
467
|
if (this.isRealNode(cur.left) && this._compare(cur.key, identifier) === 'GT')
|
|
@@ -482,6 +482,35 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
482
482
|
}
|
|
483
483
|
return ans;
|
|
484
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* Time Complexity: O(log n)
|
|
487
|
+
* Space Complexity: O(1)
|
|
488
|
+
*/
|
|
489
|
+
/**
|
|
490
|
+
* Time Complexity: O(log n)
|
|
491
|
+
* Space Complexity: O(1)
|
|
492
|
+
*
|
|
493
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
494
|
+
* callback function.
|
|
495
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
496
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
497
|
+
* with the type of nodes in the tree.
|
|
498
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
499
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
500
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
501
|
+
* `identifier` parameter.
|
|
502
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
503
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
504
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
505
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
506
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
507
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
508
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
509
|
+
*/
|
|
510
|
+
getNode(identifier, callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
511
|
+
var _a;
|
|
512
|
+
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
513
|
+
}
|
|
485
514
|
/**
|
|
486
515
|
* Time complexity: O(n)
|
|
487
516
|
* Space complexity: O(n)
|
|
@@ -505,7 +534,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
505
534
|
* following values:
|
|
506
535
|
* @returns The method is returning an array of the return type of the callback function.
|
|
507
536
|
*/
|
|
508
|
-
dfs(callback = this.
|
|
537
|
+
dfs(callback = this._DEFAULT_CALLBACK, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
509
538
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
510
539
|
}
|
|
511
540
|
/**
|
|
@@ -529,7 +558,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
529
558
|
* nodes are visited.
|
|
530
559
|
* @returns The method is returning an array of the return type of the callback function.
|
|
531
560
|
*/
|
|
532
|
-
bfs(callback = this.
|
|
561
|
+
bfs(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
533
562
|
return super.bfs(callback, beginRoot, iterationType, false);
|
|
534
563
|
}
|
|
535
564
|
/**
|
|
@@ -554,7 +583,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
554
583
|
* @returns The method is returning a two-dimensional array of the return type of the callback
|
|
555
584
|
* function.
|
|
556
585
|
*/
|
|
557
|
-
listLevels(callback = this.
|
|
586
|
+
listLevels(callback = this._DEFAULT_CALLBACK, beginRoot = this.root, iterationType = this.iterationType) {
|
|
558
587
|
return super.listLevels(callback, beginRoot, iterationType, false);
|
|
559
588
|
}
|
|
560
589
|
/**
|
|
@@ -617,7 +646,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
617
646
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
618
647
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
619
648
|
*/
|
|
620
|
-
lesserOrGreaterTraverse(callback = this.
|
|
649
|
+
lesserOrGreaterTraverse(callback = this._DEFAULT_CALLBACK, lesserOrGreater = 'LT', targetNode = this.root, iterationType = this.iterationType) {
|
|
621
650
|
targetNode = this.ensureNode(targetNode);
|
|
622
651
|
const ans = [];
|
|
623
652
|
if (!targetNode)
|
|
@@ -626,16 +655,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
626
655
|
return ans;
|
|
627
656
|
const targetKey = targetNode.key;
|
|
628
657
|
if (iterationType === 'RECURSIVE') {
|
|
629
|
-
const
|
|
658
|
+
const dfs = (cur) => {
|
|
630
659
|
const compared = this._compare(cur.key, targetKey);
|
|
631
660
|
if (compared === lesserOrGreater)
|
|
632
661
|
ans.push(callback(cur));
|
|
633
662
|
if (this.isRealNode(cur.left))
|
|
634
|
-
|
|
663
|
+
dfs(cur.left);
|
|
635
664
|
if (this.isRealNode(cur.right))
|
|
636
|
-
|
|
665
|
+
dfs(cur.right);
|
|
637
666
|
};
|
|
638
|
-
|
|
667
|
+
dfs(this.root);
|
|
639
668
|
return ans;
|
|
640
669
|
}
|
|
641
670
|
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
|
*/
|
|
@@ -47,19 +47,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
47
47
|
*/
|
|
48
48
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
49
49
|
super([], options);
|
|
50
|
-
this.
|
|
51
|
-
this._root = this.SENTINEL;
|
|
50
|
+
this._root = this.NIL;
|
|
52
51
|
if (keysOrNodesOrEntries) {
|
|
53
52
|
this.addMany(keysOrNodesOrEntries);
|
|
54
53
|
}
|
|
55
54
|
}
|
|
56
|
-
/**
|
|
57
|
-
* The function returns the value of the _SENTINEL property.
|
|
58
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
59
|
-
*/
|
|
60
|
-
get SENTINEL() {
|
|
61
|
-
return this._SENTINEL;
|
|
62
|
-
}
|
|
63
55
|
/**
|
|
64
56
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
65
57
|
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
@@ -147,53 +139,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
147
139
|
isNode(keyOrNodeOrEntry) {
|
|
148
140
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
149
141
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Time Complexity: O(1)
|
|
152
|
-
* Space Complexity: O(1)
|
|
153
|
-
*/
|
|
154
|
-
/**
|
|
155
|
-
* Time Complexity: O(1)
|
|
156
|
-
* Space Complexity: O(1)
|
|
157
|
-
*
|
|
158
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
159
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
160
|
-
* it can either be of type `NODE` or `undefined`.
|
|
161
|
-
* @returns a boolean value.
|
|
162
|
-
*/
|
|
163
|
-
isRealNode(node) {
|
|
164
|
-
if (node === this.SENTINEL || node === undefined)
|
|
165
|
-
return false;
|
|
166
|
-
return node instanceof RedBlackTreeNode;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Time Complexity: O(log n)
|
|
170
|
-
* Space Complexity: O(1)
|
|
171
|
-
*/
|
|
172
|
-
/**
|
|
173
|
-
* Time Complexity: O(log n)
|
|
174
|
-
* Space Complexity: O(1)
|
|
175
|
-
*
|
|
176
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
177
|
-
* callback function.
|
|
178
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
179
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
180
|
-
* with the type of nodes in the tree.
|
|
181
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
182
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
183
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
184
|
-
* `identifier` parameter.
|
|
185
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
186
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
187
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
188
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
189
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
190
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
191
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
192
|
-
*/
|
|
193
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
194
|
-
var _a;
|
|
195
|
-
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
196
|
-
}
|
|
197
142
|
/**
|
|
198
143
|
* Time Complexity: O(1)
|
|
199
144
|
* Space Complexity: O(1)
|
|
@@ -207,7 +152,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
207
152
|
*/
|
|
208
153
|
clear() {
|
|
209
154
|
super.clear();
|
|
210
|
-
this._root = this.
|
|
155
|
+
this._root = this.NIL;
|
|
211
156
|
}
|
|
212
157
|
/**
|
|
213
158
|
* Time Complexity: O(log n)
|
|
@@ -261,11 +206,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
261
206
|
* deleted is not found.
|
|
262
207
|
* @param {C} callback - The `callback` parameter is a function that is used to retrieve a node from
|
|
263
208
|
* the binary tree based on its identifier. It is an optional parameter and if not provided, the
|
|
264
|
-
* `
|
|
209
|
+
* `_DEFAULT_CALLBACK` function is used as the default callback. The callback function should
|
|
265
210
|
* return the identifier of the node to
|
|
266
211
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
267
212
|
*/
|
|
268
|
-
delete(identifier, callback = this.
|
|
213
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
269
214
|
if (identifier === null)
|
|
270
215
|
return [];
|
|
271
216
|
const results = [];
|
|
@@ -369,10 +314,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
369
314
|
while (this.isRealNode(current)) {
|
|
370
315
|
parent = current;
|
|
371
316
|
if (node.key < current.key) {
|
|
372
|
-
current = (_a = current.left) !== null && _a !== void 0 ? _a : this.
|
|
317
|
+
current = (_a = current.left) !== null && _a !== void 0 ? _a : this.NIL;
|
|
373
318
|
}
|
|
374
319
|
else if (node.key > current.key) {
|
|
375
|
-
current = (_b = current.right) !== null && _b !== void 0 ? _b : this.
|
|
320
|
+
current = (_b = current.right) !== null && _b !== void 0 ? _b : this.NIL;
|
|
376
321
|
}
|
|
377
322
|
else {
|
|
378
323
|
this._replaceNode(current, node);
|
|
@@ -389,8 +334,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
389
334
|
else {
|
|
390
335
|
parent.right = node;
|
|
391
336
|
}
|
|
392
|
-
node.left = this.
|
|
393
|
-
node.right = this.
|
|
337
|
+
node.left = this.NIL;
|
|
338
|
+
node.right = this.NIL;
|
|
394
339
|
node.color = 'RED';
|
|
395
340
|
this._insertFixup(node);
|
|
396
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
|
|
@@ -198,7 +198,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
198
198
|
* function. It can also be null or undefined if no node needs to be deleted.
|
|
199
199
|
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
200
200
|
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
201
|
-
* identifier for deletion. If no callback is provided, the `
|
|
201
|
+
* identifier for deletion. If no callback is provided, the `_DEFAULT_CALLBACK` function is
|
|
202
202
|
* used
|
|
203
203
|
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
204
204
|
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
@@ -206,7 +206,7 @@ class TreeMultiMap extends rb_tree_1.RedBlackTree {
|
|
|
206
206
|
* target node will be decremented
|
|
207
207
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
208
208
|
*/
|
|
209
|
-
delete(identifier, callback = this.
|
|
209
|
+
delete(identifier, callback = this._DEFAULT_CALLBACK, ignoreCount = false) {
|
|
210
210
|
if (identifier === null)
|
|
211
211
|
return [];
|
|
212
212
|
const results = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.51.
|
|
3
|
+
"version": "1.51.2",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -136,6 +136,6 @@
|
|
|
136
136
|
"typescript": "^4.9.5"
|
|
137
137
|
},
|
|
138
138
|
"dependencies": {
|
|
139
|
-
"data-structure-typed": "^1.51.
|
|
139
|
+
"data-structure-typed": "^1.51.2"
|
|
140
140
|
}
|
|
141
141
|
}
|
|
@@ -240,7 +240,7 @@ export class AVLTreeMultiMap<
|
|
|
240
240
|
*/
|
|
241
241
|
override delete<C extends BTNCallback<NODE>>(
|
|
242
242
|
identifier: ReturnType<C>,
|
|
243
|
-
callback: C = this.
|
|
243
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
244
244
|
ignoreCount = false
|
|
245
245
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
246
246
|
const deletedResult: BinaryTreeDeleteResult<NODE>[] = [];
|
|
@@ -164,13 +164,13 @@ export class AVLTree<
|
|
|
164
164
|
* `callback` function.
|
|
165
165
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
166
166
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
167
|
-
* default to the `
|
|
167
|
+
* default to the `_DEFAULT_CALLBACK` function. The `callback` function should have a single
|
|
168
168
|
* parameter of type `NODE
|
|
169
169
|
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
170
170
|
*/
|
|
171
171
|
override delete<C extends BTNCallback<NODE>>(
|
|
172
172
|
identifier: ReturnType<C>,
|
|
173
|
-
callback: C = this.
|
|
173
|
+
callback: C = this._DEFAULT_CALLBACK as C
|
|
174
174
|
): BinaryTreeDeleteResult<NODE>[] {
|
|
175
175
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
176
176
|
const deletedResults = super.delete(identifier, callback);
|