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
|
@@ -11,9 +11,34 @@ exports.RedBlackTree = exports.RedBlackTreeNode = void 0;
|
|
|
11
11
|
const types_1 = require("../../types");
|
|
12
12
|
const bst_1 = require("./bst");
|
|
13
13
|
class RedBlackTreeNode extends bst_1.BSTNode {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
16
|
+
* color.
|
|
17
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
18
|
+
* Red-Black Tree.
|
|
19
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
20
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
21
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
22
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
23
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
24
|
+
*/
|
|
14
25
|
constructor(key, value, color = types_1.RBTNColor.BLACK) {
|
|
15
26
|
super(key, value);
|
|
16
|
-
this.
|
|
27
|
+
this._color = color;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The function returns the color value of a variable.
|
|
31
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
32
|
+
*/
|
|
33
|
+
get color() {
|
|
34
|
+
return this._color;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The function sets the color property to the specified value.
|
|
38
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
39
|
+
*/
|
|
40
|
+
set color(value) {
|
|
41
|
+
this._color = value;
|
|
17
42
|
}
|
|
18
43
|
}
|
|
19
44
|
exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
@@ -28,7 +53,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
28
53
|
/**
|
|
29
54
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
30
55
|
* initializes the tree with optional nodes and options.
|
|
31
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
56
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
32
57
|
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
33
58
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
34
59
|
* nodes to the
|
|
@@ -38,15 +63,30 @@ class RedBlackTree extends bst_1.BST {
|
|
|
38
63
|
*/
|
|
39
64
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
40
65
|
super([], options);
|
|
41
|
-
this.
|
|
66
|
+
this._Sentinel = new RedBlackTreeNode(NaN);
|
|
42
67
|
this._size = 0;
|
|
43
|
-
this._root = this.
|
|
68
|
+
this._root = this._Sentinel;
|
|
44
69
|
if (keysOrNodesOrEntries)
|
|
45
70
|
super.addMany(keysOrNodesOrEntries);
|
|
46
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* The function returns the value of the `_Sentinel` property.
|
|
74
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
75
|
+
*/
|
|
76
|
+
get Sentinel() {
|
|
77
|
+
return this._Sentinel;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The function returns the root node.
|
|
81
|
+
* @returns The root node of the data structure.
|
|
82
|
+
*/
|
|
47
83
|
get root() {
|
|
48
84
|
return this._root;
|
|
49
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* The function returns the size of an object.
|
|
88
|
+
* @returns The size of the object, which is a number.
|
|
89
|
+
*/
|
|
50
90
|
get size() {
|
|
51
91
|
return this._size;
|
|
52
92
|
}
|
|
@@ -76,14 +116,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
76
116
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType }, options));
|
|
77
117
|
}
|
|
78
118
|
/**
|
|
79
|
-
* The function `
|
|
80
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
119
|
+
* The function `keyValueOrEntryToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
120
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, where:
|
|
81
121
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
82
|
-
* `
|
|
122
|
+
* `keyValueOrEntryToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
83
123
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
84
|
-
* @returns a node of type
|
|
124
|
+
* @returns a node of type NODE or undefined.
|
|
85
125
|
*/
|
|
86
|
-
|
|
126
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry, value) {
|
|
87
127
|
let node;
|
|
88
128
|
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
89
129
|
return;
|
|
@@ -110,7 +150,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
110
150
|
}
|
|
111
151
|
/**
|
|
112
152
|
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
113
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
153
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
114
154
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
115
155
|
* class.
|
|
116
156
|
*/
|
|
@@ -118,18 +158,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
118
158
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
119
159
|
}
|
|
120
160
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
161
|
+
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
162
|
+
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
163
|
+
* it can either be of type `NODE` or `undefined`.
|
|
164
|
+
* @returns a boolean value.
|
|
123
165
|
*/
|
|
124
166
|
isRealNode(node) {
|
|
125
|
-
if (node === this.
|
|
167
|
+
if (node === this._Sentinel || node === undefined)
|
|
126
168
|
return false;
|
|
127
169
|
return node instanceof RedBlackTreeNode;
|
|
128
170
|
}
|
|
129
171
|
/**
|
|
130
172
|
* Time Complexity: O(log n)
|
|
131
173
|
* Space Complexity: O(1)
|
|
132
|
-
*
|
|
174
|
+
* On average (where n is the number of nodes in the tree)
|
|
133
175
|
*/
|
|
134
176
|
/**
|
|
135
177
|
* Time Complexity: O(log n)
|
|
@@ -141,17 +183,17 @@ class RedBlackTree extends bst_1.BST {
|
|
|
141
183
|
* entry.
|
|
142
184
|
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
143
185
|
* being added to the binary search tree.
|
|
144
|
-
* @returns The method `add` returns either the newly added node (`
|
|
186
|
+
* @returns The method `add` returns either the newly added node (`NODE`) or `undefined`.
|
|
145
187
|
*/
|
|
146
188
|
add(keyOrNodeOrEntry, value) {
|
|
147
|
-
const newNode = this.
|
|
189
|
+
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
148
190
|
if (newNode === undefined)
|
|
149
191
|
return false;
|
|
150
|
-
newNode.left = this.
|
|
151
|
-
newNode.right = this.
|
|
192
|
+
newNode.left = this._Sentinel;
|
|
193
|
+
newNode.right = this._Sentinel;
|
|
152
194
|
let y = undefined;
|
|
153
195
|
let x = this.root;
|
|
154
|
-
while (x !== this.
|
|
196
|
+
while (x !== this._Sentinel) {
|
|
155
197
|
y = x;
|
|
156
198
|
if (x) {
|
|
157
199
|
if (newNode.key < x.key) {
|
|
@@ -194,7 +236,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
194
236
|
/**
|
|
195
237
|
* Time Complexity: O(log n)
|
|
196
238
|
* Space Complexity: O(1)
|
|
197
|
-
* on average (where n is the number of nodes in the tree)
|
|
198
239
|
*/
|
|
199
240
|
/**
|
|
200
241
|
* Time Complexity: O(log n)
|
|
@@ -206,19 +247,19 @@ class RedBlackTree extends bst_1.BST {
|
|
|
206
247
|
* that you want to use to identify the node that you want to delete from the binary tree. It can be
|
|
207
248
|
* of any type that is returned by the callback function `C`. It can also be `null` or `undefined` if
|
|
208
249
|
* you don't want to
|
|
209
|
-
* @param {C} callback - The `callback` parameter is a function that takes a node of type `
|
|
250
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` and
|
|
210
251
|
* returns a value of type `ReturnType<C>`. It is used to determine if a node should be deleted based
|
|
211
252
|
* on its identifier. The `callback` function is optional and defaults to `this._defaultOneParam
|
|
212
|
-
* @returns an array of `BinaryTreeDeleteResult<
|
|
253
|
+
* @returns an array of `BinaryTreeDeleteResult<NODE>`.
|
|
213
254
|
*/
|
|
214
255
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
215
256
|
const ans = [];
|
|
216
257
|
if (identifier === null)
|
|
217
258
|
return ans;
|
|
218
259
|
const helper = (node) => {
|
|
219
|
-
let z = this.
|
|
260
|
+
let z = this._Sentinel;
|
|
220
261
|
let x, y;
|
|
221
|
-
while (node !== this.
|
|
262
|
+
while (node !== this._Sentinel) {
|
|
222
263
|
if (node && callback(node) === identifier) {
|
|
223
264
|
z = node;
|
|
224
265
|
}
|
|
@@ -229,17 +270,17 @@ class RedBlackTree extends bst_1.BST {
|
|
|
229
270
|
node = node === null || node === void 0 ? void 0 : node.left;
|
|
230
271
|
}
|
|
231
272
|
}
|
|
232
|
-
if (z === this.
|
|
273
|
+
if (z === this._Sentinel) {
|
|
233
274
|
this._size--;
|
|
234
275
|
return;
|
|
235
276
|
}
|
|
236
277
|
y = z;
|
|
237
278
|
let yOriginalColor = y.color;
|
|
238
|
-
if (z.left === this.
|
|
279
|
+
if (z.left === this._Sentinel) {
|
|
239
280
|
x = z.right;
|
|
240
281
|
this._rbTransplant(z, z.right);
|
|
241
282
|
}
|
|
242
|
-
else if (z.right === this.
|
|
283
|
+
else if (z.right === this._Sentinel) {
|
|
243
284
|
x = z.left;
|
|
244
285
|
this._rbTransplant(z, z.left);
|
|
245
286
|
}
|
|
@@ -285,14 +326,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
285
326
|
* node that matches the other criteria
|
|
286
327
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
287
328
|
* the binary tree. It is used to determine if a node matches the given identifier. The `callback`
|
|
288
|
-
* function should take a single parameter of type `
|
|
289
|
-
* @param {K |
|
|
329
|
+
* function should take a single parameter of type `NODE` (the type of the nodes in the binary tree) and
|
|
330
|
+
* @param {K | NODE | undefined} beginRoot - The `beginRoot` parameter is the starting point for
|
|
290
331
|
* searching for a node in a binary tree. It can be either a key value or a node object. If it is not
|
|
291
332
|
* provided, the search will start from the root of the binary tree.
|
|
292
333
|
* @param iterationType - The `iterationType` parameter is a variable that determines the type of
|
|
293
334
|
* iteration to be performed when searching for nodes in the binary tree. It is used in the
|
|
294
335
|
* `getNodes` method, which is called within the `getNode` method.
|
|
295
|
-
* @returns a value of type `
|
|
336
|
+
* @returns a value of type `NODE`, `null`, or `undefined`.
|
|
296
337
|
*/
|
|
297
338
|
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
298
339
|
var _a;
|
|
@@ -301,6 +342,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
301
342
|
beginRoot = this.ensureNode(beginRoot);
|
|
302
343
|
return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : undefined;
|
|
303
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* Time Complexity: O(1)
|
|
347
|
+
* Space Complexity: O(1)
|
|
348
|
+
*/
|
|
349
|
+
/**
|
|
350
|
+
* Time Complexity: O(1)
|
|
351
|
+
* Space Complexity: O(1)
|
|
352
|
+
*
|
|
353
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
354
|
+
*/
|
|
355
|
+
clear() {
|
|
356
|
+
this._root = this._Sentinel;
|
|
357
|
+
this._size = 0;
|
|
358
|
+
}
|
|
304
359
|
/**
|
|
305
360
|
* Time Complexity: O(log n)
|
|
306
361
|
* Space Complexity: O(1)
|
|
@@ -326,13 +381,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
326
381
|
return y;
|
|
327
382
|
}
|
|
328
383
|
/**
|
|
329
|
-
*
|
|
330
|
-
*
|
|
384
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
385
|
+
* root node.
|
|
386
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
387
|
+
* structure.
|
|
331
388
|
*/
|
|
332
|
-
clear() {
|
|
333
|
-
this._root = this.Sentinel;
|
|
334
|
-
this._size = 0;
|
|
335
|
-
}
|
|
336
389
|
_setRoot(v) {
|
|
337
390
|
if (v) {
|
|
338
391
|
v.parent = undefined;
|
|
@@ -348,13 +401,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
348
401
|
* Space Complexity: O(1)
|
|
349
402
|
*
|
|
350
403
|
* The function performs a left rotation on a binary tree node.
|
|
351
|
-
* @param {RedBlackTreeNode} x - The parameter `x` is of type `
|
|
404
|
+
* @param {RedBlackTreeNode} x - The parameter `x` is of type `NODE`, which likely represents a node in a binary tree.
|
|
352
405
|
*/
|
|
353
406
|
_leftRotate(x) {
|
|
354
407
|
if (x.right) {
|
|
355
408
|
const y = x.right;
|
|
356
409
|
x.right = y.left;
|
|
357
|
-
if (y.left !== this.
|
|
410
|
+
if (y.left !== this._Sentinel) {
|
|
358
411
|
if (y.left)
|
|
359
412
|
y.left.parent = x;
|
|
360
413
|
}
|
|
@@ -388,7 +441,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
388
441
|
if (x.left) {
|
|
389
442
|
const y = x.left;
|
|
390
443
|
x.left = y.right;
|
|
391
|
-
if (y.right !== this.
|
|
444
|
+
if (y.right !== this._Sentinel) {
|
|
392
445
|
if (y.right)
|
|
393
446
|
y.right.parent = x;
|
|
394
447
|
}
|
|
@@ -406,6 +459,63 @@ class RedBlackTree extends bst_1.BST {
|
|
|
406
459
|
x.parent = y;
|
|
407
460
|
}
|
|
408
461
|
}
|
|
462
|
+
/**
|
|
463
|
+
* Time Complexity: O(log n)
|
|
464
|
+
* Space Complexity: O(1)
|
|
465
|
+
*/
|
|
466
|
+
/**
|
|
467
|
+
* Time Complexity: O(log n)
|
|
468
|
+
* Space Complexity: O(1)
|
|
469
|
+
*
|
|
470
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
471
|
+
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
472
|
+
* red-black tree.
|
|
473
|
+
*/
|
|
474
|
+
_fixInsert(k) {
|
|
475
|
+
let u;
|
|
476
|
+
while (k.parent && k.parent.color === 1) {
|
|
477
|
+
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
478
|
+
u = k.parent.parent.left;
|
|
479
|
+
if (u && u.color === 1) {
|
|
480
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
481
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
482
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
483
|
+
k = k.parent.parent;
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
if (k === k.parent.left) {
|
|
487
|
+
k = k.parent;
|
|
488
|
+
this._rightRotate(k);
|
|
489
|
+
}
|
|
490
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
491
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
492
|
+
this._leftRotate(k.parent.parent);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else {
|
|
496
|
+
u = k.parent.parent.right;
|
|
497
|
+
if (u && u.color === 1) {
|
|
498
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
499
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
500
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
501
|
+
k = k.parent.parent;
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
if (k === k.parent.right) {
|
|
505
|
+
k = k.parent;
|
|
506
|
+
this._leftRotate(k);
|
|
507
|
+
}
|
|
508
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
509
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
510
|
+
this._rightRotate(k.parent.parent);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
if (k === this.root) {
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.root.color = types_1.RBTNColor.BLACK;
|
|
518
|
+
}
|
|
409
519
|
/**
|
|
410
520
|
* Time Complexity: O(log n)
|
|
411
521
|
* Space Complexity: O(1)
|
|
@@ -505,68 +615,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
505
615
|
}
|
|
506
616
|
v.parent = u.parent;
|
|
507
617
|
}
|
|
508
|
-
/**
|
|
509
|
-
* Time Complexity: O(log n)
|
|
510
|
-
* Space Complexity: O(1)
|
|
511
|
-
*/
|
|
512
|
-
/**
|
|
513
|
-
* Time Complexity: O(log n)
|
|
514
|
-
* Space Complexity: O(1)
|
|
515
|
-
*
|
|
516
|
-
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
517
|
-
* @param {RedBlackTreeNode} k - The parameter `k` is a RedBlackTreeNode object, which represents a node in a
|
|
518
|
-
* red-black tree.
|
|
519
|
-
*/
|
|
520
|
-
_fixInsert(k) {
|
|
521
|
-
let u;
|
|
522
|
-
while (k.parent && k.parent.color === 1) {
|
|
523
|
-
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
524
|
-
u = k.parent.parent.left;
|
|
525
|
-
if (u && u.color === 1) {
|
|
526
|
-
u.color = types_1.RBTNColor.BLACK;
|
|
527
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
528
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
529
|
-
k = k.parent.parent;
|
|
530
|
-
}
|
|
531
|
-
else {
|
|
532
|
-
if (k === k.parent.left) {
|
|
533
|
-
k = k.parent;
|
|
534
|
-
this._rightRotate(k);
|
|
535
|
-
}
|
|
536
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
537
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
538
|
-
this._leftRotate(k.parent.parent);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
else {
|
|
542
|
-
u = k.parent.parent.right;
|
|
543
|
-
if (u && u.color === 1) {
|
|
544
|
-
u.color = types_1.RBTNColor.BLACK;
|
|
545
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
546
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
547
|
-
k = k.parent.parent;
|
|
548
|
-
}
|
|
549
|
-
else {
|
|
550
|
-
if (k === k.parent.right) {
|
|
551
|
-
k = k.parent;
|
|
552
|
-
this._leftRotate(k);
|
|
553
|
-
}
|
|
554
|
-
k.parent.color = types_1.RBTNColor.BLACK;
|
|
555
|
-
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
556
|
-
this._rightRotate(k.parent.parent);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
if (k === this.root) {
|
|
560
|
-
break;
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
this.root.color = types_1.RBTNColor.BLACK;
|
|
564
|
-
}
|
|
565
618
|
/**
|
|
566
619
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
567
|
-
* @param {
|
|
568
|
-
* data structure. It is of type `
|
|
569
|
-
* @param {
|
|
620
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in a
|
|
621
|
+
* data structure. It is of type `NODE`, which is the type of the nodes in the data structure.
|
|
622
|
+
* @param {NODE} newNode - The `newNode` parameter is the node that will replace the `oldNode` in the
|
|
570
623
|
* data structure.
|
|
571
624
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
572
625
|
* superclass, passing in the `oldNode` and `newNode` as arguments.
|
|
@@ -7,13 +7,90 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
/**
|
|
11
|
+
* The constructor initializes the properties of a SegmentTreeNode object.
|
|
12
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment covered
|
|
13
|
+
* by this node in a segment tree.
|
|
14
|
+
* @param {number} end - The `end` parameter represents the end index of the segment covered by this
|
|
15
|
+
* node in a segment tree.
|
|
16
|
+
* @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
|
|
17
|
+
* the segment tree node.
|
|
18
|
+
* @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
|
|
19
|
+
* of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
|
|
20
|
+
*/
|
|
16
21
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined);
|
|
22
|
+
protected _start: number;
|
|
23
|
+
/**
|
|
24
|
+
* The function returns the value of the protected variable _start.
|
|
25
|
+
* @returns The start value, which is of type number.
|
|
26
|
+
*/
|
|
27
|
+
get start(): number;
|
|
28
|
+
/**
|
|
29
|
+
* The above function sets the value of the "start" property.
|
|
30
|
+
* @param {number} value - The value parameter is of type number.
|
|
31
|
+
*/
|
|
32
|
+
set start(value: number);
|
|
33
|
+
protected _end: number;
|
|
34
|
+
/**
|
|
35
|
+
* The function returns the value of the protected variable `_end`.
|
|
36
|
+
* @returns The value of the protected property `_end`.
|
|
37
|
+
*/
|
|
38
|
+
get end(): number;
|
|
39
|
+
/**
|
|
40
|
+
* The above function sets the value of the "end" property.
|
|
41
|
+
* @param {number} value - The value parameter is a number that represents the new value for the end
|
|
42
|
+
* property.
|
|
43
|
+
*/
|
|
44
|
+
set end(value: number);
|
|
45
|
+
protected _value: SegmentTreeNodeVal | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The function returns the value of a segment tree node.
|
|
48
|
+
* @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
|
|
49
|
+
*/
|
|
50
|
+
get value(): SegmentTreeNodeVal | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The function sets the value of a segment tree node.
|
|
53
|
+
* @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
|
|
54
|
+
* `SegmentTreeNodeVal` or `undefined`.
|
|
55
|
+
*/
|
|
56
|
+
set value(value: SegmentTreeNodeVal | undefined);
|
|
57
|
+
protected _sum: number;
|
|
58
|
+
/**
|
|
59
|
+
* The function returns the value of the sum property.
|
|
60
|
+
* @returns The method is returning the value of the variable `_sum`.
|
|
61
|
+
*/
|
|
62
|
+
get sum(): number;
|
|
63
|
+
/**
|
|
64
|
+
* The above function sets the value of the sum property.
|
|
65
|
+
* @param {number} value - The parameter "value" is of type "number".
|
|
66
|
+
*/
|
|
67
|
+
set sum(value: number);
|
|
68
|
+
protected _left: SegmentTreeNode | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* The function returns the left child of a segment tree node.
|
|
71
|
+
* @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
|
|
72
|
+
* `SegmentTreeNode` or `undefined`.
|
|
73
|
+
*/
|
|
74
|
+
get left(): SegmentTreeNode | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* The function sets the value of the left property of a SegmentTreeNode object.
|
|
77
|
+
* @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
|
|
78
|
+
* undefined.
|
|
79
|
+
*/
|
|
80
|
+
set left(value: SegmentTreeNode | undefined);
|
|
81
|
+
protected _right: SegmentTreeNode | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* The function returns the right child of a segment tree node.
|
|
84
|
+
* @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
|
|
85
|
+
*/
|
|
86
|
+
get right(): SegmentTreeNode | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The function sets the right child of a segment tree node.
|
|
89
|
+
* @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
|
|
90
|
+
* undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
|
|
91
|
+
* value.
|
|
92
|
+
*/
|
|
93
|
+
set right(value: SegmentTreeNode | undefined);
|
|
17
94
|
}
|
|
18
95
|
export declare class SegmentTree {
|
|
19
96
|
/**
|
|
@@ -27,12 +104,28 @@ export declare class SegmentTree {
|
|
|
27
104
|
*/
|
|
28
105
|
constructor(values: number[], start?: number, end?: number);
|
|
29
106
|
protected _values: number[];
|
|
107
|
+
/**
|
|
108
|
+
* The function returns an array of numbers.
|
|
109
|
+
* @returns An array of numbers is being returned.
|
|
110
|
+
*/
|
|
30
111
|
get values(): number[];
|
|
31
112
|
protected _start: number;
|
|
113
|
+
/**
|
|
114
|
+
* The function returns the value of the protected variable _start.
|
|
115
|
+
* @returns The start value, which is of type number.
|
|
116
|
+
*/
|
|
32
117
|
get start(): number;
|
|
33
118
|
protected _end: number;
|
|
119
|
+
/**
|
|
120
|
+
* The function returns the value of the protected variable `_end`.
|
|
121
|
+
* @returns The value of the protected property `_end`.
|
|
122
|
+
*/
|
|
34
123
|
get end(): number;
|
|
35
124
|
protected _root: SegmentTreeNode | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* The function returns the root node of a segment tree.
|
|
127
|
+
* @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
|
|
128
|
+
*/
|
|
36
129
|
get root(): SegmentTreeNode | undefined;
|
|
37
130
|
/**
|
|
38
131
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|