graph-typed 1.53.9 → 1.54.1
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-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +133 -357
- package/dist/data-structures/binary-tree/avl-tree.d.ts +108 -78
- package/dist/data-structures/binary-tree/avl-tree.js +126 -79
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +243 -190
- package/dist/data-structures/binary-tree/binary-tree.js +273 -229
- package/dist/data-structures/binary-tree/bst.d.ts +141 -122
- package/dist/data-structures/binary-tree/bst.js +170 -134
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +84 -80
- package/dist/data-structures/binary-tree/red-black-tree.js +101 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -186
- package/dist/data-structures/binary-tree/tree-multi-map.js +140 -388
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/interfaces/binary-tree.d.ts +7 -6
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/index.js +2 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -3
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -3
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +148 -394
- package/src/data-structures/binary-tree/avl-tree.ts +152 -112
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +446 -379
- package/src/data-structures/binary-tree/bst.ts +224 -201
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +138 -114
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +156 -428
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/interfaces/binary-tree.ts +10 -11
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -3
- package/src/types/data-structures/binary-tree/index.ts +2 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -4
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -4
|
@@ -4,50 +4,55 @@ import type {
|
|
|
4
4
|
CRUD,
|
|
5
5
|
EntryCallback,
|
|
6
6
|
OptNode,
|
|
7
|
+
OptNodeOrNull,
|
|
7
8
|
RBTNColor,
|
|
8
|
-
RedBlackTreeOptions
|
|
9
|
-
RedBlackTreeNodeNested
|
|
9
|
+
RedBlackTreeOptions
|
|
10
10
|
} from '../../types';
|
|
11
11
|
import { BST, BSTNode } from './bst';
|
|
12
12
|
import { IBinaryTree } from '../../interfaces';
|
|
13
13
|
|
|
14
|
-
export class RedBlackTreeNode<
|
|
15
|
-
K = any,
|
|
16
|
-
V = any,
|
|
17
|
-
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
18
|
-
> extends BSTNode<K, V, NODE> {
|
|
14
|
+
export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
19
15
|
/**
|
|
20
|
-
* The constructor
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* Tree Node. It is an optional parameter with a default value of `'BLACK'`.
|
|
16
|
+
* The constructor initializes a node with a key, value, and color for a Red-Black Tree.
|
|
17
|
+
* @param {K} key - The `key` parameter is a key of type `K` that is used to identify the node in a
|
|
18
|
+
* Red-Black Tree data structure.
|
|
19
|
+
* @param {V} [value] - The `value` parameter in the constructor is an optional parameter of type
|
|
20
|
+
* `V`. It represents the value associated with the key in the data structure being constructed.
|
|
21
|
+
* @param {RBTNColor} [color=BLACK] - The `color` parameter in the constructor is used to specify the
|
|
22
|
+
* color of the node in a Red-Black Tree. It has a default value of 'BLACK' if not provided
|
|
23
|
+
* explicitly.
|
|
29
24
|
*/
|
|
30
25
|
constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
|
|
31
26
|
super(key, value);
|
|
32
27
|
this._color = color;
|
|
33
28
|
}
|
|
34
29
|
|
|
35
|
-
|
|
30
|
+
override parent?: RedBlackTreeNode<K, V> = undefined;
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
get color(): RBTNColor {
|
|
42
|
-
return this._color;
|
|
32
|
+
override _left?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined;
|
|
33
|
+
|
|
34
|
+
override get left(): OptNodeOrNull<RedBlackTreeNode<K, V>> {
|
|
35
|
+
return this._left;
|
|
43
36
|
}
|
|
44
37
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
38
|
+
override set left(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) {
|
|
39
|
+
if (v) {
|
|
40
|
+
v.parent = this;
|
|
41
|
+
}
|
|
42
|
+
this._left = v;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
override _right?: OptNodeOrNull<RedBlackTreeNode<K, V>> = undefined;
|
|
46
|
+
|
|
47
|
+
override get right(): OptNodeOrNull<RedBlackTreeNode<K, V>> {
|
|
48
|
+
return this._right;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
override set right(v: OptNodeOrNull<RedBlackTreeNode<K, V>>) {
|
|
52
|
+
if (v) {
|
|
53
|
+
v.parent = this;
|
|
54
|
+
}
|
|
55
|
+
this._right = v;
|
|
51
56
|
}
|
|
52
57
|
}
|
|
53
58
|
|
|
@@ -104,26 +109,25 @@ export class RedBlackTreeNode<
|
|
|
104
109
|
* );
|
|
105
110
|
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
106
111
|
*/
|
|
107
|
-
export class RedBlackTree<
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
R = object,
|
|
111
|
-
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>
|
|
112
|
-
>
|
|
113
|
-
extends BST<K, V, R, NODE>
|
|
114
|
-
implements IBinaryTree<K, V, R, NODE>
|
|
112
|
+
export class RedBlackTree<K = any, V = any, R = object, MK = any, MV = any, MR = object>
|
|
113
|
+
extends BST<K, V, R, MK, MV, MR>
|
|
114
|
+
implements IBinaryTree<K, V, R, MK, MV, MR>
|
|
115
115
|
{
|
|
116
116
|
/**
|
|
117
|
-
* This
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* constructor
|
|
123
|
-
*
|
|
124
|
-
*
|
|
117
|
+
* This TypeScript constructor initializes a Red-Black Tree with optional keys, nodes, entries, or
|
|
118
|
+
* raw data.
|
|
119
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
120
|
+
* iterable that can contain either `BTNRep<K, V, RedBlackTreeNode<K, V>>` objects or `R` objects. It
|
|
121
|
+
* is used to initialize the Red-Black Tree with keys, nodes, entries, or
|
|
122
|
+
* @param [options] - The `options` parameter in the constructor is of type `RedBlackTreeOptions<K,
|
|
123
|
+
* V, R>`. It is an optional parameter that allows you to specify additional options for the
|
|
124
|
+
* RedBlackTree class. These options could include configuration settings, behavior customization, or
|
|
125
|
+
* any other parameters that are specific to
|
|
125
126
|
*/
|
|
126
|
-
constructor(
|
|
127
|
+
constructor(
|
|
128
|
+
keysNodesEntriesOrRaws: Iterable<BTNRep<K, V, RedBlackTreeNode<K, V>> | R> = [],
|
|
129
|
+
options?: RedBlackTreeOptions<K, V, R>
|
|
130
|
+
) {
|
|
127
131
|
super([], options);
|
|
128
132
|
|
|
129
133
|
this._root = this.NIL;
|
|
@@ -133,17 +137,16 @@ export class RedBlackTree<
|
|
|
133
137
|
}
|
|
134
138
|
}
|
|
135
139
|
|
|
136
|
-
protected override _root:
|
|
140
|
+
protected override _root: RedBlackTreeNode<K, V> | undefined;
|
|
137
141
|
|
|
138
|
-
|
|
139
|
-
* The function returns the root node of a tree or undefined if there is no root.
|
|
140
|
-
* @returns The root node of the tree structure, or undefined if there is no root node.
|
|
141
|
-
*/
|
|
142
|
-
override get root(): NODE | undefined {
|
|
142
|
+
override get root(): RedBlackTreeNode<K, V> | undefined {
|
|
143
143
|
return this._root;
|
|
144
144
|
}
|
|
145
145
|
|
|
146
146
|
/**
|
|
147
|
+
* Time Complexity: O(1)
|
|
148
|
+
* Space Complexity: O(1)
|
|
149
|
+
*
|
|
147
150
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
148
151
|
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
149
152
|
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
@@ -157,22 +160,21 @@ export class RedBlackTree<
|
|
|
157
160
|
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
158
161
|
* returned.
|
|
159
162
|
*/
|
|
160
|
-
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'):
|
|
161
|
-
return new RedBlackTreeNode<K, V
|
|
163
|
+
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): RedBlackTreeNode<K, V> {
|
|
164
|
+
return new RedBlackTreeNode<K, V>(key, this._isMapMode ? undefined : value, color);
|
|
162
165
|
}
|
|
163
166
|
|
|
164
167
|
/**
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
168
|
+
* Time Complexity: O(1)
|
|
169
|
+
* Space Complexity: O(1)
|
|
170
|
+
*
|
|
171
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
172
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
173
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
174
|
+
* @returns a new instance of a RedBlackTree object.
|
|
172
175
|
*/
|
|
173
|
-
// @ts-ignore
|
|
174
176
|
override createTree(options?: RedBlackTreeOptions<K, V, R>) {
|
|
175
|
-
return new RedBlackTree<K, V, R>([], {
|
|
177
|
+
return new RedBlackTree<K, V, R, MK, MV, MR>([], {
|
|
176
178
|
iterationType: this.iterationType,
|
|
177
179
|
isMapMode: this._isMapMode,
|
|
178
180
|
specifyComparable: this._specifyComparable,
|
|
@@ -186,13 +188,13 @@ export class RedBlackTree<
|
|
|
186
188
|
* Space Complexity: O(1)
|
|
187
189
|
*
|
|
188
190
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
189
|
-
* @param {BTNRep<K, V,
|
|
190
|
-
* `
|
|
191
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
191
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
192
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
|
|
193
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
192
194
|
* an instance of the `RedBlackTreeNode` class.
|
|
193
195
|
*/
|
|
194
|
-
override isNode(
|
|
195
|
-
return
|
|
196
|
+
override isNode(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>): keyNodeOrEntry is RedBlackTreeNode<K, V> {
|
|
197
|
+
return keyNodeOrEntry instanceof RedBlackTreeNode;
|
|
196
198
|
}
|
|
197
199
|
|
|
198
200
|
/**
|
|
@@ -209,12 +211,12 @@ export class RedBlackTree<
|
|
|
209
211
|
|
|
210
212
|
/**
|
|
211
213
|
* Time Complexity: O(log n)
|
|
212
|
-
* Space Complexity: O(
|
|
214
|
+
* Space Complexity: O(log n)
|
|
213
215
|
*
|
|
214
216
|
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
215
217
|
* added.
|
|
216
|
-
* @param {BTNRep<K, V,
|
|
217
|
-
* `
|
|
218
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
219
|
+
* `keyNodeOrEntry` can accept a value of type `R` or `BTNRep<K, V, RedBlackTreeNode<K, V>>`.
|
|
218
220
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
219
221
|
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
220
222
|
* structure.
|
|
@@ -222,8 +224,8 @@ export class RedBlackTree<
|
|
|
222
224
|
* the method returns true. If the node already exists and its value is updated, the method also
|
|
223
225
|
* returns true. If the node cannot be added or updated, the method returns false.
|
|
224
226
|
*/
|
|
225
|
-
override add(
|
|
226
|
-
const [newNode, newValue] = this.
|
|
227
|
+
override add(keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>, value?: V): boolean {
|
|
228
|
+
const [newNode, newValue] = this._keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry, value);
|
|
227
229
|
if (!this.isRealNode(newNode)) return false;
|
|
228
230
|
|
|
229
231
|
const insertStatus = this._insert(newNode);
|
|
@@ -248,36 +250,40 @@ export class RedBlackTree<
|
|
|
248
250
|
|
|
249
251
|
/**
|
|
250
252
|
* Time Complexity: O(log n)
|
|
251
|
-
* Space Complexity: O(
|
|
253
|
+
* Space Complexity: O(log n)
|
|
252
254
|
*
|
|
253
255
|
* The function overrides the delete method in a binary tree data structure to remove a node based on
|
|
254
256
|
* a given predicate and maintain the binary search tree properties.
|
|
255
|
-
* @param {BTNRep<K, V,
|
|
257
|
+
* @param {BTNRep<K, V, RedBlackTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
256
258
|
* parameter in the `override delete` method is used to specify the condition or key based on which a
|
|
257
259
|
* node should be deleted from the binary tree. It can be a key, a node, an entry, or a predicate
|
|
258
260
|
* function that determines which node(s) should be deleted.
|
|
259
|
-
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<
|
|
261
|
+
* @returns The `override delete` method is returning an array of `BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>`
|
|
260
262
|
* objects. Each object in the array contains information about the deleted node and whether
|
|
261
263
|
* balancing is needed.
|
|
262
264
|
*/
|
|
263
|
-
override delete(
|
|
264
|
-
|
|
265
|
+
override delete(
|
|
266
|
+
keyNodeOrEntry: BTNRep<K, V, RedBlackTreeNode<K, V>>
|
|
267
|
+
): BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] {
|
|
268
|
+
if (keyNodeOrEntry === null) return [];
|
|
265
269
|
|
|
266
|
-
const results: BinaryTreeDeleteResult<
|
|
267
|
-
let nodeToDelete: OptNode<
|
|
268
|
-
if (this._isPredicate(
|
|
269
|
-
else nodeToDelete = this.isRealNode(
|
|
270
|
+
const results: BinaryTreeDeleteResult<RedBlackTreeNode<K, V>>[] = [];
|
|
271
|
+
let nodeToDelete: OptNode<RedBlackTreeNode<K, V>>;
|
|
272
|
+
if (this._isPredicate(keyNodeOrEntry)) nodeToDelete = this.getNode(keyNodeOrEntry);
|
|
273
|
+
else nodeToDelete = this.isRealNode(keyNodeOrEntry) ? keyNodeOrEntry : this.getNode(keyNodeOrEntry);
|
|
270
274
|
|
|
271
275
|
if (!nodeToDelete) {
|
|
272
276
|
return results;
|
|
273
277
|
}
|
|
274
278
|
|
|
275
279
|
let originalColor = nodeToDelete.color;
|
|
276
|
-
let replacementNode:
|
|
280
|
+
let replacementNode: RedBlackTreeNode<K, V> | undefined;
|
|
277
281
|
|
|
278
282
|
if (!this.isRealNode(nodeToDelete.left)) {
|
|
279
|
-
|
|
280
|
-
|
|
283
|
+
if (nodeToDelete.right !== null) {
|
|
284
|
+
replacementNode = nodeToDelete.right;
|
|
285
|
+
this._transplant(nodeToDelete, nodeToDelete.right);
|
|
286
|
+
}
|
|
281
287
|
} else if (!this.isRealNode(nodeToDelete.right)) {
|
|
282
288
|
replacementNode = nodeToDelete.left;
|
|
283
289
|
this._transplant(nodeToDelete, nodeToDelete.left);
|
|
@@ -285,15 +291,17 @@ export class RedBlackTree<
|
|
|
285
291
|
const successor = this.getLeftMost(node => node, nodeToDelete.right);
|
|
286
292
|
if (successor) {
|
|
287
293
|
originalColor = successor.color;
|
|
288
|
-
replacementNode = successor.right;
|
|
294
|
+
if (successor.right !== null) replacementNode = successor.right;
|
|
289
295
|
|
|
290
296
|
if (successor.parent === nodeToDelete) {
|
|
291
297
|
if (this.isRealNode(replacementNode)) {
|
|
292
298
|
replacementNode.parent = successor;
|
|
293
299
|
}
|
|
294
300
|
} else {
|
|
295
|
-
|
|
296
|
-
|
|
301
|
+
if (successor.right !== null) {
|
|
302
|
+
this._transplant(successor, successor.right);
|
|
303
|
+
successor.right = nodeToDelete.right;
|
|
304
|
+
}
|
|
297
305
|
if (this.isRealNode(successor.right)) {
|
|
298
306
|
successor.right.parent = successor;
|
|
299
307
|
}
|
|
@@ -340,12 +348,11 @@ export class RedBlackTree<
|
|
|
340
348
|
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
341
349
|
* provided callback function.
|
|
342
350
|
*/
|
|
343
|
-
|
|
344
|
-
override map<MK, MV, MR>(
|
|
351
|
+
override map(
|
|
345
352
|
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
346
353
|
options?: RedBlackTreeOptions<MK, MV, MR>,
|
|
347
354
|
thisArg?: any
|
|
348
|
-
) {
|
|
355
|
+
): RedBlackTree<MK, MV, MR> {
|
|
349
356
|
const newTree = new RedBlackTree<MK, MV, MR>([], options);
|
|
350
357
|
let index = 0;
|
|
351
358
|
for (const [key, value] of this) {
|
|
@@ -354,15 +361,29 @@ export class RedBlackTree<
|
|
|
354
361
|
return newTree;
|
|
355
362
|
}
|
|
356
363
|
|
|
364
|
+
/**
|
|
365
|
+
* Time Complexity: O(n)
|
|
366
|
+
* Space Complexity: O(n)
|
|
367
|
+
*
|
|
368
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
369
|
+
* structure.
|
|
370
|
+
* @returns The `cloned` object is being returned.
|
|
371
|
+
*/
|
|
372
|
+
override clone() {
|
|
373
|
+
const cloned = this.createTree();
|
|
374
|
+
this._clone(cloned);
|
|
375
|
+
return cloned;
|
|
376
|
+
}
|
|
377
|
+
|
|
357
378
|
/**
|
|
358
379
|
* Time Complexity: O(1)
|
|
359
380
|
* Space Complexity: O(1)
|
|
360
381
|
*
|
|
361
382
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
362
383
|
* root.
|
|
363
|
-
* @param {
|
|
384
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - v is a parameter of type RedBlackTreeNode<K, V> or undefined.
|
|
364
385
|
*/
|
|
365
|
-
protected override _setRoot(v:
|
|
386
|
+
protected override _setRoot(v: RedBlackTreeNode<K, V> | undefined) {
|
|
366
387
|
if (v) {
|
|
367
388
|
v.parent = undefined;
|
|
368
389
|
}
|
|
@@ -374,14 +395,17 @@ export class RedBlackTree<
|
|
|
374
395
|
* Space Complexity: O(1)
|
|
375
396
|
*
|
|
376
397
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
377
|
-
* @param {
|
|
398
|
+
* @param {RedBlackTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
378
399
|
* the data structure.
|
|
379
|
-
* @param {
|
|
400
|
+
* @param {RedBlackTreeNode<K, V>} newNode - The `newNode` parameter is of type `RedBlackTreeNode<K, V>`, which represents a node in a
|
|
380
401
|
* data structure.
|
|
381
402
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
382
403
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
383
404
|
*/
|
|
384
|
-
protected override _replaceNode(
|
|
405
|
+
protected override _replaceNode(
|
|
406
|
+
oldNode: RedBlackTreeNode<K, V>,
|
|
407
|
+
newNode: RedBlackTreeNode<K, V>
|
|
408
|
+
): RedBlackTreeNode<K, V> {
|
|
385
409
|
newNode.color = oldNode.color;
|
|
386
410
|
|
|
387
411
|
return super._replaceNode(oldNode, newNode);
|
|
@@ -389,19 +413,19 @@ export class RedBlackTree<
|
|
|
389
413
|
|
|
390
414
|
/**
|
|
391
415
|
* Time Complexity: O(log n)
|
|
392
|
-
* Space Complexity: O(
|
|
416
|
+
* Space Complexity: O(log n)
|
|
393
417
|
*
|
|
394
418
|
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
395
419
|
* maintain the red-black tree properties.
|
|
396
|
-
* @param {
|
|
420
|
+
* @param {RedBlackTreeNode<K, V>} node - The `node` parameter represents the node that needs to be inserted into the
|
|
397
421
|
* binary search tree.
|
|
398
422
|
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
399
423
|
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
400
424
|
* was created and inserted into the tree.
|
|
401
425
|
*/
|
|
402
|
-
protected _insert(node:
|
|
426
|
+
protected _insert(node: RedBlackTreeNode<K, V>): CRUD {
|
|
403
427
|
let current = this.root;
|
|
404
|
-
let parent:
|
|
428
|
+
let parent: RedBlackTreeNode<K, V> | undefined = undefined;
|
|
405
429
|
|
|
406
430
|
while (this.isRealNode(current)) {
|
|
407
431
|
parent = current;
|
|
@@ -439,11 +463,11 @@ export class RedBlackTree<
|
|
|
439
463
|
* Space Complexity: O(1)
|
|
440
464
|
*
|
|
441
465
|
* The function `_transplant` is used to replace a node `u` with another node `v` in a binary tree.
|
|
442
|
-
* @param {
|
|
443
|
-
* @param {
|
|
444
|
-
* either be a `
|
|
466
|
+
* @param {RedBlackTreeNode<K, V>} u - The parameter "u" represents a node in a binary tree.
|
|
467
|
+
* @param {RedBlackTreeNode<K, V> | undefined} v - The parameter `v` is of type `RedBlackTreeNode<K, V> | undefined`, which means it can
|
|
468
|
+
* either be a `RedBlackTreeNode<K, V>` object or `undefined`.
|
|
445
469
|
*/
|
|
446
|
-
protected _transplant(u:
|
|
470
|
+
protected _transplant(u: RedBlackTreeNode<K, V>, v: RedBlackTreeNode<K, V> | undefined): void {
|
|
447
471
|
if (!u.parent) {
|
|
448
472
|
this._setRoot(v);
|
|
449
473
|
} else if (u === u.parent.left) {
|
|
@@ -462,10 +486,10 @@ export class RedBlackTree<
|
|
|
462
486
|
* Space Complexity: O(1)
|
|
463
487
|
*
|
|
464
488
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
465
|
-
* @param {
|
|
489
|
+
* @param {RedBlackTreeNode<K, V> | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
466
490
|
* structure. It can either be a valid node or `undefined`.
|
|
467
491
|
*/
|
|
468
|
-
protected _insertFixup(z:
|
|
492
|
+
protected _insertFixup(z: RedBlackTreeNode<K, V> | undefined): void {
|
|
469
493
|
// Continue fixing the tree as long as the parent of z is red
|
|
470
494
|
while (z?.parent?.color === 'RED') {
|
|
471
495
|
// Check if the parent of z is the left child of its parent
|
|
@@ -498,7 +522,7 @@ export class RedBlackTree<
|
|
|
498
522
|
} else {
|
|
499
523
|
// Symmetric case for the right child (left and right exchanged)
|
|
500
524
|
// Follow the same logic as above with left and right exchanged
|
|
501
|
-
const y:
|
|
525
|
+
const y: RedBlackTreeNode<K, V> | undefined = z?.parent?.parent?.left ?? undefined;
|
|
502
526
|
if (y?.color === 'RED') {
|
|
503
527
|
z.parent.color = 'BLACK';
|
|
504
528
|
y.color = 'BLACK';
|
|
@@ -529,12 +553,12 @@ export class RedBlackTree<
|
|
|
529
553
|
*
|
|
530
554
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
531
555
|
* the colors and performing rotations.
|
|
532
|
-
* @param {
|
|
556
|
+
* @param {RedBlackTreeNode<K, V> | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
533
557
|
* be either a valid node object or `undefined`.
|
|
534
558
|
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
535
559
|
* does not return anything.
|
|
536
560
|
*/
|
|
537
|
-
protected _deleteFixup(node:
|
|
561
|
+
protected _deleteFixup(node: RedBlackTreeNode<K, V> | undefined): void {
|
|
538
562
|
// Early exit condition
|
|
539
563
|
if (!node || node === this.root || node.color === 'BLACK') {
|
|
540
564
|
if (node) {
|
|
@@ -544,7 +568,7 @@ export class RedBlackTree<
|
|
|
544
568
|
}
|
|
545
569
|
|
|
546
570
|
while (node && node !== this.root && node.color === 'BLACK') {
|
|
547
|
-
const parent:
|
|
571
|
+
const parent: RedBlackTreeNode<K, V> | undefined = node.parent;
|
|
548
572
|
|
|
549
573
|
if (!parent) {
|
|
550
574
|
break; // Ensure the loop terminates if there's an issue with the tree structure
|
|
@@ -611,11 +635,11 @@ export class RedBlackTree<
|
|
|
611
635
|
* Space Complexity: O(1)
|
|
612
636
|
*
|
|
613
637
|
* The `_leftRotate` function performs a left rotation on a given node in a binary tree.
|
|
614
|
-
* @param {
|
|
638
|
+
* @param {RedBlackTreeNode<K, V> | undefined} x - The parameter `x` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
615
639
|
* node in a binary tree or `undefined` if there is no node.
|
|
616
640
|
* @returns void, which means it does not return any value.
|
|
617
641
|
*/
|
|
618
|
-
protected _leftRotate(x:
|
|
642
|
+
protected _leftRotate(x: RedBlackTreeNode<K, V> | undefined): void {
|
|
619
643
|
if (!x || !x.right) {
|
|
620
644
|
return;
|
|
621
645
|
}
|
|
@@ -646,11 +670,11 @@ export class RedBlackTree<
|
|
|
646
670
|
* Space Complexity: O(1)
|
|
647
671
|
*
|
|
648
672
|
* The `_rightRotate` function performs a right rotation on a given node in a binary tree.
|
|
649
|
-
* @param {
|
|
673
|
+
* @param {RedBlackTreeNode<K, V> | undefined} y - The parameter `y` is of type `RedBlackTreeNode<K, V> | undefined`. It represents a
|
|
650
674
|
* node in a binary tree or `undefined` if there is no node.
|
|
651
675
|
* @returns void, which means it does not return any value.
|
|
652
676
|
*/
|
|
653
|
-
protected _rightRotate(y:
|
|
677
|
+
protected _rightRotate(y: RedBlackTreeNode<K, V> | undefined): void {
|
|
654
678
|
if (!y || !y.left) {
|
|
655
679
|
return;
|
|
656
680
|
}
|