priority-queue-typed 1.50.8 → 1.50.9
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 +15 -3
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +14 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/binary-tree.js +11 -11
- package/dist/data-structures/binary-tree/bst.d.ts +19 -2
- package/dist/data-structures/binary-tree/bst.js +20 -2
- package/dist/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/data-structures/binary-tree/rb-tree.js +42 -44
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +40 -22
- package/dist/data-structures/binary-tree/tree-multi-map.js +43 -27
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +5 -5
- package/dist/types/common.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +17 -3
- package/src/data-structures/binary-tree/binary-tree.ts +36 -24
- package/src/data-structures/binary-tree/bst.ts +32 -11
- package/src/data-structures/binary-tree/rb-tree.ts +44 -44
- package/src/data-structures/binary-tree/tree-multi-map.ts +46 -27
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/types/common.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -45,7 +45,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
45
45
|
* @returns the sum of the count property of all nodes in the tree.
|
|
46
46
|
*/
|
|
47
47
|
get count(): number;
|
|
48
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Time Complexity: O(n)
|
|
50
|
+
* Space Complexity: O(1)
|
|
51
|
+
*/
|
|
52
|
+
/**
|
|
53
|
+
* Time Complexity: O(n)
|
|
54
|
+
* Space Complexity: O(1)
|
|
55
|
+
*
|
|
56
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
57
|
+
* search.
|
|
58
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
59
|
+
*/
|
|
60
|
+
getComputedCount(): number;
|
|
49
61
|
/**
|
|
50
62
|
* The function creates a new BSTNode with the given key, value, and count.
|
|
51
63
|
* @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
@@ -156,7 +168,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
|
|
|
156
168
|
* values:
|
|
157
169
|
* @returns a boolean value.
|
|
158
170
|
*/
|
|
159
|
-
perfectlyBalance(iterationType?:
|
|
171
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
160
172
|
/**
|
|
161
173
|
* Time complexity: O(n)
|
|
162
174
|
* Space complexity: O(n)
|
|
@@ -54,7 +54,19 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
54
54
|
get count() {
|
|
55
55
|
return this._count;
|
|
56
56
|
}
|
|
57
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Time Complexity: O(n)
|
|
59
|
+
* Space Complexity: O(1)
|
|
60
|
+
*/
|
|
61
|
+
/**
|
|
62
|
+
* Time Complexity: O(n)
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*
|
|
65
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
66
|
+
* search.
|
|
67
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
68
|
+
*/
|
|
69
|
+
getComputedCount() {
|
|
58
70
|
let sum = 0;
|
|
59
71
|
this.dfs(node => (sum += node.count));
|
|
60
72
|
return sum;
|
|
@@ -270,7 +282,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
270
282
|
* @returns a boolean value.
|
|
271
283
|
*/
|
|
272
284
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
273
|
-
const sorted = this.dfs(node => node, '
|
|
285
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
274
286
|
if (sorted.length < 1)
|
|
275
287
|
return false;
|
|
276
288
|
this.clear();
|
|
@@ -139,7 +139,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
139
139
|
* @returns either the node corresponding to the given key if it is a valid node key, or the key
|
|
140
140
|
* itself if it is not a valid node key.
|
|
141
141
|
*/
|
|
142
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
142
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | null | undefined;
|
|
143
143
|
/**
|
|
144
144
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
145
145
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
@@ -248,7 +248,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
|
|
|
248
248
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
249
249
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
250
250
|
*/
|
|
251
|
-
getNodeByKey(key: K, iterationType?:
|
|
251
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
252
252
|
get<C extends BTNCallback<NODE, K>>(identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
253
253
|
get<C extends BTNCallback<NODE, NODE>>(identifier: NODE | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
254
254
|
get<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback: C, beginRoot?: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): V | undefined;
|
|
@@ -1110,7 +1110,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1110
1110
|
* `false`, null or undefined
|
|
1111
1111
|
* @returns an array of values that are the return values of the callback function.
|
|
1112
1112
|
*/
|
|
1113
|
-
dfs(callback = this._defaultOneParamCallback, pattern = '
|
|
1113
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1114
1114
|
beginRoot = this.ensureNode(beginRoot);
|
|
1115
1115
|
if (!beginRoot)
|
|
1116
1116
|
return [];
|
|
@@ -1118,7 +1118,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1118
1118
|
if (iterationType === 'RECURSIVE') {
|
|
1119
1119
|
const _traverse = (node) => {
|
|
1120
1120
|
switch (pattern) {
|
|
1121
|
-
case '
|
|
1121
|
+
case 'IN':
|
|
1122
1122
|
if (includeNull) {
|
|
1123
1123
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1124
1124
|
_traverse(node.left);
|
|
@@ -1134,7 +1134,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1134
1134
|
_traverse(node.right);
|
|
1135
1135
|
}
|
|
1136
1136
|
break;
|
|
1137
|
-
case '
|
|
1137
|
+
case 'PRE':
|
|
1138
1138
|
if (includeNull) {
|
|
1139
1139
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1140
1140
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
@@ -1150,7 +1150,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1150
1150
|
_traverse(node.right);
|
|
1151
1151
|
}
|
|
1152
1152
|
break;
|
|
1153
|
-
case '
|
|
1153
|
+
case 'POST':
|
|
1154
1154
|
if (includeNull) {
|
|
1155
1155
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1156
1156
|
_traverse(node.left);
|
|
@@ -1190,17 +1190,17 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1190
1190
|
}
|
|
1191
1191
|
else {
|
|
1192
1192
|
switch (pattern) {
|
|
1193
|
-
case '
|
|
1193
|
+
case 'IN':
|
|
1194
1194
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1195
1195
|
stack.push({ opt: 1, node: cur.node });
|
|
1196
1196
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1197
1197
|
break;
|
|
1198
|
-
case '
|
|
1198
|
+
case 'PRE':
|
|
1199
1199
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1200
1200
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1201
1201
|
stack.push({ opt: 1, node: cur.node });
|
|
1202
1202
|
break;
|
|
1203
|
-
case '
|
|
1203
|
+
case 'POST':
|
|
1204
1204
|
stack.push({ opt: 1, node: cur.node });
|
|
1205
1205
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1206
1206
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
@@ -1390,7 +1390,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1390
1390
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1391
1391
|
* by the return type of the `callback` function.
|
|
1392
1392
|
*/
|
|
1393
|
-
morris(callback = this._defaultOneParamCallback, pattern = '
|
|
1393
|
+
morris(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root) {
|
|
1394
1394
|
beginRoot = this.ensureNode(beginRoot);
|
|
1395
1395
|
if (beginRoot === null)
|
|
1396
1396
|
return [];
|
|
@@ -1417,7 +1417,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1417
1417
|
_reverseEdge(tail);
|
|
1418
1418
|
};
|
|
1419
1419
|
switch (pattern) {
|
|
1420
|
-
case '
|
|
1420
|
+
case 'IN':
|
|
1421
1421
|
while (cur) {
|
|
1422
1422
|
if (cur.left) {
|
|
1423
1423
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1434,7 +1434,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1434
1434
|
cur = cur.right;
|
|
1435
1435
|
}
|
|
1436
1436
|
break;
|
|
1437
|
-
case '
|
|
1437
|
+
case 'PRE':
|
|
1438
1438
|
while (cur) {
|
|
1439
1439
|
if (cur.left) {
|
|
1440
1440
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1454,7 +1454,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1454
1454
|
cur = cur.right;
|
|
1455
1455
|
}
|
|
1456
1456
|
break;
|
|
1457
|
-
case '
|
|
1457
|
+
case 'POST':
|
|
1458
1458
|
while (cur) {
|
|
1459
1459
|
if (cur.left) {
|
|
1460
1460
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -112,7 +112,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
112
112
|
* type of iteration to be performed. It has a default value of `'ITERATIVE'`.
|
|
113
113
|
* @returns either a node object (NODE) or undefined.
|
|
114
114
|
*/
|
|
115
|
-
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?:
|
|
115
|
+
ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | undefined;
|
|
116
116
|
/**
|
|
117
117
|
* The function checks if an keyOrNodeOrEntry is an instance of BSTNode.
|
|
118
118
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
@@ -179,7 +179,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
179
179
|
* @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
|
|
180
180
|
* found in the binary tree. If no node is found, it returns `undefined`.
|
|
181
181
|
*/
|
|
182
|
-
getNodeByKey(key: K, iterationType?:
|
|
182
|
+
getNodeByKey(key: K, iterationType?: IterationType): NODE | undefined;
|
|
183
183
|
/**
|
|
184
184
|
* Time Complexity: O(log n)
|
|
185
185
|
* Space Complexity: O(k + log n)
|
|
@@ -376,6 +376,23 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
|
|
|
376
376
|
* than), 'LT' (less than), or 'EQ' (equal).
|
|
377
377
|
*/
|
|
378
378
|
protected _compare(a: K, b: K): CP;
|
|
379
|
+
/**
|
|
380
|
+
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
381
|
+
* `a` is less than `b` based on the specified variant.
|
|
382
|
+
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
383
|
+
* first value to be compared in the function.
|
|
384
|
+
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
385
|
+
* of the arguments for the comparison in the `_lt` function.
|
|
386
|
+
* @returns a boolean value.
|
|
387
|
+
*/
|
|
379
388
|
protected _lt(a: K, b: K): boolean;
|
|
389
|
+
/**
|
|
390
|
+
* The function compares two values using a custom extractor function and returns true if the first
|
|
391
|
+
* value is greater than the second value.
|
|
392
|
+
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
393
|
+
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
394
|
+
* of the arguments for the comparison in the function.
|
|
395
|
+
* @returns a boolean value.
|
|
396
|
+
*/
|
|
380
397
|
protected _gt(a: K, b: K): boolean;
|
|
381
398
|
}
|
|
@@ -361,6 +361,7 @@ 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._defaultOneParamCallback, true, this.root, iterationType)[0];
|
|
364
365
|
if (!this.isRealNode(this.root))
|
|
365
366
|
return undefined;
|
|
366
367
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -504,7 +505,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
504
505
|
* following values:
|
|
505
506
|
* @returns The method is returning an array of the return type of the callback function.
|
|
506
507
|
*/
|
|
507
|
-
dfs(callback = this._defaultOneParamCallback, pattern = '
|
|
508
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
508
509
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
509
510
|
}
|
|
510
511
|
/**
|
|
@@ -670,7 +671,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
670
671
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
671
672
|
*/
|
|
672
673
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
673
|
-
const sorted = this.dfs(node => node, '
|
|
674
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
674
675
|
this.clear();
|
|
675
676
|
if (sorted.length < 1)
|
|
676
677
|
return false;
|
|
@@ -798,6 +799,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
798
799
|
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
799
800
|
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
|
|
800
801
|
}
|
|
802
|
+
/**
|
|
803
|
+
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
804
|
+
* `a` is less than `b` based on the specified variant.
|
|
805
|
+
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
806
|
+
* first value to be compared in the function.
|
|
807
|
+
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
808
|
+
* of the arguments for the comparison in the `_lt` function.
|
|
809
|
+
* @returns a boolean value.
|
|
810
|
+
*/
|
|
801
811
|
_lt(a, b) {
|
|
802
812
|
const extractedA = this.extractor(a);
|
|
803
813
|
const extractedB = this.extractor(b);
|
|
@@ -806,6 +816,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
806
816
|
// return extractedA < extractedB;
|
|
807
817
|
// return a < b;
|
|
808
818
|
}
|
|
819
|
+
/**
|
|
820
|
+
* The function compares two values using a custom extractor function and returns true if the first
|
|
821
|
+
* value is greater than the second value.
|
|
822
|
+
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
823
|
+
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
824
|
+
* of the arguments for the comparison in the function.
|
|
825
|
+
* @returns a boolean value.
|
|
826
|
+
*/
|
|
809
827
|
_gt(a, b) {
|
|
810
828
|
const extractedA = this.extractor(a);
|
|
811
829
|
const extractedB = this.extractor(b);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
1
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, 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';
|
|
@@ -12,7 +12,7 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
12
12
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
13
13
|
* creating a new instance of the Red-Black Tree Node.
|
|
14
14
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
15
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
15
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
16
16
|
*/
|
|
17
17
|
constructor(key: K, value?: V, color?: RBTNColor);
|
|
18
18
|
protected _color: RBTNColor;
|
|
@@ -58,8 +58,8 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
58
58
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
59
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
60
60
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
61
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
62
|
-
* can be either "
|
|
61
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
62
|
+
* can be either "'RED'" or "'BLACK'".
|
|
63
63
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
64
64
|
* value, and color.
|
|
65
65
|
*/
|
|
@@ -140,7 +140,7 @@ export declare class RedBlackTree<K = any, V = any, NODE extends RedBlackTreeNod
|
|
|
140
140
|
* its default value is taken from the `iterationType` property of the class.
|
|
141
141
|
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
142
142
|
*/
|
|
143
|
-
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?:
|
|
143
|
+
getNode<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | undefined, callback?: C, beginRoot?: BSTNKeyOrNode<K, NODE>, iterationType?: IterationType): NODE | null | undefined;
|
|
144
144
|
/**
|
|
145
145
|
* Time Complexity: O(1)
|
|
146
146
|
* Space Complexity: O(1)
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
4
|
-
const types_1 = require("../../types");
|
|
5
4
|
const bst_1 = require("./bst");
|
|
6
5
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
7
6
|
/**
|
|
@@ -13,9 +12,9 @@ class RedBlackTreeNode extends bst_1.BSTNode {
|
|
|
13
12
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
14
13
|
* creating a new instance of the Red-Black Tree Node.
|
|
15
14
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
16
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
15
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
17
16
|
*/
|
|
18
|
-
constructor(key, value, color =
|
|
17
|
+
constructor(key, value, color = 'BLACK') {
|
|
19
18
|
super(key, value);
|
|
20
19
|
this._color = color;
|
|
21
20
|
}
|
|
@@ -75,12 +74,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
75
74
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
76
75
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
77
76
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
78
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
79
|
-
* can be either "
|
|
77
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
78
|
+
* can be either "'RED'" or "'BLACK'".
|
|
80
79
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
81
80
|
* value, and color.
|
|
82
81
|
*/
|
|
83
|
-
createNode(key, value, color =
|
|
82
|
+
createNode(key, value, color = 'BLACK') {
|
|
84
83
|
return new RedBlackTreeNode(key, value, color);
|
|
85
84
|
}
|
|
86
85
|
/**
|
|
@@ -121,11 +120,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
121
120
|
return;
|
|
122
121
|
}
|
|
123
122
|
else {
|
|
124
|
-
node = this.createNode(key, value,
|
|
123
|
+
node = this.createNode(key, value, 'RED');
|
|
125
124
|
}
|
|
126
125
|
}
|
|
127
126
|
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
128
|
-
node = this.createNode(keyOrNodeOrEntry, value,
|
|
127
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
129
128
|
}
|
|
130
129
|
else {
|
|
131
130
|
return;
|
|
@@ -193,7 +192,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
193
192
|
*/
|
|
194
193
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
195
194
|
var _a;
|
|
196
|
-
// if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
197
195
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
198
196
|
}
|
|
199
197
|
/**
|
|
@@ -236,7 +234,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
236
234
|
if (insertStatus === 'CREATED') {
|
|
237
235
|
// Ensure the root is black
|
|
238
236
|
if (this.isRealNode(this._root)) {
|
|
239
|
-
this._root.color =
|
|
237
|
+
this._root.color = 'BLACK';
|
|
240
238
|
}
|
|
241
239
|
else {
|
|
242
240
|
return false;
|
|
@@ -312,7 +310,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
312
310
|
}
|
|
313
311
|
this._size--;
|
|
314
312
|
// If the original color was black, fix the tree
|
|
315
|
-
if (originalColor ===
|
|
313
|
+
if (originalColor === 'BLACK') {
|
|
316
314
|
this._deleteFixup(replacementNode);
|
|
317
315
|
}
|
|
318
316
|
results.push({ deleted: nodeToDelete, needBalanced: undefined });
|
|
@@ -393,7 +391,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
393
391
|
}
|
|
394
392
|
node.left = this.SENTINEL;
|
|
395
393
|
node.right = this.SENTINEL;
|
|
396
|
-
node.color =
|
|
394
|
+
node.color = 'RED';
|
|
397
395
|
this._insertFixup(node);
|
|
398
396
|
return 'CREATED';
|
|
399
397
|
}
|
|
@@ -439,16 +437,16 @@ class RedBlackTree extends bst_1.BST {
|
|
|
439
437
|
_insertFixup(z) {
|
|
440
438
|
var _a, _b, _c, _d;
|
|
441
439
|
// Continue fixing the tree as long as the parent of z is red
|
|
442
|
-
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) ===
|
|
440
|
+
while (((_a = z === null || z === void 0 ? void 0 : z.parent) === null || _a === void 0 ? void 0 : _a.color) === 'RED') {
|
|
443
441
|
// Check if the parent of z is the left child of its parent
|
|
444
442
|
if (z.parent === ((_b = z.parent.parent) === null || _b === void 0 ? void 0 : _b.left)) {
|
|
445
443
|
// Case 1: The uncle (y) of z is red
|
|
446
444
|
const y = z.parent.parent.right;
|
|
447
|
-
if ((y === null || y === void 0 ? void 0 : y.color) ===
|
|
445
|
+
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
448
446
|
// Set colors to restore properties of Red-Black Tree
|
|
449
|
-
z.parent.color =
|
|
450
|
-
y.color =
|
|
451
|
-
z.parent.parent.color =
|
|
447
|
+
z.parent.color = 'BLACK';
|
|
448
|
+
y.color = 'BLACK';
|
|
449
|
+
z.parent.parent.color = 'RED';
|
|
452
450
|
// Move up the tree to continue fixing
|
|
453
451
|
z = z.parent.parent;
|
|
454
452
|
}
|
|
@@ -462,8 +460,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
462
460
|
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
463
461
|
// Adjust colors and perform a right rotation
|
|
464
462
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
465
|
-
z.parent.color =
|
|
466
|
-
z.parent.parent.color =
|
|
463
|
+
z.parent.color = 'BLACK';
|
|
464
|
+
z.parent.parent.color = 'RED';
|
|
467
465
|
this._rightRotate(z.parent.parent);
|
|
468
466
|
}
|
|
469
467
|
}
|
|
@@ -472,10 +470,10 @@ class RedBlackTree extends bst_1.BST {
|
|
|
472
470
|
// Symmetric case for the right child (left and right exchanged)
|
|
473
471
|
// Follow the same logic as above with left and right exchanged
|
|
474
472
|
const y = (_d = (_c = z === null || z === void 0 ? void 0 : z.parent) === null || _c === void 0 ? void 0 : _c.parent) === null || _d === void 0 ? void 0 : _d.left;
|
|
475
|
-
if ((y === null || y === void 0 ? void 0 : y.color) ===
|
|
476
|
-
z.parent.color =
|
|
477
|
-
y.color =
|
|
478
|
-
z.parent.parent.color =
|
|
473
|
+
if ((y === null || y === void 0 ? void 0 : y.color) === 'RED') {
|
|
474
|
+
z.parent.color = 'BLACK';
|
|
475
|
+
y.color = 'BLACK';
|
|
476
|
+
z.parent.parent.color = 'RED';
|
|
479
477
|
z = z.parent.parent;
|
|
480
478
|
}
|
|
481
479
|
else {
|
|
@@ -484,8 +482,8 @@ class RedBlackTree extends bst_1.BST {
|
|
|
484
482
|
this._rightRotate(z);
|
|
485
483
|
}
|
|
486
484
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
487
|
-
z.parent.color =
|
|
488
|
-
z.parent.parent.color =
|
|
485
|
+
z.parent.color = 'BLACK';
|
|
486
|
+
z.parent.parent.color = 'RED';
|
|
489
487
|
this._leftRotate(z.parent.parent);
|
|
490
488
|
}
|
|
491
489
|
}
|
|
@@ -493,7 +491,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
493
491
|
}
|
|
494
492
|
// Ensure that the root is black after fixing
|
|
495
493
|
if (this.isRealNode(this._root))
|
|
496
|
-
this._root.color =
|
|
494
|
+
this._root.color = 'BLACK';
|
|
497
495
|
}
|
|
498
496
|
/**
|
|
499
497
|
* Time Complexity: O(log n)
|
|
@@ -512,13 +510,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
512
510
|
_deleteFixup(node) {
|
|
513
511
|
var _a, _b, _c, _d;
|
|
514
512
|
// Early exit condition
|
|
515
|
-
if (!node || node === this.root || node.color ===
|
|
513
|
+
if (!node || node === this.root || node.color === 'BLACK') {
|
|
516
514
|
if (node) {
|
|
517
|
-
node.color =
|
|
515
|
+
node.color = 'BLACK'; // Ensure the final node is black
|
|
518
516
|
}
|
|
519
517
|
return;
|
|
520
518
|
}
|
|
521
|
-
while (node && node !== this.root && node.color ===
|
|
519
|
+
while (node && node !== this.root && node.color === 'BLACK') {
|
|
522
520
|
const parent = node.parent;
|
|
523
521
|
if (!parent) {
|
|
524
522
|
break; // Ensure the loop terminates if there's an issue with the tree structure
|
|
@@ -526,25 +524,25 @@ class RedBlackTree extends bst_1.BST {
|
|
|
526
524
|
if (node === parent.left) {
|
|
527
525
|
let sibling = parent.right;
|
|
528
526
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
529
|
-
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) ===
|
|
530
|
-
sibling.color =
|
|
531
|
-
parent.color =
|
|
527
|
+
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
528
|
+
sibling.color = 'BLACK';
|
|
529
|
+
parent.color = 'RED';
|
|
532
530
|
this._leftRotate(parent);
|
|
533
531
|
sibling = parent.right;
|
|
534
532
|
}
|
|
535
533
|
// Case 3: Sibling's left child is black
|
|
536
|
-
if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b :
|
|
534
|
+
if (((_b = (_a = sibling === null || sibling === void 0 ? void 0 : sibling.left) === null || _a === void 0 ? void 0 : _a.color) !== null && _b !== void 0 ? _b : 'BLACK') === 'BLACK') {
|
|
537
535
|
if (sibling)
|
|
538
|
-
sibling.color =
|
|
536
|
+
sibling.color = 'RED';
|
|
539
537
|
node = parent;
|
|
540
538
|
}
|
|
541
539
|
else {
|
|
542
540
|
// Case 4: Adjust colors and perform a right rotation
|
|
543
541
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.left)
|
|
544
|
-
sibling.left.color =
|
|
542
|
+
sibling.left.color = 'BLACK';
|
|
545
543
|
if (sibling)
|
|
546
544
|
sibling.color = parent.color;
|
|
547
|
-
parent.color =
|
|
545
|
+
parent.color = 'BLACK';
|
|
548
546
|
this._rightRotate(parent);
|
|
549
547
|
node = this.root;
|
|
550
548
|
}
|
|
@@ -553,28 +551,28 @@ class RedBlackTree extends bst_1.BST {
|
|
|
553
551
|
// Symmetric case for the right child (left and right exchanged)
|
|
554
552
|
let sibling = parent.left;
|
|
555
553
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
556
|
-
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) ===
|
|
557
|
-
sibling.color =
|
|
554
|
+
if ((sibling === null || sibling === void 0 ? void 0 : sibling.color) === 'RED') {
|
|
555
|
+
sibling.color = 'BLACK';
|
|
558
556
|
if (parent)
|
|
559
|
-
parent.color =
|
|
557
|
+
parent.color = 'RED';
|
|
560
558
|
this._rightRotate(parent);
|
|
561
559
|
if (parent)
|
|
562
560
|
sibling = parent.left;
|
|
563
561
|
}
|
|
564
562
|
// Case 3: Sibling's left child is black
|
|
565
|
-
if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d :
|
|
563
|
+
if (((_d = (_c = sibling === null || sibling === void 0 ? void 0 : sibling.right) === null || _c === void 0 ? void 0 : _c.color) !== null && _d !== void 0 ? _d : 'BLACK') === 'BLACK') {
|
|
566
564
|
if (sibling)
|
|
567
|
-
sibling.color =
|
|
565
|
+
sibling.color = 'RED';
|
|
568
566
|
node = parent;
|
|
569
567
|
}
|
|
570
568
|
else {
|
|
571
569
|
// Case 4: Adjust colors and perform a left rotation
|
|
572
570
|
if (sibling === null || sibling === void 0 ? void 0 : sibling.right)
|
|
573
|
-
sibling.right.color =
|
|
571
|
+
sibling.right.color = 'BLACK';
|
|
574
572
|
if (sibling)
|
|
575
573
|
sibling.color = parent.color;
|
|
576
574
|
if (parent)
|
|
577
|
-
parent.color =
|
|
575
|
+
parent.color = 'BLACK';
|
|
578
576
|
this._leftRotate(parent);
|
|
579
577
|
node = this.root;
|
|
580
578
|
}
|
|
@@ -582,7 +580,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
582
580
|
}
|
|
583
581
|
// Ensure that the final node (possibly the root) is black
|
|
584
582
|
if (node) {
|
|
585
|
-
node.color =
|
|
583
|
+
node.color = 'BLACK';
|
|
586
584
|
}
|
|
587
585
|
}
|
|
588
586
|
/**
|