priority-queue-typed 1.50.5 → 1.50.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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.js +32 -28
- package/dist/data-structures/binary-tree/bst.js +17 -17
- package/dist/data-structures/binary-tree/rb-tree.d.ts +158 -141
- package/dist/data-structures/binary-tree/rb-tree.js +416 -396
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +1 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +84 -76
- package/dist/types/common.d.ts +6 -0
- package/dist/types/common.js +8 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +5 -1
- package/src/data-structures/binary-tree/avl-tree.ts +1 -1
- package/src/data-structures/binary-tree/binary-tree.ts +31 -29
- package/src/data-structures/binary-tree/bst.ts +18 -18
- package/src/data-structures/binary-tree/rb-tree.ts +436 -405
- package/src/data-structures/binary-tree/tree-multi-map.ts +85 -82
- package/src/types/common.ts +7 -0
|
@@ -46,6 +46,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
46
46
|
* @returns the sum of the count property of all nodes in the tree.
|
|
47
47
|
*/
|
|
48
48
|
get count(): number;
|
|
49
|
+
getMutableCount(): number;
|
|
49
50
|
/**
|
|
50
51
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
51
52
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -53,6 +53,9 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
53
53
|
* @returns the sum of the count property of all nodes in the tree.
|
|
54
54
|
*/
|
|
55
55
|
get count() {
|
|
56
|
+
return this._count;
|
|
57
|
+
}
|
|
58
|
+
getMutableCount() {
|
|
56
59
|
let sum = 0;
|
|
57
60
|
this.dfs(node => (sum += node.count));
|
|
58
61
|
return sum;
|
|
@@ -736,7 +736,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
736
736
|
return true;
|
|
737
737
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
738
738
|
const dfs = (cur, min, max) => {
|
|
739
|
-
if (!cur)
|
|
739
|
+
if (!this.isRealNode(cur))
|
|
740
740
|
return true;
|
|
741
741
|
const numKey = this.extractor(cur.key);
|
|
742
742
|
if (numKey <= min || numKey >= max)
|
|
@@ -753,14 +753,14 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
753
753
|
let prev = checkMax ? Number.MAX_SAFE_INTEGER : Number.MIN_SAFE_INTEGER;
|
|
754
754
|
// @ts-ignore
|
|
755
755
|
let curr = beginRoot;
|
|
756
|
-
while (curr || stack.length > 0) {
|
|
757
|
-
while (curr) {
|
|
756
|
+
while (this.isRealNode(curr) || stack.length > 0) {
|
|
757
|
+
while (this.isRealNode(curr)) {
|
|
758
758
|
stack.push(curr);
|
|
759
759
|
curr = curr.left;
|
|
760
760
|
}
|
|
761
761
|
curr = stack.pop();
|
|
762
762
|
const numKey = this.extractor(curr.key);
|
|
763
|
-
if (!curr || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
763
|
+
if (!this.isRealNode(curr) || (!checkMax && prev >= numKey) || (checkMax && prev <= numKey))
|
|
764
764
|
return false;
|
|
765
765
|
prev = numKey;
|
|
766
766
|
curr = curr.right;
|
|
@@ -821,11 +821,11 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
821
821
|
*/
|
|
822
822
|
getHeight(beginRoot = this.root, iterationType = this.iterationType) {
|
|
823
823
|
beginRoot = this.ensureNode(beginRoot);
|
|
824
|
-
if (!beginRoot)
|
|
824
|
+
if (!this.isRealNode(beginRoot))
|
|
825
825
|
return -1;
|
|
826
826
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
827
827
|
const _getMaxHeight = (cur) => {
|
|
828
|
-
if (!cur)
|
|
828
|
+
if (!this.isRealNode(cur))
|
|
829
829
|
return -1;
|
|
830
830
|
const leftHeight = _getMaxHeight(cur.left);
|
|
831
831
|
const rightHeight = _getMaxHeight(cur.right);
|
|
@@ -838,9 +838,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
838
838
|
let maxHeight = 0;
|
|
839
839
|
while (stack.length > 0) {
|
|
840
840
|
const { node, depth } = stack.pop();
|
|
841
|
-
if (node.left)
|
|
841
|
+
if (this.isRealNode(node.left))
|
|
842
842
|
stack.push({ node: node.left, depth: depth + 1 });
|
|
843
|
-
if (node.right)
|
|
843
|
+
if (this.isRealNode(node.right))
|
|
844
844
|
stack.push({ node: node.right, depth: depth + 1 });
|
|
845
845
|
maxHeight = Math.max(maxHeight, depth);
|
|
846
846
|
}
|
|
@@ -871,9 +871,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
871
871
|
return -1;
|
|
872
872
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
873
873
|
const _getMinHeight = (cur) => {
|
|
874
|
-
if (!cur)
|
|
874
|
+
if (!this.isRealNode(cur))
|
|
875
875
|
return 0;
|
|
876
|
-
if (!cur.left && !cur.right)
|
|
876
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
877
877
|
return 0;
|
|
878
878
|
const leftMinHeight = _getMinHeight(cur.left);
|
|
879
879
|
const rightMinHeight = _getMinHeight(cur.right);
|
|
@@ -886,17 +886,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
886
886
|
let node = beginRoot, last = null;
|
|
887
887
|
const depths = new Map();
|
|
888
888
|
while (stack.length > 0 || node) {
|
|
889
|
-
if (node) {
|
|
889
|
+
if (this.isRealNode(node)) {
|
|
890
890
|
stack.push(node);
|
|
891
891
|
node = node.left;
|
|
892
892
|
}
|
|
893
893
|
else {
|
|
894
894
|
node = stack[stack.length - 1];
|
|
895
|
-
if (!node.right || last === node.right) {
|
|
895
|
+
if (!this.isRealNode(node.right) || last === node.right) {
|
|
896
896
|
node = stack.pop();
|
|
897
|
-
if (node) {
|
|
898
|
-
const leftMinHeight = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
899
|
-
const rightMinHeight = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
897
|
+
if (this.isRealNode(node)) {
|
|
898
|
+
const leftMinHeight = this.isRealNode(node.left) ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
899
|
+
const rightMinHeight = this.isRealNode(node.right) ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
900
900
|
depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
|
|
901
901
|
last = node;
|
|
902
902
|
node = null;
|
|
@@ -962,8 +962,10 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
962
962
|
* is no leftmost node, it returns `null` or `undefined` depending on the input.
|
|
963
963
|
*/
|
|
964
964
|
getLeftMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
965
|
+
if (this.isNIL(beginRoot))
|
|
966
|
+
return beginRoot;
|
|
965
967
|
beginRoot = this.ensureNode(beginRoot);
|
|
966
|
-
if (!beginRoot)
|
|
968
|
+
if (!this.isRealNode(beginRoot))
|
|
967
969
|
return beginRoot;
|
|
968
970
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
969
971
|
const _traverse = (cur) => {
|
|
@@ -1003,6 +1005,8 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1003
1005
|
* is no rightmost node, it returns `null` or `undefined`, depending on the input.
|
|
1004
1006
|
*/
|
|
1005
1007
|
getRightMost(beginRoot = this.root, iterationType = this.iterationType) {
|
|
1008
|
+
if (this.isNIL(beginRoot))
|
|
1009
|
+
return beginRoot;
|
|
1006
1010
|
// TODO support get right most by passing key in
|
|
1007
1011
|
beginRoot = this.ensureNode(beginRoot);
|
|
1008
1012
|
if (!beginRoot)
|
|
@@ -1117,48 +1121,48 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1117
1121
|
switch (pattern) {
|
|
1118
1122
|
case 'in':
|
|
1119
1123
|
if (includeNull) {
|
|
1120
|
-
if (node && this.isNodeOrNull(node.left))
|
|
1124
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1121
1125
|
_traverse(node.left);
|
|
1122
1126
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1123
|
-
if (node && this.isNodeOrNull(node.right))
|
|
1127
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1124
1128
|
_traverse(node.right);
|
|
1125
1129
|
}
|
|
1126
1130
|
else {
|
|
1127
|
-
if (node && node.left)
|
|
1131
|
+
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1128
1132
|
_traverse(node.left);
|
|
1129
1133
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1130
|
-
if (node && node.right)
|
|
1134
|
+
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1131
1135
|
_traverse(node.right);
|
|
1132
1136
|
}
|
|
1133
1137
|
break;
|
|
1134
1138
|
case 'pre':
|
|
1135
1139
|
if (includeNull) {
|
|
1136
1140
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1137
|
-
if (node && this.isNodeOrNull(node.left))
|
|
1141
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1138
1142
|
_traverse(node.left);
|
|
1139
|
-
if (node && this.isNodeOrNull(node.right))
|
|
1143
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1140
1144
|
_traverse(node.right);
|
|
1141
1145
|
}
|
|
1142
1146
|
else {
|
|
1143
1147
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1144
|
-
if (node && node.left)
|
|
1148
|
+
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1145
1149
|
_traverse(node.left);
|
|
1146
|
-
if (node && node.right)
|
|
1150
|
+
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1147
1151
|
_traverse(node.right);
|
|
1148
1152
|
}
|
|
1149
1153
|
break;
|
|
1150
1154
|
case 'post':
|
|
1151
1155
|
if (includeNull) {
|
|
1152
|
-
if (node && this.isNodeOrNull(node.left))
|
|
1156
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1153
1157
|
_traverse(node.left);
|
|
1154
|
-
if (node && this.isNodeOrNull(node.right))
|
|
1158
|
+
if (this.isRealNode(node) && this.isNodeOrNull(node.right))
|
|
1155
1159
|
_traverse(node.right);
|
|
1156
1160
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1157
1161
|
}
|
|
1158
1162
|
else {
|
|
1159
|
-
if (node && node.left)
|
|
1163
|
+
if (this.isRealNode(node) && this.isRealNode(node.left))
|
|
1160
1164
|
_traverse(node.left);
|
|
1161
|
-
if (node && node.right)
|
|
1165
|
+
if (this.isRealNode(node) && this.isRealNode(node.right))
|
|
1162
1166
|
_traverse(node.right);
|
|
1163
1167
|
this.isRealNode(node) && ans.push(callback(node));
|
|
1164
1168
|
}
|
|
@@ -362,17 +362,17 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
362
362
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
363
363
|
*/
|
|
364
364
|
getNodeByKey(key, iterationType = types_1.IterationType.ITERATIVE) {
|
|
365
|
-
if (!this.root)
|
|
365
|
+
if (!this.isRealNode(this.root))
|
|
366
366
|
return undefined;
|
|
367
367
|
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
368
368
|
const _dfs = (cur) => {
|
|
369
369
|
if (cur.key === key)
|
|
370
370
|
return cur;
|
|
371
|
-
if (!cur.left && !cur.right)
|
|
371
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
372
372
|
return;
|
|
373
|
-
if (this._compare(cur.key, key) === types_1.CP.gt && cur.left)
|
|
373
|
+
if (this._compare(cur.key, key) === types_1.CP.gt && this.isRealNode(cur.left))
|
|
374
374
|
return _dfs(cur.left);
|
|
375
|
-
if (this._compare(cur.key, key) === types_1.CP.lt && cur.right)
|
|
375
|
+
if (this._compare(cur.key, key) === types_1.CP.lt && this.isRealNode(cur.right))
|
|
376
376
|
return _dfs(cur.right);
|
|
377
377
|
};
|
|
378
378
|
return _dfs(this.root);
|
|
@@ -381,13 +381,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
381
381
|
const queue = new queue_1.Queue([this.root]);
|
|
382
382
|
while (queue.size > 0) {
|
|
383
383
|
const cur = queue.shift();
|
|
384
|
-
if (cur) {
|
|
384
|
+
if (this.isRealNode(cur)) {
|
|
385
385
|
if (this._compare(cur.key, key) === types_1.CP.eq)
|
|
386
386
|
return cur;
|
|
387
387
|
if (this._compare(cur.key, key) === types_1.CP.gt)
|
|
388
|
-
cur.left && queue.push(cur.left);
|
|
388
|
+
this.isRealNode(cur.left) && queue.push(cur.left);
|
|
389
389
|
if (this._compare(cur.key, key) === types_1.CP.lt)
|
|
390
|
-
cur.right && queue.push(cur.right);
|
|
390
|
+
this.isRealNode(cur.right) && queue.push(cur.right);
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
393
|
}
|
|
@@ -433,18 +433,18 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
433
433
|
if (onlyOne)
|
|
434
434
|
return;
|
|
435
435
|
}
|
|
436
|
-
if (!cur.left && !cur.right)
|
|
436
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right))
|
|
437
437
|
return;
|
|
438
438
|
// TODO potential bug
|
|
439
439
|
if (callback === this._defaultOneParamCallback) {
|
|
440
440
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
441
|
-
cur.left && _traverse(cur.left);
|
|
441
|
+
this.isRealNode(cur.left) && _traverse(cur.left);
|
|
442
442
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
443
|
-
cur.right && _traverse(cur.right);
|
|
443
|
+
this.isRealNode(cur.right) && _traverse(cur.right);
|
|
444
444
|
}
|
|
445
445
|
else {
|
|
446
|
-
cur.left && _traverse(cur.left);
|
|
447
|
-
cur.right && _traverse(cur.right);
|
|
446
|
+
this.isRealNode(cur.left) && _traverse(cur.left);
|
|
447
|
+
this.isRealNode(cur.right) && _traverse(cur.right);
|
|
448
448
|
}
|
|
449
449
|
};
|
|
450
450
|
_traverse(beginRoot);
|
|
@@ -453,7 +453,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
453
453
|
const queue = new queue_1.Queue([beginRoot]);
|
|
454
454
|
while (queue.size > 0) {
|
|
455
455
|
const cur = queue.shift();
|
|
456
|
-
if (cur) {
|
|
456
|
+
if (this.isRealNode(cur)) {
|
|
457
457
|
const callbackResult = callback(cur);
|
|
458
458
|
if (callbackResult === identifier) {
|
|
459
459
|
ans.push(cur);
|
|
@@ -463,13 +463,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
463
463
|
// TODO potential bug
|
|
464
464
|
if (callback === this._defaultOneParamCallback) {
|
|
465
465
|
if (this._compare(cur.key, identifier) === types_1.CP.gt)
|
|
466
|
-
cur.left && queue.push(cur.left);
|
|
466
|
+
this.isRealNode(cur.left) && queue.push(cur.left);
|
|
467
467
|
if (this._compare(cur.key, identifier) === types_1.CP.lt)
|
|
468
|
-
cur.right && queue.push(cur.right);
|
|
468
|
+
this.isRealNode(cur.right) && queue.push(cur.right);
|
|
469
469
|
}
|
|
470
470
|
else {
|
|
471
|
-
cur.left && queue.push(cur.left);
|
|
472
|
-
cur.right && queue.push(cur.right);
|
|
471
|
+
this.isRealNode(cur.left) && queue.push(cur.left);
|
|
472
|
+
this.isRealNode(cur.right) && queue.push(cur.right);
|
|
473
473
|
}
|
|
474
474
|
}
|
|
475
475
|
}
|