min-heap-typed 1.50.1 → 1.50.3
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/base/iterable-base.d.ts +120 -9
- package/dist/data-structures/base/iterable-base.js +143 -7
- package/dist/data-structures/binary-tree/avl-tree.d.ts +72 -47
- package/dist/data-structures/binary-tree/avl-tree.js +101 -72
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/data-structures/binary-tree/bst.d.ts +92 -79
- package/dist/data-structures/binary-tree/bst.js +68 -76
- package/dist/data-structures/binary-tree/rb-tree.d.ts +127 -57
- package/dist/data-structures/binary-tree/rb-tree.js +152 -99
- package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +72 -58
- package/dist/data-structures/binary-tree/tree-multimap.js +102 -85
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -78
- package/dist/data-structures/graph/abstract-graph.js +3 -189
- package/dist/data-structures/graph/directed-graph.d.ts +73 -0
- package/dist/data-structures/graph/directed-graph.js +131 -0
- package/dist/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/data-structures/graph/map-graph.js +14 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +76 -7
- package/dist/data-structures/graph/undirected-graph.js +151 -18
- package/dist/data-structures/hash/hash-map.d.ts +254 -28
- package/dist/data-structures/hash/hash-map.js +347 -78
- package/dist/data-structures/heap/heap.d.ts +95 -25
- package/dist/data-structures/heap/heap.js +95 -26
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
- package/dist/data-structures/linked-list/doubly-linked-list.js +141 -77
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +154 -106
- package/dist/data-structures/linked-list/singly-linked-list.js +164 -115
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/data-structures/matrix/matrix.js +50 -11
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/data-structures/queue/deque.d.ts +139 -35
- package/dist/data-structures/queue/deque.js +200 -62
- package/dist/data-structures/queue/queue.d.ts +103 -49
- package/dist/data-structures/queue/queue.js +111 -49
- package/dist/data-structures/stack/stack.d.ts +51 -21
- package/dist/data-structures/stack/stack.js +58 -22
- package/dist/data-structures/tree/tree.d.ts +57 -3
- package/dist/data-structures/tree/tree.js +77 -11
- package/dist/data-structures/trie/trie.d.ts +135 -34
- package/dist/data-structures/trie/trie.js +153 -33
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/types/utils/utils.d.ts +1 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +184 -19
- package/src/data-structures/binary-tree/avl-tree.ts +134 -100
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +127 -136
- package/src/data-structures/binary-tree/rb-tree.ts +199 -166
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
- package/src/data-structures/graph/abstract-graph.ts +4 -211
- package/src/data-structures/graph/directed-graph.ts +152 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +171 -19
- package/src/data-structures/hash/hash-map.ts +389 -96
- package/src/data-structures/heap/heap.ts +97 -26
- package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
- package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +225 -70
- package/src/data-structures/queue/queue.ts +118 -49
- package/src/data-structures/stack/stack.ts +63 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +173 -38
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
|
@@ -10,7 +10,6 @@ import {
|
|
|
10
10
|
BinaryTreeDeleteResult,
|
|
11
11
|
BSTNKeyOrNode,
|
|
12
12
|
BTNCallback,
|
|
13
|
-
IterationType,
|
|
14
13
|
KeyOrNodeOrEntry,
|
|
15
14
|
RBTNColor,
|
|
16
15
|
RBTreeOptions,
|
|
@@ -23,13 +22,40 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
23
22
|
export class RedBlackTreeNode<
|
|
24
23
|
K = any,
|
|
25
24
|
V = any,
|
|
26
|
-
|
|
27
|
-
> extends BSTNode<K, V,
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
26
|
+
> extends BSTNode<K, V, NODE> {
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
29
|
+
* color.
|
|
30
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
31
|
+
* Red-Black Tree.
|
|
32
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
33
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
34
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
35
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
36
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
37
|
+
*/
|
|
30
38
|
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
31
39
|
super(key, value);
|
|
32
|
-
this.
|
|
40
|
+
this._color = color;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected _color: RBTNColor;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The function returns the color value of a variable.
|
|
47
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
48
|
+
*/
|
|
49
|
+
get color(): RBTNColor {
|
|
50
|
+
return this._color;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function sets the color property to the specified value.
|
|
55
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
56
|
+
*/
|
|
57
|
+
set color(value: RBTNColor) {
|
|
58
|
+
this._color = value;
|
|
33
59
|
}
|
|
34
60
|
}
|
|
35
61
|
|
|
@@ -43,17 +69,15 @@ export class RedBlackTreeNode<
|
|
|
43
69
|
export class RedBlackTree<
|
|
44
70
|
K = any,
|
|
45
71
|
V = any,
|
|
46
|
-
|
|
47
|
-
TREE extends RedBlackTree<K, V,
|
|
72
|
+
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
73
|
+
TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
|
|
48
74
|
>
|
|
49
|
-
extends BST<K, V,
|
|
50
|
-
implements IBinaryTree<K, V,
|
|
51
|
-
Sentinel: N = new RedBlackTreeNode<K, V>(NaN as K) as unknown as N;
|
|
52
|
-
|
|
75
|
+
extends BST<K, V, NODE, TREE>
|
|
76
|
+
implements IBinaryTree<K, V, NODE, TREE> {
|
|
53
77
|
/**
|
|
54
78
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
55
79
|
* initializes the tree with optional nodes and options.
|
|
56
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
80
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
57
81
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
58
82
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
59
83
|
* nodes to the
|
|
@@ -61,21 +85,39 @@ export class RedBlackTree<
|
|
|
61
85
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
62
86
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
63
87
|
*/
|
|
64
|
-
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V,
|
|
88
|
+
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
65
89
|
super([], options);
|
|
66
90
|
|
|
67
|
-
this._root = this.
|
|
91
|
+
this._root = this._Sentinel;
|
|
68
92
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
69
93
|
}
|
|
70
94
|
|
|
71
|
-
protected
|
|
95
|
+
protected _Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The function returns the value of the `_Sentinel` property.
|
|
99
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
100
|
+
*/
|
|
101
|
+
get Sentinel(): NODE {
|
|
102
|
+
return this._Sentinel;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
protected _root: NODE;
|
|
72
106
|
|
|
73
|
-
|
|
107
|
+
/**
|
|
108
|
+
* The function returns the root node.
|
|
109
|
+
* @returns The root node of the data structure.
|
|
110
|
+
*/
|
|
111
|
+
get root(): NODE {
|
|
74
112
|
return this._root;
|
|
75
113
|
}
|
|
76
114
|
|
|
77
115
|
protected _size: number = 0;
|
|
78
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The function returns the size of an object.
|
|
119
|
+
* @returns The size of the object, which is a number.
|
|
120
|
+
*/
|
|
79
121
|
get size(): number {
|
|
80
122
|
return this._size;
|
|
81
123
|
}
|
|
@@ -92,8 +134,8 @@ export class RedBlackTree<
|
|
|
92
134
|
* @returns The method is returning a new instance of a RedBlackTreeNode with the specified key,
|
|
93
135
|
* value, and color.
|
|
94
136
|
*/
|
|
95
|
-
override createNode(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK):
|
|
96
|
-
return new RedBlackTreeNode<K, V,
|
|
137
|
+
override createNode(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK): NODE {
|
|
138
|
+
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
|
|
97
139
|
}
|
|
98
140
|
|
|
99
141
|
/**
|
|
@@ -104,22 +146,22 @@ export class RedBlackTree<
|
|
|
104
146
|
* @returns a new instance of a RedBlackTree object.
|
|
105
147
|
*/
|
|
106
148
|
override createTree(options?: RBTreeOptions<K>): TREE {
|
|
107
|
-
return new RedBlackTree<K, V,
|
|
149
|
+
return new RedBlackTree<K, V, NODE, TREE>([], {
|
|
108
150
|
iterationType: this.iterationType,
|
|
109
151
|
...options
|
|
110
152
|
}) as TREE;
|
|
111
153
|
}
|
|
112
154
|
|
|
113
155
|
/**
|
|
114
|
-
* The function `
|
|
115
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
156
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
157
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
116
158
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
117
|
-
* `
|
|
159
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
118
160
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
119
|
-
* @returns a node of type
|
|
161
|
+
* @returns a node of type NODE or undefined.
|
|
120
162
|
*/
|
|
121
|
-
override
|
|
122
|
-
let node:
|
|
163
|
+
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
164
|
+
let node: NODE | undefined;
|
|
123
165
|
|
|
124
166
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
125
167
|
return;
|
|
@@ -142,28 +184,29 @@ export class RedBlackTree<
|
|
|
142
184
|
|
|
143
185
|
/**
|
|
144
186
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
145
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
187
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
146
188
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
147
189
|
* class.
|
|
148
190
|
*/
|
|
149
|
-
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
191
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
|
|
150
192
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
151
193
|
}
|
|
152
194
|
|
|
153
195
|
/**
|
|
154
|
-
*
|
|
155
|
-
*
|
|
196
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
197
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
198
|
+
* it can either be of type `NODE` or `undefined`.
|
|
199
|
+
* @returns a boolean value.
|
|
156
200
|
*/
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (node === this.Sentinel || node === undefined) return false;
|
|
201
|
+
override isRealNode(node: NODE | undefined): node is NODE {
|
|
202
|
+
if (node === this._Sentinel || node === undefined) return false;
|
|
160
203
|
return node instanceof RedBlackTreeNode;
|
|
161
204
|
}
|
|
162
205
|
|
|
163
206
|
/**
|
|
164
207
|
* Time Complexity: O(log n)
|
|
165
208
|
* Space Complexity: O(1)
|
|
166
|
-
*
|
|
209
|
+
* On average (where n is the number of nodes in the tree)
|
|
167
210
|
*/
|
|
168
211
|
|
|
169
212
|
/**
|
|
@@ -176,19 +219,19 @@ export class RedBlackTree<
|
|
|
176
219
|
* entry.
|
|
177
220
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
178
221
|
* being added to the binary search tree.
|
|
179
|
-
* @returns The method `add` returns either the newly added node (`
|
|
222
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
180
223
|
*/
|
|
181
|
-
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
182
|
-
const newNode = this.
|
|
224
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
225
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
183
226
|
if (newNode === undefined) return false;
|
|
184
227
|
|
|
185
|
-
newNode.left = this.
|
|
186
|
-
newNode.right = this.
|
|
228
|
+
newNode.left = this._Sentinel;
|
|
229
|
+
newNode.right = this._Sentinel;
|
|
187
230
|
|
|
188
|
-
let y:
|
|
189
|
-
let x:
|
|
231
|
+
let y: NODE | undefined = undefined;
|
|
232
|
+
let x: NODE | undefined = this.root;
|
|
190
233
|
|
|
191
|
-
while (x !== this.
|
|
234
|
+
while (x !== this._Sentinel) {
|
|
192
235
|
y = x;
|
|
193
236
|
if (x) {
|
|
194
237
|
if (newNode.key < x.key) {
|
|
@@ -232,7 +275,6 @@ export class RedBlackTree<
|
|
|
232
275
|
/**
|
|
233
276
|
* Time Complexity: O(log n)
|
|
234
277
|
* Space Complexity: O(1)
|
|
235
|
-
* on average (where n is the number of nodes in the tree)
|
|
236
278
|
*/
|
|
237
279
|
|
|
238
280
|
/**
|
|
@@ -245,21 +287,21 @@ export class RedBlackTree<
|
|
|
245
287
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
246
288
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
247
289
|
* you don't want to
|
|
248
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
290
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
249
291
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
250
292
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
251
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
293
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
252
294
|
*/
|
|
253
|
-
delete<C extends BTNCallback<
|
|
295
|
+
delete<C extends BTNCallback<NODE>>(
|
|
254
296
|
identifier: ReturnType<C> | null | undefined,
|
|
255
297
|
callback: C = this._defaultOneParamCallback as C
|
|
256
|
-
): BinaryTreeDeleteResult<
|
|
257
|
-
const ans: BinaryTreeDeleteResult<
|
|
298
|
+
): BinaryTreeDeleteResult<NODE>[] {
|
|
299
|
+
const ans: BinaryTreeDeleteResult<NODE>[] = [];
|
|
258
300
|
if (identifier === null) return ans;
|
|
259
|
-
const helper = (node:
|
|
260
|
-
let z:
|
|
261
|
-
let x:
|
|
262
|
-
while (node !== this.
|
|
301
|
+
const helper = (node: NODE | undefined): void => {
|
|
302
|
+
let z: NODE = this._Sentinel;
|
|
303
|
+
let x: NODE | undefined, y: NODE;
|
|
304
|
+
while (node !== this._Sentinel) {
|
|
263
305
|
if (node && callback(node) === identifier) {
|
|
264
306
|
z = node;
|
|
265
307
|
}
|
|
@@ -271,17 +313,17 @@ export class RedBlackTree<
|
|
|
271
313
|
}
|
|
272
314
|
}
|
|
273
315
|
|
|
274
|
-
if (z === this.
|
|
316
|
+
if (z === this._Sentinel) {
|
|
275
317
|
this._size--;
|
|
276
318
|
return;
|
|
277
319
|
}
|
|
278
320
|
|
|
279
321
|
y = z;
|
|
280
322
|
let yOriginalColor: number = y.color;
|
|
281
|
-
if (z.left === this.
|
|
323
|
+
if (z.left === this._Sentinel) {
|
|
282
324
|
x = z.right;
|
|
283
325
|
this._rbTransplant(z, z.right!);
|
|
284
|
-
} else if (z.right === this.
|
|
326
|
+
} else if (z.right === this._Sentinel) {
|
|
285
327
|
x = z.left;
|
|
286
328
|
this._rbTransplant(z, z.left!);
|
|
287
329
|
} else {
|
|
@@ -311,27 +353,6 @@ export class RedBlackTree<
|
|
|
311
353
|
return ans;
|
|
312
354
|
}
|
|
313
355
|
|
|
314
|
-
getNode<C extends BTNCallback<N, K>>(
|
|
315
|
-
identifier: K,
|
|
316
|
-
callback?: C,
|
|
317
|
-
beginRoot?: N | undefined,
|
|
318
|
-
iterationType?: IterationType
|
|
319
|
-
): N | undefined;
|
|
320
|
-
|
|
321
|
-
getNode<C extends BTNCallback<N, N>>(
|
|
322
|
-
identifier: N | undefined,
|
|
323
|
-
callback?: C,
|
|
324
|
-
beginRoot?: N | undefined,
|
|
325
|
-
iterationType?: IterationType
|
|
326
|
-
): N | undefined;
|
|
327
|
-
|
|
328
|
-
getNode<C extends BTNCallback<N>>(
|
|
329
|
-
identifier: ReturnType<C>,
|
|
330
|
-
callback: C,
|
|
331
|
-
beginRoot?: N | undefined,
|
|
332
|
-
iterationType?: IterationType
|
|
333
|
-
): N | undefined;
|
|
334
|
-
|
|
335
356
|
/**
|
|
336
357
|
* Time Complexity: O(log n)
|
|
337
358
|
* Space Complexity: O(1)
|
|
@@ -349,26 +370,42 @@ export class RedBlackTree<
|
|
|
349
370
|
* node that matches the other criteria
|
|
350
371
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
351
372
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
352
|
-
* function should take a single parameter of type `
|
|
353
|
-
* @param {K |
|
|
373
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
374
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
354
375
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
355
376
|
* provided, the search will start from the root of the binary tree.
|
|
356
377
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
357
378
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
358
379
|
* `getNodes` method, which is called within the `getNode` method.
|
|
359
|
-
* @returns a value of type `
|
|
380
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
360
381
|
*/
|
|
361
|
-
getNode<C extends BTNCallback<
|
|
382
|
+
getNode<C extends BTNCallback<NODE>>(
|
|
362
383
|
identifier: ReturnType<C> | undefined,
|
|
363
384
|
callback: C = this._defaultOneParamCallback as C,
|
|
364
|
-
beginRoot: BSTNKeyOrNode<K,
|
|
385
|
+
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
365
386
|
iterationType = this.iterationType
|
|
366
|
-
):
|
|
387
|
+
): NODE | null | undefined {
|
|
367
388
|
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
|
368
389
|
beginRoot = this.ensureNode(beginRoot);
|
|
369
390
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
370
391
|
}
|
|
371
392
|
|
|
393
|
+
/**
|
|
394
|
+
* Time Complexity: O(1)
|
|
395
|
+
* Space Complexity: O(1)
|
|
396
|
+
*/
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Time Complexity: O(1)
|
|
400
|
+
* Space Complexity: O(1)
|
|
401
|
+
*
|
|
402
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
403
|
+
*/
|
|
404
|
+
override clear() {
|
|
405
|
+
this._root = this._Sentinel;
|
|
406
|
+
this._size = 0;
|
|
407
|
+
}
|
|
408
|
+
|
|
372
409
|
/**
|
|
373
410
|
* Time Complexity: O(log n)
|
|
374
411
|
* Space Complexity: O(1)
|
|
@@ -383,12 +420,12 @@ export class RedBlackTree<
|
|
|
383
420
|
* Red-Black Tree.
|
|
384
421
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
385
422
|
*/
|
|
386
|
-
override getPredecessor(x:
|
|
423
|
+
override getPredecessor(x: NODE): NODE {
|
|
387
424
|
if (this.isRealNode(x.left)) {
|
|
388
425
|
return this.getRightMost(x.left)!;
|
|
389
426
|
}
|
|
390
427
|
|
|
391
|
-
let y:
|
|
428
|
+
let y: NODE | undefined = x.parent;
|
|
392
429
|
while (this.isRealNode(y) && x === y.left) {
|
|
393
430
|
x = y!;
|
|
394
431
|
y = y!.parent;
|
|
@@ -398,16 +435,12 @@ export class RedBlackTree<
|
|
|
398
435
|
}
|
|
399
436
|
|
|
400
437
|
/**
|
|
401
|
-
*
|
|
402
|
-
*
|
|
438
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
439
|
+
* root node.
|
|
440
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
441
|
+
* structure.
|
|
403
442
|
*/
|
|
404
|
-
|
|
405
|
-
override clear() {
|
|
406
|
-
this._root = this.Sentinel;
|
|
407
|
-
this._size = 0;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
protected override _setRoot(v: N) {
|
|
443
|
+
protected override _setRoot(v: NODE) {
|
|
411
444
|
if (v) {
|
|
412
445
|
v.parent = undefined;
|
|
413
446
|
}
|
|
@@ -424,13 +457,13 @@ export class RedBlackTree<
|
|
|
424
457
|
* Space Complexity: O(1)
|
|
425
458
|
*
|
|
426
459
|
* The function performs a left rotation on a binary tree node.
|
|
427
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
460
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
428
461
|
*/
|
|
429
|
-
protected _leftRotate(x:
|
|
462
|
+
protected _leftRotate(x: NODE): void {
|
|
430
463
|
if (x.right) {
|
|
431
|
-
const y:
|
|
464
|
+
const y: NODE = x.right;
|
|
432
465
|
x.right = y.left;
|
|
433
|
-
if (y.left !== this.
|
|
466
|
+
if (y.left !== this._Sentinel) {
|
|
434
467
|
if (y.left) y.left.parent = x;
|
|
435
468
|
}
|
|
436
469
|
y.parent = x.parent;
|
|
@@ -459,11 +492,11 @@ export class RedBlackTree<
|
|
|
459
492
|
* @param {RedBlackTreeNode} x - x is a RedBlackTreeNode, which represents the node that needs to be right
|
|
460
493
|
* rotated.
|
|
461
494
|
*/
|
|
462
|
-
protected _rightRotate(x:
|
|
495
|
+
protected _rightRotate(x: NODE): void {
|
|
463
496
|
if (x.left) {
|
|
464
|
-
const y:
|
|
497
|
+
const y: NODE = x.left;
|
|
465
498
|
x.left = y.right;
|
|
466
|
-
if (y.right !== this.
|
|
499
|
+
if (y.right !== this._Sentinel) {
|
|
467
500
|
if (y.right) y.right.parent = x;
|
|
468
501
|
}
|
|
469
502
|
y.parent = x.parent;
|
|
@@ -479,6 +512,65 @@ export class RedBlackTree<
|
|
|
479
512
|
}
|
|
480
513
|
}
|
|
481
514
|
|
|
515
|
+
/**
|
|
516
|
+
* Time Complexity: O(log n)
|
|
517
|
+
* Space Complexity: O(1)
|
|
518
|
+
*/
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Time Complexity: O(log n)
|
|
522
|
+
* Space Complexity: O(1)
|
|
523
|
+
*
|
|
524
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
525
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
526
|
+
* red-black tree.
|
|
527
|
+
*/
|
|
528
|
+
protected _fixInsert(k: NODE): void {
|
|
529
|
+
let u: NODE | undefined;
|
|
530
|
+
while (k.parent && k.parent.color === 1) {
|
|
531
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
532
|
+
u = k.parent.parent.left;
|
|
533
|
+
if (u && u.color === 1) {
|
|
534
|
+
u.color = RBTNColor.BLACK;
|
|
535
|
+
k.parent.color = RBTNColor.BLACK;
|
|
536
|
+
k.parent.parent.color = RBTNColor.RED;
|
|
537
|
+
k = k.parent.parent;
|
|
538
|
+
} else {
|
|
539
|
+
if (k === k.parent.left) {
|
|
540
|
+
k = k.parent;
|
|
541
|
+
this._rightRotate(k);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
545
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
546
|
+
this._leftRotate(k.parent!.parent!);
|
|
547
|
+
}
|
|
548
|
+
} else {
|
|
549
|
+
u = k.parent.parent!.right;
|
|
550
|
+
|
|
551
|
+
if (u && u.color === 1) {
|
|
552
|
+
u.color = RBTNColor.BLACK;
|
|
553
|
+
k.parent.color = RBTNColor.BLACK;
|
|
554
|
+
k.parent.parent!.color = RBTNColor.RED;
|
|
555
|
+
k = k.parent.parent!;
|
|
556
|
+
} else {
|
|
557
|
+
if (k === k.parent.right) {
|
|
558
|
+
k = k.parent;
|
|
559
|
+
this._leftRotate(k);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
k.parent!.color = RBTNColor.BLACK;
|
|
563
|
+
k.parent!.parent!.color = RBTNColor.RED;
|
|
564
|
+
this._rightRotate(k.parent!.parent!);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (k === this.root) {
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
this.root.color = RBTNColor.BLACK;
|
|
572
|
+
}
|
|
573
|
+
|
|
482
574
|
/**
|
|
483
575
|
* Time Complexity: O(log n)
|
|
484
576
|
* Space Complexity: O(1)
|
|
@@ -491,8 +583,8 @@ export class RedBlackTree<
|
|
|
491
583
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
492
584
|
* @param {RedBlackTreeNode} x - The parameter `x` represents a node in a Red-Black Tree (RBT).
|
|
493
585
|
*/
|
|
494
|
-
protected _fixDelete(x:
|
|
495
|
-
let s:
|
|
586
|
+
protected _fixDelete(x: NODE): void {
|
|
587
|
+
let s: NODE | undefined;
|
|
496
588
|
while (x !== this.root && x.color === RBTNColor.BLACK) {
|
|
497
589
|
if (x.parent && x === x.parent.left) {
|
|
498
590
|
s = x.parent.right!;
|
|
@@ -564,7 +656,7 @@ export class RedBlackTree<
|
|
|
564
656
|
* @param {RedBlackTreeNode} u - The parameter "u" represents a RedBlackTreeNode object.
|
|
565
657
|
* @param {RedBlackTreeNode} v - The parameter "v" is a RedBlackTreeNode object.
|
|
566
658
|
*/
|
|
567
|
-
protected _rbTransplant(u:
|
|
659
|
+
protected _rbTransplant(u: NODE, v: NODE): void {
|
|
568
660
|
if (u.parent === undefined) {
|
|
569
661
|
this._setRoot(v);
|
|
570
662
|
} else if (u === u.parent.left) {
|
|
@@ -575,75 +667,16 @@ export class RedBlackTree<
|
|
|
575
667
|
v.parent = u.parent;
|
|
576
668
|
}
|
|
577
669
|
|
|
578
|
-
/**
|
|
579
|
-
* Time Complexity: O(log n)
|
|
580
|
-
* Space Complexity: O(1)
|
|
581
|
-
*/
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* Time Complexity: O(log n)
|
|
585
|
-
* Space Complexity: O(1)
|
|
586
|
-
*
|
|
587
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
588
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
589
|
-
* red-black tree.
|
|
590
|
-
*/
|
|
591
|
-
protected _fixInsert(k: N): void {
|
|
592
|
-
let u: N | undefined;
|
|
593
|
-
while (k.parent && k.parent.color === 1) {
|
|
594
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
595
|
-
u = k.parent.parent.left;
|
|
596
|
-
if (u && u.color === 1) {
|
|
597
|
-
u.color = RBTNColor.BLACK;
|
|
598
|
-
k.parent.color = RBTNColor.BLACK;
|
|
599
|
-
k.parent.parent.color = RBTNColor.RED;
|
|
600
|
-
k = k.parent.parent;
|
|
601
|
-
} else {
|
|
602
|
-
if (k === k.parent.left) {
|
|
603
|
-
k = k.parent;
|
|
604
|
-
this._rightRotate(k);
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
k.parent!.color = RBTNColor.BLACK;
|
|
608
|
-
k.parent!.parent!.color = RBTNColor.RED;
|
|
609
|
-
this._leftRotate(k.parent!.parent!);
|
|
610
|
-
}
|
|
611
|
-
} else {
|
|
612
|
-
u = k.parent.parent!.right;
|
|
613
|
-
|
|
614
|
-
if (u && u.color === 1) {
|
|
615
|
-
u.color = RBTNColor.BLACK;
|
|
616
|
-
k.parent.color = RBTNColor.BLACK;
|
|
617
|
-
k.parent.parent!.color = RBTNColor.RED;
|
|
618
|
-
k = k.parent.parent!;
|
|
619
|
-
} else {
|
|
620
|
-
if (k === k.parent.right) {
|
|
621
|
-
k = k.parent;
|
|
622
|
-
this._leftRotate(k);
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
k.parent!.color = RBTNColor.BLACK;
|
|
626
|
-
k.parent!.parent!.color = RBTNColor.RED;
|
|
627
|
-
this._rightRotate(k.parent!.parent!);
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
if (k === this.root) {
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
this.root.color = RBTNColor.BLACK;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
670
|
/**
|
|
638
671
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
639
|
-
* @param {
|
|
640
|
-
* data structure. It is of type `
|
|
641
|
-
* @param {
|
|
672
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
673
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
674
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
642
675
|
* data structure.
|
|
643
676
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
644
677
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
645
678
|
*/
|
|
646
|
-
protected _replaceNode(oldNode:
|
|
679
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
647
680
|
newNode.color = oldNode.color;
|
|
648
681
|
|
|
649
682
|
return super._replaceNode(oldNode, newNode);
|