data-structure-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/CHANGELOG.md +1 -1
- package/README.md +73 -75
- package/benchmark/report.html +2 -2
- package/benchmark/report.json +15 -15
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +15 -3
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +14 -2
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +11 -11
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +19 -2
- package/dist/cjs/data-structures/binary-tree/bst.js +20 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +42 -44
- 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 +40 -22
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +43 -27
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +1 -1
- package/dist/cjs/data-structures/heap/heap.js +5 -5
- package/dist/cjs/types/common.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +15 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +14 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +11 -11
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +19 -2
- package/dist/mjs/data-structures/binary-tree/bst.js +20 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +42 -44
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +40 -22
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +43 -27
- package/dist/mjs/data-structures/heap/heap.d.ts +1 -1
- package/dist/mjs/data-structures/heap/heap.js +5 -5
- package/dist/mjs/types/common.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +135 -97
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +1 -1
- 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
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +20 -15
- package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +7 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +24 -24
- package/test/unit/data-structures/binary-tree/bst.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +25 -25
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +19 -19
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import type { BSTOptions } from "./bst";
|
|
3
|
-
export
|
|
4
|
-
RED = 1,
|
|
5
|
-
BLACK = 0
|
|
6
|
-
}
|
|
3
|
+
export type RBTNColor = 'RED' | 'BLACK';
|
|
7
4
|
export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
5
|
export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
9
6
|
export type RBTreeOptions<K> = Omit<BSTOptions<K>, 'variant'> & {};
|
|
@@ -1,9 +1,3 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RBTNColor = void 0;
|
|
4
|
-
var RBTNColor;
|
|
5
|
-
(function (RBTNColor) {
|
|
6
|
-
RBTNColor[RBTNColor["RED"] = 1] = "RED";
|
|
7
|
-
RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
|
|
8
|
-
})(RBTNColor || (exports.RBTNColor = RBTNColor = {}));
|
|
9
3
|
//# sourceMappingURL=rb-tree.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":""}
|
|
@@ -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)
|
|
@@ -50,7 +50,19 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
50
50
|
get count() {
|
|
51
51
|
return this._count;
|
|
52
52
|
}
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Time Complexity: O(n)
|
|
55
|
+
* Space Complexity: O(1)
|
|
56
|
+
*/
|
|
57
|
+
/**
|
|
58
|
+
* Time Complexity: O(n)
|
|
59
|
+
* Space Complexity: O(1)
|
|
60
|
+
*
|
|
61
|
+
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
62
|
+
* search.
|
|
63
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
64
|
+
*/
|
|
65
|
+
getComputedCount() {
|
|
54
66
|
let sum = 0;
|
|
55
67
|
this.dfs(node => (sum += node.count));
|
|
56
68
|
return sum;
|
|
@@ -269,7 +281,7 @@ export class AVLTreeMultiMap extends AVLTree {
|
|
|
269
281
|
* @returns a boolean value.
|
|
270
282
|
*/
|
|
271
283
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
272
|
-
const sorted = this.dfs(node => node, '
|
|
284
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
273
285
|
if (sorted.length < 1)
|
|
274
286
|
return false;
|
|
275
287
|
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;
|
|
@@ -1109,7 +1109,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1109
1109
|
* `false`, null or undefined
|
|
1110
1110
|
* @returns an array of values that are the return values of the callback function.
|
|
1111
1111
|
*/
|
|
1112
|
-
dfs(callback = this._defaultOneParamCallback, pattern = '
|
|
1112
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
|
|
1113
1113
|
beginRoot = this.ensureNode(beginRoot);
|
|
1114
1114
|
if (!beginRoot)
|
|
1115
1115
|
return [];
|
|
@@ -1117,7 +1117,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1117
1117
|
if (iterationType === 'RECURSIVE') {
|
|
1118
1118
|
const _traverse = (node) => {
|
|
1119
1119
|
switch (pattern) {
|
|
1120
|
-
case '
|
|
1120
|
+
case 'IN':
|
|
1121
1121
|
if (includeNull) {
|
|
1122
1122
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1123
1123
|
_traverse(node.left);
|
|
@@ -1133,7 +1133,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1133
1133
|
_traverse(node.right);
|
|
1134
1134
|
}
|
|
1135
1135
|
break;
|
|
1136
|
-
case '
|
|
1136
|
+
case 'PRE':
|
|
1137
1137
|
if (includeNull) {
|
|
1138
1138
|
this.isNodeOrNull(node) && ans.push(callback(node));
|
|
1139
1139
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
@@ -1149,7 +1149,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1149
1149
|
_traverse(node.right);
|
|
1150
1150
|
}
|
|
1151
1151
|
break;
|
|
1152
|
-
case '
|
|
1152
|
+
case 'POST':
|
|
1153
1153
|
if (includeNull) {
|
|
1154
1154
|
if (this.isRealNode(node) && this.isNodeOrNull(node.left))
|
|
1155
1155
|
_traverse(node.left);
|
|
@@ -1189,17 +1189,17 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1189
1189
|
}
|
|
1190
1190
|
else {
|
|
1191
1191
|
switch (pattern) {
|
|
1192
|
-
case '
|
|
1192
|
+
case 'IN':
|
|
1193
1193
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1194
1194
|
stack.push({ opt: 1, node: cur.node });
|
|
1195
1195
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1196
1196
|
break;
|
|
1197
|
-
case '
|
|
1197
|
+
case 'PRE':
|
|
1198
1198
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1199
1199
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
1200
1200
|
stack.push({ opt: 1, node: cur.node });
|
|
1201
1201
|
break;
|
|
1202
|
-
case '
|
|
1202
|
+
case 'POST':
|
|
1203
1203
|
stack.push({ opt: 1, node: cur.node });
|
|
1204
1204
|
cur.node && stack.push({ opt: 0, node: cur.node.right });
|
|
1205
1205
|
cur.node && stack.push({ opt: 0, node: cur.node.left });
|
|
@@ -1389,7 +1389,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1389
1389
|
* `callback` function on each node in the binary tree. The type of the array nodes is determined
|
|
1390
1390
|
* by the return type of the `callback` function.
|
|
1391
1391
|
*/
|
|
1392
|
-
morris(callback = this._defaultOneParamCallback, pattern = '
|
|
1392
|
+
morris(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root) {
|
|
1393
1393
|
beginRoot = this.ensureNode(beginRoot);
|
|
1394
1394
|
if (beginRoot === null)
|
|
1395
1395
|
return [];
|
|
@@ -1416,7 +1416,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1416
1416
|
_reverseEdge(tail);
|
|
1417
1417
|
};
|
|
1418
1418
|
switch (pattern) {
|
|
1419
|
-
case '
|
|
1419
|
+
case 'IN':
|
|
1420
1420
|
while (cur) {
|
|
1421
1421
|
if (cur.left) {
|
|
1422
1422
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1433,7 +1433,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1433
1433
|
cur = cur.right;
|
|
1434
1434
|
}
|
|
1435
1435
|
break;
|
|
1436
|
-
case '
|
|
1436
|
+
case 'PRE':
|
|
1437
1437
|
while (cur) {
|
|
1438
1438
|
if (cur.left) {
|
|
1439
1439
|
const predecessor = this.getPredecessor(cur);
|
|
@@ -1453,7 +1453,7 @@ export class BinaryTree extends IterableEntryBase {
|
|
|
1453
1453
|
cur = cur.right;
|
|
1454
1454
|
}
|
|
1455
1455
|
break;
|
|
1456
|
-
case '
|
|
1456
|
+
case 'POST':
|
|
1457
1457
|
while (cur) {
|
|
1458
1458
|
if (cur.left) {
|
|
1459
1459
|
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
|
}
|
|
@@ -365,6 +365,7 @@ 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._defaultOneParamCallback, true, this.root, iterationType)[0];
|
|
368
369
|
if (!this.isRealNode(this.root))
|
|
369
370
|
return undefined;
|
|
370
371
|
if (iterationType === 'RECURSIVE') {
|
|
@@ -508,7 +509,7 @@ export class BST extends BinaryTree {
|
|
|
508
509
|
* following values:
|
|
509
510
|
* @returns The method is returning an array of the return type of the callback function.
|
|
510
511
|
*/
|
|
511
|
-
dfs(callback = this._defaultOneParamCallback, pattern = '
|
|
512
|
+
dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE') {
|
|
512
513
|
return super.dfs(callback, pattern, beginRoot, iterationType, false);
|
|
513
514
|
}
|
|
514
515
|
/**
|
|
@@ -674,7 +675,7 @@ export class BST extends BinaryTree {
|
|
|
674
675
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
675
676
|
*/
|
|
676
677
|
perfectlyBalance(iterationType = this.iterationType) {
|
|
677
|
-
const sorted = this.dfs(node => node, '
|
|
678
|
+
const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
|
|
678
679
|
this.clear();
|
|
679
680
|
if (sorted.length < 1)
|
|
680
681
|
return false;
|
|
@@ -801,6 +802,15 @@ export class BST extends BinaryTree {
|
|
|
801
802
|
const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
|
|
802
803
|
return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
|
|
803
804
|
}
|
|
805
|
+
/**
|
|
806
|
+
* The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
|
|
807
|
+
* `a` is less than `b` based on the specified variant.
|
|
808
|
+
* @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
|
|
809
|
+
* first value to be compared in the function.
|
|
810
|
+
* @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
|
|
811
|
+
* of the arguments for the comparison in the `_lt` function.
|
|
812
|
+
* @returns a boolean value.
|
|
813
|
+
*/
|
|
804
814
|
_lt(a, b) {
|
|
805
815
|
const extractedA = this.extractor(a);
|
|
806
816
|
const extractedB = this.extractor(b);
|
|
@@ -809,6 +819,14 @@ export class BST extends BinaryTree {
|
|
|
809
819
|
// return extractedA < extractedB;
|
|
810
820
|
// return a < b;
|
|
811
821
|
}
|
|
822
|
+
/**
|
|
823
|
+
* The function compares two values using a custom extractor function and returns true if the first
|
|
824
|
+
* value is greater than the second value.
|
|
825
|
+
* @param {K} a - The parameter "a" is of type K, which means it can be any type.
|
|
826
|
+
* @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
|
|
827
|
+
* of the arguments for the comparison in the function.
|
|
828
|
+
* @returns a boolean value.
|
|
829
|
+
*/
|
|
812
830
|
_gt(a, b) {
|
|
813
831
|
const extractedA = this.extractor(a);
|
|
814
832
|
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,4 +1,3 @@
|
|
|
1
|
-
import { RBTNColor } from '../../types';
|
|
2
1
|
import { BST, BSTNode } from './bst';
|
|
3
2
|
export class RedBlackTreeNode extends BSTNode {
|
|
4
3
|
/**
|
|
@@ -10,9 +9,9 @@ export class RedBlackTreeNode extends BSTNode {
|
|
|
10
9
|
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
11
10
|
* creating a new instance of the Red-Black Tree Node.
|
|
12
11
|
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
13
|
-
* Tree Node. It is an optional parameter with a default value of `
|
|
12
|
+
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
14
13
|
*/
|
|
15
|
-
constructor(key, value, color =
|
|
14
|
+
constructor(key, value, color = 'BLACK') {
|
|
16
15
|
super(key, value);
|
|
17
16
|
this._color = color;
|
|
18
17
|
}
|
|
@@ -73,12 +72,12 @@ export class RedBlackTree extends BST {
|
|
|
73
72
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
74
73
|
* associated with the key in the node. It is not required and can be omitted if not needed.
|
|
75
74
|
* @param {RBTNColor} color - The "color" parameter is used to specify the color of the node in a
|
|
76
|
-
* Red-Black Tree. It is an optional parameter with a default value of "
|
|
77
|
-
* can be either "
|
|
75
|
+
* Red-Black Tree. It is an optional parameter with a default value of "'BLACK'". The color
|
|
76
|
+
* can be either "'RED'" or "'BLACK'".
|
|
78
77
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
79
78
|
* value, and color.
|
|
80
79
|
*/
|
|
81
|
-
createNode(key, value, color =
|
|
80
|
+
createNode(key, value, color = 'BLACK') {
|
|
82
81
|
return new RedBlackTreeNode(key, value, color);
|
|
83
82
|
}
|
|
84
83
|
/**
|
|
@@ -122,11 +121,11 @@ export class RedBlackTree extends BST {
|
|
|
122
121
|
return;
|
|
123
122
|
}
|
|
124
123
|
else {
|
|
125
|
-
node = this.createNode(key, value,
|
|
124
|
+
node = this.createNode(key, value, 'RED');
|
|
126
125
|
}
|
|
127
126
|
}
|
|
128
127
|
else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
129
|
-
node = this.createNode(keyOrNodeOrEntry, value,
|
|
128
|
+
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
130
129
|
}
|
|
131
130
|
else {
|
|
132
131
|
return;
|
|
@@ -193,7 +192,6 @@ export class RedBlackTree extends BST {
|
|
|
193
192
|
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
194
193
|
*/
|
|
195
194
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
196
|
-
// if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
197
195
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
198
196
|
}
|
|
199
197
|
/**
|
|
@@ -236,7 +234,7 @@ export class RedBlackTree extends 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 @@ export class RedBlackTree extends 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 });
|
|
@@ -392,7 +390,7 @@ export class RedBlackTree extends BST {
|
|
|
392
390
|
}
|
|
393
391
|
node.left = this.SENTINEL;
|
|
394
392
|
node.right = this.SENTINEL;
|
|
395
|
-
node.color =
|
|
393
|
+
node.color = 'RED';
|
|
396
394
|
this._insertFixup(node);
|
|
397
395
|
return 'CREATED';
|
|
398
396
|
}
|
|
@@ -437,16 +435,16 @@ export class RedBlackTree extends BST {
|
|
|
437
435
|
*/
|
|
438
436
|
_insertFixup(z) {
|
|
439
437
|
// Continue fixing the tree as long as the parent of z is red
|
|
440
|
-
while (z?.parent?.color ===
|
|
438
|
+
while (z?.parent?.color === 'RED') {
|
|
441
439
|
// Check if the parent of z is the left child of its parent
|
|
442
440
|
if (z.parent === z.parent.parent?.left) {
|
|
443
441
|
// Case 1: The uncle (y) of z is red
|
|
444
442
|
const y = z.parent.parent.right;
|
|
445
|
-
if (y?.color ===
|
|
443
|
+
if (y?.color === 'RED') {
|
|
446
444
|
// Set colors to restore properties of Red-Black Tree
|
|
447
|
-
z.parent.color =
|
|
448
|
-
y.color =
|
|
449
|
-
z.parent.parent.color =
|
|
445
|
+
z.parent.color = 'BLACK';
|
|
446
|
+
y.color = 'BLACK';
|
|
447
|
+
z.parent.parent.color = 'RED';
|
|
450
448
|
// Move up the tree to continue fixing
|
|
451
449
|
z = z.parent.parent;
|
|
452
450
|
}
|
|
@@ -460,8 +458,8 @@ export class RedBlackTree extends BST {
|
|
|
460
458
|
// Case 3: The uncle (y) of z is black, and z is a left child
|
|
461
459
|
// Adjust colors and perform a right rotation
|
|
462
460
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
463
|
-
z.parent.color =
|
|
464
|
-
z.parent.parent.color =
|
|
461
|
+
z.parent.color = 'BLACK';
|
|
462
|
+
z.parent.parent.color = 'RED';
|
|
465
463
|
this._rightRotate(z.parent.parent);
|
|
466
464
|
}
|
|
467
465
|
}
|
|
@@ -470,10 +468,10 @@ export class RedBlackTree extends BST {
|
|
|
470
468
|
// Symmetric case for the right child (left and right exchanged)
|
|
471
469
|
// Follow the same logic as above with left and right exchanged
|
|
472
470
|
const y = z?.parent?.parent?.left;
|
|
473
|
-
if (y?.color ===
|
|
474
|
-
z.parent.color =
|
|
475
|
-
y.color =
|
|
476
|
-
z.parent.parent.color =
|
|
471
|
+
if (y?.color === 'RED') {
|
|
472
|
+
z.parent.color = 'BLACK';
|
|
473
|
+
y.color = 'BLACK';
|
|
474
|
+
z.parent.parent.color = 'RED';
|
|
477
475
|
z = z.parent.parent;
|
|
478
476
|
}
|
|
479
477
|
else {
|
|
@@ -482,8 +480,8 @@ export class RedBlackTree extends BST {
|
|
|
482
480
|
this._rightRotate(z);
|
|
483
481
|
}
|
|
484
482
|
if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
|
|
485
|
-
z.parent.color =
|
|
486
|
-
z.parent.parent.color =
|
|
483
|
+
z.parent.color = 'BLACK';
|
|
484
|
+
z.parent.parent.color = 'RED';
|
|
487
485
|
this._leftRotate(z.parent.parent);
|
|
488
486
|
}
|
|
489
487
|
}
|
|
@@ -491,7 +489,7 @@ export class RedBlackTree extends BST {
|
|
|
491
489
|
}
|
|
492
490
|
// Ensure that the root is black after fixing
|
|
493
491
|
if (this.isRealNode(this._root))
|
|
494
|
-
this._root.color =
|
|
492
|
+
this._root.color = 'BLACK';
|
|
495
493
|
}
|
|
496
494
|
/**
|
|
497
495
|
* Time Complexity: O(log n)
|
|
@@ -509,13 +507,13 @@ export class RedBlackTree extends BST {
|
|
|
509
507
|
*/
|
|
510
508
|
_deleteFixup(node) {
|
|
511
509
|
// Early exit condition
|
|
512
|
-
if (!node || node === this.root || node.color ===
|
|
510
|
+
if (!node || node === this.root || node.color === 'BLACK') {
|
|
513
511
|
if (node) {
|
|
514
|
-
node.color =
|
|
512
|
+
node.color = 'BLACK'; // Ensure the final node is black
|
|
515
513
|
}
|
|
516
514
|
return;
|
|
517
515
|
}
|
|
518
|
-
while (node && node !== this.root && node.color ===
|
|
516
|
+
while (node && node !== this.root && node.color === 'BLACK') {
|
|
519
517
|
const parent = node.parent;
|
|
520
518
|
if (!parent) {
|
|
521
519
|
break; // Ensure the loop terminates if there's an issue with the tree structure
|
|
@@ -523,25 +521,25 @@ export class RedBlackTree extends BST {
|
|
|
523
521
|
if (node === parent.left) {
|
|
524
522
|
let sibling = parent.right;
|
|
525
523
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
526
|
-
if (sibling?.color ===
|
|
527
|
-
sibling.color =
|
|
528
|
-
parent.color =
|
|
524
|
+
if (sibling?.color === 'RED') {
|
|
525
|
+
sibling.color = 'BLACK';
|
|
526
|
+
parent.color = 'RED';
|
|
529
527
|
this._leftRotate(parent);
|
|
530
528
|
sibling = parent.right;
|
|
531
529
|
}
|
|
532
530
|
// Case 3: Sibling's left child is black
|
|
533
|
-
if ((sibling?.left?.color ??
|
|
531
|
+
if ((sibling?.left?.color ?? 'BLACK') === 'BLACK') {
|
|
534
532
|
if (sibling)
|
|
535
|
-
sibling.color =
|
|
533
|
+
sibling.color = 'RED';
|
|
536
534
|
node = parent;
|
|
537
535
|
}
|
|
538
536
|
else {
|
|
539
537
|
// Case 4: Adjust colors and perform a right rotation
|
|
540
538
|
if (sibling?.left)
|
|
541
|
-
sibling.left.color =
|
|
539
|
+
sibling.left.color = 'BLACK';
|
|
542
540
|
if (sibling)
|
|
543
541
|
sibling.color = parent.color;
|
|
544
|
-
parent.color =
|
|
542
|
+
parent.color = 'BLACK';
|
|
545
543
|
this._rightRotate(parent);
|
|
546
544
|
node = this.root;
|
|
547
545
|
}
|
|
@@ -550,28 +548,28 @@ export class RedBlackTree extends BST {
|
|
|
550
548
|
// Symmetric case for the right child (left and right exchanged)
|
|
551
549
|
let sibling = parent.left;
|
|
552
550
|
// Cases 1 and 2: Sibling is red or both children of sibling are black
|
|
553
|
-
if (sibling?.color ===
|
|
554
|
-
sibling.color =
|
|
551
|
+
if (sibling?.color === 'RED') {
|
|
552
|
+
sibling.color = 'BLACK';
|
|
555
553
|
if (parent)
|
|
556
|
-
parent.color =
|
|
554
|
+
parent.color = 'RED';
|
|
557
555
|
this._rightRotate(parent);
|
|
558
556
|
if (parent)
|
|
559
557
|
sibling = parent.left;
|
|
560
558
|
}
|
|
561
559
|
// Case 3: Sibling's left child is black
|
|
562
|
-
if ((sibling?.right?.color ??
|
|
560
|
+
if ((sibling?.right?.color ?? 'BLACK') === 'BLACK') {
|
|
563
561
|
if (sibling)
|
|
564
|
-
sibling.color =
|
|
562
|
+
sibling.color = 'RED';
|
|
565
563
|
node = parent;
|
|
566
564
|
}
|
|
567
565
|
else {
|
|
568
566
|
// Case 4: Adjust colors and perform a left rotation
|
|
569
567
|
if (sibling?.right)
|
|
570
|
-
sibling.right.color =
|
|
568
|
+
sibling.right.color = 'BLACK';
|
|
571
569
|
if (sibling)
|
|
572
570
|
sibling.color = parent.color;
|
|
573
571
|
if (parent)
|
|
574
|
-
parent.color =
|
|
572
|
+
parent.color = 'BLACK';
|
|
575
573
|
this._leftRotate(parent);
|
|
576
574
|
node = this.root;
|
|
577
575
|
}
|
|
@@ -579,7 +577,7 @@ export class RedBlackTree extends BST {
|
|
|
579
577
|
}
|
|
580
578
|
// Ensure that the final node (possibly the root) is black
|
|
581
579
|
if (node) {
|
|
582
|
-
node.color =
|
|
580
|
+
node.color = 'BLACK';
|
|
583
581
|
}
|
|
584
582
|
}
|
|
585
583
|
/**
|