directed-graph-typed 1.39.6 → 1.41.0
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.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +13 -20
- package/dist/data-structures/binary-tree/binary-tree.js +30 -31
- package/dist/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/data-structures/binary-tree/bst.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.d.ts +95 -11
- package/dist/data-structures/binary-tree/rb-tree.js +379 -18
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +40 -43
- package/src/data-structures/binary-tree/bst.ts +9 -10
- package/src/data-structures/binary-tree/rb-tree.ts +415 -355
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -1,27 +1,388 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RedBlackTree = exports.RBTreeNode = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return this._color;
|
|
13
|
-
}
|
|
14
|
-
set color(value) {
|
|
15
|
-
this._color = value;
|
|
5
|
+
class RBTreeNode {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.key = 0;
|
|
8
|
+
this.color = types_1.RBTNColor.BLACK;
|
|
9
|
+
this.parent = null;
|
|
10
|
+
this.left = null;
|
|
11
|
+
this.right = null;
|
|
16
12
|
}
|
|
17
13
|
}
|
|
18
14
|
exports.RBTreeNode = RBTreeNode;
|
|
19
|
-
class
|
|
20
|
-
constructor(
|
|
21
|
-
|
|
15
|
+
class RedBlackTree {
|
|
16
|
+
constructor() {
|
|
17
|
+
this._NIL = new RBTreeNode();
|
|
18
|
+
this.NIL.color = types_1.RBTNColor.BLACK;
|
|
19
|
+
this.NIL.left = null;
|
|
20
|
+
this.NIL.right = null;
|
|
21
|
+
this._root = this.NIL;
|
|
22
|
+
}
|
|
23
|
+
get root() {
|
|
24
|
+
return this._root;
|
|
25
|
+
}
|
|
26
|
+
get NIL() {
|
|
27
|
+
return this._NIL;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The `insert` function inserts a new node with a given key into a red-black tree and fixes any
|
|
31
|
+
* violations of the red-black tree properties.
|
|
32
|
+
* @param {number} key - The key parameter is a number that represents the value to be inserted into
|
|
33
|
+
* the RBTree.
|
|
34
|
+
* @returns The function does not explicitly return anything.
|
|
35
|
+
*/
|
|
36
|
+
insert(key) {
|
|
37
|
+
const node = new RBTreeNode();
|
|
38
|
+
node.parent = null;
|
|
39
|
+
node.key = key;
|
|
40
|
+
node.left = this.NIL;
|
|
41
|
+
node.right = this.NIL;
|
|
42
|
+
node.color = types_1.RBTNColor.RED;
|
|
43
|
+
let y = null;
|
|
44
|
+
let x = this.root;
|
|
45
|
+
while (x !== this.NIL) {
|
|
46
|
+
y = x;
|
|
47
|
+
if (node.key < x.key) {
|
|
48
|
+
x = x.left;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
x = x.right;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
node.parent = y;
|
|
55
|
+
if (y === null) {
|
|
56
|
+
this._root = node;
|
|
57
|
+
}
|
|
58
|
+
else if (node.key < y.key) {
|
|
59
|
+
y.left = node;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
y.right = node;
|
|
63
|
+
}
|
|
64
|
+
if (node.parent === null) {
|
|
65
|
+
node.color = types_1.RBTNColor.BLACK;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (node.parent.parent === null) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
this._fixInsert(node);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
|
|
75
|
+
* tree.
|
|
76
|
+
* @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
|
|
77
|
+
* node being processed in the delete operation.
|
|
78
|
+
* @returns The `delete` function does not return anything. It has a return type of `void`.
|
|
79
|
+
*/
|
|
80
|
+
delete(key) {
|
|
81
|
+
const helper = (node) => {
|
|
82
|
+
let z = this.NIL;
|
|
83
|
+
let x, y;
|
|
84
|
+
while (node !== this.NIL) {
|
|
85
|
+
if (node.key === key) {
|
|
86
|
+
z = node;
|
|
87
|
+
}
|
|
88
|
+
if (node.key <= key) {
|
|
89
|
+
node = node.right;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
node = node.left;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (z === this.NIL) {
|
|
96
|
+
console.log("Couldn't find key in the tree");
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
y = z;
|
|
100
|
+
let yOriginalColor = y.color;
|
|
101
|
+
if (z.left === this.NIL) {
|
|
102
|
+
x = z.right;
|
|
103
|
+
this._rbTransplant(z, z.right);
|
|
104
|
+
}
|
|
105
|
+
else if (z.right === this.NIL) {
|
|
106
|
+
x = z.left;
|
|
107
|
+
this._rbTransplant(z, z.left);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
y = this.getLeftMost(z.right);
|
|
111
|
+
yOriginalColor = y.color;
|
|
112
|
+
x = y.right;
|
|
113
|
+
if (y.parent === z) {
|
|
114
|
+
x.parent = y;
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this._rbTransplant(y, y.right);
|
|
118
|
+
y.right = z.right;
|
|
119
|
+
y.right.parent = y;
|
|
120
|
+
}
|
|
121
|
+
this._rbTransplant(z, y);
|
|
122
|
+
y.left = z.left;
|
|
123
|
+
y.left.parent = y;
|
|
124
|
+
y.color = z.color;
|
|
125
|
+
}
|
|
126
|
+
if (yOriginalColor === 0) {
|
|
127
|
+
this._fixDelete(x);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
helper(this.root);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
|
|
134
|
+
* given key in a red-black tree.
|
|
135
|
+
* @param {number} key - The key parameter is a number that represents the value we are searching for
|
|
136
|
+
* in the RBTree.
|
|
137
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
|
|
138
|
+
* point for the search in the binary search tree. If no value is provided for `beginRoot`, it
|
|
139
|
+
* defaults to the root of the binary search tree (`this.root`).
|
|
140
|
+
* @returns a RBTreeNode.
|
|
141
|
+
*/
|
|
142
|
+
getNode(key, beginRoot = this.root) {
|
|
143
|
+
const dfs = (node) => {
|
|
144
|
+
if (node === this.NIL || key === node.key) {
|
|
145
|
+
return node;
|
|
146
|
+
}
|
|
147
|
+
if (key < node.key) {
|
|
148
|
+
return dfs(node.left);
|
|
149
|
+
}
|
|
150
|
+
return dfs(node.right);
|
|
151
|
+
};
|
|
152
|
+
return dfs(beginRoot);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The function returns the leftmost node in a red-black tree.
|
|
156
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
|
|
157
|
+
* a Red-Black Tree.
|
|
158
|
+
* @returns The leftmost node in the given RBTreeNode.
|
|
159
|
+
*/
|
|
160
|
+
getLeftMost(node) {
|
|
161
|
+
while (node.left !== null && node.left !== this.NIL) {
|
|
162
|
+
node = node.left;
|
|
163
|
+
}
|
|
164
|
+
return node;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The function returns the rightmost node in a red-black tree.
|
|
168
|
+
* @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
|
|
169
|
+
* @returns the rightmost node in a red-black tree.
|
|
170
|
+
*/
|
|
171
|
+
getRightMost(node) {
|
|
172
|
+
while (node.right !== null && node.right !== this.NIL) {
|
|
173
|
+
node = node.right;
|
|
174
|
+
}
|
|
175
|
+
return node;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The function returns the successor of a given node in a red-black tree.
|
|
179
|
+
* @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
|
|
180
|
+
* @returns the successor of the given RBTreeNode.
|
|
181
|
+
*/
|
|
182
|
+
getSuccessor(x) {
|
|
183
|
+
if (x.right !== this.NIL) {
|
|
184
|
+
return this.getLeftMost(x.right);
|
|
185
|
+
}
|
|
186
|
+
let y = x.parent;
|
|
187
|
+
while (y !== this.NIL && y !== null && x === y.right) {
|
|
188
|
+
x = y;
|
|
189
|
+
y = y.parent;
|
|
190
|
+
}
|
|
191
|
+
return y;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* The function returns the predecessor of a given node in a red-black tree.
|
|
195
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
196
|
+
* Red-Black Tree.
|
|
197
|
+
* @returns the predecessor of the given RBTreeNode 'x'.
|
|
198
|
+
*/
|
|
199
|
+
getPredecessor(x) {
|
|
200
|
+
if (x.left !== this.NIL) {
|
|
201
|
+
return this.getRightMost(x.left);
|
|
202
|
+
}
|
|
203
|
+
let y = x.parent;
|
|
204
|
+
while (y !== this.NIL && x === y.left) {
|
|
205
|
+
x = y;
|
|
206
|
+
y = y.parent;
|
|
207
|
+
}
|
|
208
|
+
return y;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The function performs a left rotation on a red-black tree node.
|
|
212
|
+
* @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
|
|
213
|
+
*/
|
|
214
|
+
_leftRotate(x) {
|
|
215
|
+
const y = x.right;
|
|
216
|
+
x.right = y.left;
|
|
217
|
+
if (y.left !== this.NIL) {
|
|
218
|
+
y.left.parent = x;
|
|
219
|
+
}
|
|
220
|
+
y.parent = x.parent;
|
|
221
|
+
if (x.parent === null) {
|
|
222
|
+
this._root = y;
|
|
223
|
+
}
|
|
224
|
+
else if (x === x.parent.left) {
|
|
225
|
+
x.parent.left = y;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
x.parent.right = y;
|
|
229
|
+
}
|
|
230
|
+
y.left = x;
|
|
231
|
+
x.parent = y;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The function performs a right rotation on a red-black tree node.
|
|
235
|
+
* @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
|
|
236
|
+
* rotated.
|
|
237
|
+
*/
|
|
238
|
+
_rightRotate(x) {
|
|
239
|
+
const y = x.left;
|
|
240
|
+
x.left = y.right;
|
|
241
|
+
if (y.right !== this.NIL) {
|
|
242
|
+
y.right.parent = x;
|
|
243
|
+
}
|
|
244
|
+
y.parent = x.parent;
|
|
245
|
+
if (x.parent === null) {
|
|
246
|
+
this._root = y;
|
|
247
|
+
}
|
|
248
|
+
else if (x === x.parent.right) {
|
|
249
|
+
x.parent.right = y;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
x.parent.left = y;
|
|
253
|
+
}
|
|
254
|
+
y.right = x;
|
|
255
|
+
x.parent = y;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
|
|
259
|
+
* @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
|
|
260
|
+
* red-black tree.
|
|
261
|
+
*/
|
|
262
|
+
_fixDelete(x) {
|
|
263
|
+
let s;
|
|
264
|
+
while (x !== this.root && x.color === 0) {
|
|
265
|
+
if (x === x.parent.left) {
|
|
266
|
+
s = x.parent.right;
|
|
267
|
+
if (s.color === 1) {
|
|
268
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
269
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
270
|
+
this._leftRotate(x.parent);
|
|
271
|
+
s = x.parent.right;
|
|
272
|
+
}
|
|
273
|
+
if (s.left.color === 0 && s.right.color === 0) {
|
|
274
|
+
s.color = types_1.RBTNColor.RED;
|
|
275
|
+
x = x.parent;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
if (s.right.color === 0) {
|
|
279
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
280
|
+
s.color = types_1.RBTNColor.RED;
|
|
281
|
+
this._rightRotate(s);
|
|
282
|
+
s = x.parent.right;
|
|
283
|
+
}
|
|
284
|
+
s.color = x.parent.color;
|
|
285
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
286
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
287
|
+
this._leftRotate(x.parent);
|
|
288
|
+
x = this.root;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
s = x.parent.left;
|
|
293
|
+
if (s.color === 1) {
|
|
294
|
+
s.color = types_1.RBTNColor.BLACK;
|
|
295
|
+
x.parent.color = types_1.RBTNColor.RED;
|
|
296
|
+
this._rightRotate(x.parent);
|
|
297
|
+
s = x.parent.left;
|
|
298
|
+
}
|
|
299
|
+
if (s.right.color === 0 && s.right.color === 0) {
|
|
300
|
+
s.color = types_1.RBTNColor.RED;
|
|
301
|
+
x = x.parent;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
if (s.left.color === 0) {
|
|
305
|
+
s.right.color = types_1.RBTNColor.BLACK;
|
|
306
|
+
s.color = types_1.RBTNColor.RED;
|
|
307
|
+
this._leftRotate(s);
|
|
308
|
+
s = x.parent.left;
|
|
309
|
+
}
|
|
310
|
+
s.color = x.parent.color;
|
|
311
|
+
x.parent.color = types_1.RBTNColor.BLACK;
|
|
312
|
+
s.left.color = types_1.RBTNColor.BLACK;
|
|
313
|
+
this._rightRotate(x.parent);
|
|
314
|
+
x = this.root;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
x.color = types_1.RBTNColor.BLACK;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* The function `_rbTransplant` replaces one node in a red-black tree with another node.
|
|
322
|
+
* @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
|
|
323
|
+
* @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
|
|
324
|
+
*/
|
|
325
|
+
_rbTransplant(u, v) {
|
|
326
|
+
if (u.parent === null) {
|
|
327
|
+
this._root = v;
|
|
328
|
+
}
|
|
329
|
+
else if (u === u.parent.left) {
|
|
330
|
+
u.parent.left = v;
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
u.parent.right = v;
|
|
334
|
+
}
|
|
335
|
+
v.parent = u.parent;
|
|
22
336
|
}
|
|
23
|
-
|
|
24
|
-
|
|
337
|
+
/**
|
|
338
|
+
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
339
|
+
* @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
|
|
340
|
+
* red-black tree.
|
|
341
|
+
*/
|
|
342
|
+
_fixInsert(k) {
|
|
343
|
+
let u;
|
|
344
|
+
while (k.parent.color === 1) {
|
|
345
|
+
if (k.parent === k.parent.parent.right) {
|
|
346
|
+
u = k.parent.parent.left;
|
|
347
|
+
if (u.color === 1) {
|
|
348
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
349
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
350
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
351
|
+
k = k.parent.parent;
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
if (k === k.parent.left) {
|
|
355
|
+
k = k.parent;
|
|
356
|
+
this._rightRotate(k);
|
|
357
|
+
}
|
|
358
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
359
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
360
|
+
this._leftRotate(k.parent.parent);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
u = k.parent.parent.right;
|
|
365
|
+
if (u.color === 1) {
|
|
366
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
367
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
368
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
369
|
+
k = k.parent.parent;
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
if (k === k.parent.right) {
|
|
373
|
+
k = k.parent;
|
|
374
|
+
this._leftRotate(k);
|
|
375
|
+
}
|
|
376
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
377
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
378
|
+
this._rightRotate(k.parent.parent);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (k === this.root) {
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
this.root.color = types_1.RBTNColor.BLACK;
|
|
25
386
|
}
|
|
26
387
|
}
|
|
27
|
-
exports.
|
|
388
|
+
exports.RedBlackTree = RedBlackTree;
|
|
@@ -7,25 +7,13 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import type { SegmentTreeNodeVal } from '../../types';
|
|
9
9
|
export declare class SegmentTreeNode {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
value: SegmentTreeNodeVal | null;
|
|
13
|
+
sum: number;
|
|
14
|
+
left: SegmentTreeNode | null;
|
|
15
|
+
right: SegmentTreeNode | null;
|
|
10
16
|
constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | null);
|
|
11
|
-
private _start;
|
|
12
|
-
get start(): number;
|
|
13
|
-
set start(v: number);
|
|
14
|
-
private _end;
|
|
15
|
-
get end(): number;
|
|
16
|
-
set end(v: number);
|
|
17
|
-
private _value;
|
|
18
|
-
get value(): SegmentTreeNodeVal | null;
|
|
19
|
-
set value(v: SegmentTreeNodeVal | null);
|
|
20
|
-
private _sum;
|
|
21
|
-
get sum(): number;
|
|
22
|
-
set sum(v: number);
|
|
23
|
-
private _left;
|
|
24
|
-
get left(): SegmentTreeNode | null;
|
|
25
|
-
set left(v: SegmentTreeNode | null);
|
|
26
|
-
private _right;
|
|
27
|
-
get right(): SegmentTreeNode | null;
|
|
28
|
-
set right(v: SegmentTreeNode | null);
|
|
29
17
|
}
|
|
30
18
|
export declare class SegmentTree {
|
|
31
19
|
/**
|
|
@@ -38,13 +26,13 @@ export declare class SegmentTree {
|
|
|
38
26
|
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
39
27
|
*/
|
|
40
28
|
constructor(values: number[], start?: number, end?: number);
|
|
41
|
-
|
|
29
|
+
protected _values: number[];
|
|
42
30
|
get values(): number[];
|
|
43
|
-
|
|
31
|
+
protected _start: number;
|
|
44
32
|
get start(): number;
|
|
45
|
-
|
|
33
|
+
protected _end: number;
|
|
46
34
|
get end(): number;
|
|
47
|
-
|
|
35
|
+
protected _root: SegmentTreeNode | null;
|
|
48
36
|
get root(): SegmentTreeNode | null;
|
|
49
37
|
/**
|
|
50
38
|
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
@@ -76,8 +64,4 @@ export declare class SegmentTree {
|
|
|
76
64
|
* @returns The function `querySumByRange` returns a number.
|
|
77
65
|
*/
|
|
78
66
|
querySumByRange(indexA: number, indexB: number): number;
|
|
79
|
-
protected _setValues(value: number[]): void;
|
|
80
|
-
protected _setStart(value: number): void;
|
|
81
|
-
protected _setEnd(value: number): void;
|
|
82
|
-
protected _setRoot(v: SegmentTreeNode | null): void;
|
|
83
67
|
}
|
|
@@ -10,52 +10,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
10
10
|
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
11
11
|
class SegmentTreeNode {
|
|
12
12
|
constructor(start, end, sum, value) {
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
this.
|
|
21
|
-
this.
|
|
22
|
-
this.
|
|
23
|
-
}
|
|
24
|
-
get start() {
|
|
25
|
-
return this._start;
|
|
26
|
-
}
|
|
27
|
-
set start(v) {
|
|
28
|
-
this._start = v;
|
|
29
|
-
}
|
|
30
|
-
get end() {
|
|
31
|
-
return this._end;
|
|
32
|
-
}
|
|
33
|
-
set end(v) {
|
|
34
|
-
this._end = v;
|
|
35
|
-
}
|
|
36
|
-
get value() {
|
|
37
|
-
return this._value;
|
|
38
|
-
}
|
|
39
|
-
set value(v) {
|
|
40
|
-
this._value = v;
|
|
41
|
-
}
|
|
42
|
-
get sum() {
|
|
43
|
-
return this._sum;
|
|
44
|
-
}
|
|
45
|
-
set sum(v) {
|
|
46
|
-
this._sum = v;
|
|
47
|
-
}
|
|
48
|
-
get left() {
|
|
49
|
-
return this._left;
|
|
50
|
-
}
|
|
51
|
-
set left(v) {
|
|
52
|
-
this._left = v;
|
|
53
|
-
}
|
|
54
|
-
get right() {
|
|
55
|
-
return this._right;
|
|
56
|
-
}
|
|
57
|
-
set right(v) {
|
|
58
|
-
this._right = v;
|
|
13
|
+
this.start = 0;
|
|
14
|
+
this.end = 0;
|
|
15
|
+
this.value = null;
|
|
16
|
+
this.sum = 0;
|
|
17
|
+
this.left = null;
|
|
18
|
+
this.right = null;
|
|
19
|
+
this.start = start;
|
|
20
|
+
this.end = end;
|
|
21
|
+
this.sum = sum;
|
|
22
|
+
this.value = value || null;
|
|
59
23
|
}
|
|
60
24
|
}
|
|
61
25
|
exports.SegmentTreeNode = SegmentTreeNode;
|
|
@@ -212,17 +176,5 @@ class SegmentTree {
|
|
|
212
176
|
};
|
|
213
177
|
return dfs(root, indexA, indexB);
|
|
214
178
|
}
|
|
215
|
-
_setValues(value) {
|
|
216
|
-
this._values = value;
|
|
217
|
-
}
|
|
218
|
-
_setStart(value) {
|
|
219
|
-
this._start = value;
|
|
220
|
-
}
|
|
221
|
-
_setEnd(value) {
|
|
222
|
-
this._end = value;
|
|
223
|
-
}
|
|
224
|
-
_setRoot(v) {
|
|
225
|
-
this._root = v;
|
|
226
|
-
}
|
|
227
179
|
}
|
|
228
180
|
exports.SegmentTree = SegmentTree;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BTNKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult,
|
|
9
|
+
import { BinaryTreeDeletedResult, BTNCallback, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -75,7 +75,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
75
75
|
}
|
|
76
76
|
if (!this.root) {
|
|
77
77
|
this._setRoot(newNode);
|
|
78
|
-
this.
|
|
78
|
+
this._size = this.size + 1;
|
|
79
79
|
newNode && this._setCount(this.count + newNode.count);
|
|
80
80
|
inserted = this.root;
|
|
81
81
|
}
|
|
@@ -97,7 +97,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
97
97
|
if (cur.left === undefined) {
|
|
98
98
|
//Add to the left of the current node
|
|
99
99
|
cur.left = newNode;
|
|
100
|
-
this.
|
|
100
|
+
this._size = this.size + 1;
|
|
101
101
|
this._setCount(this.count + newNode.count);
|
|
102
102
|
traversing = false;
|
|
103
103
|
inserted = cur.left;
|
|
@@ -113,7 +113,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
113
113
|
if (cur.right === undefined) {
|
|
114
114
|
//Add to the right of the current node
|
|
115
115
|
cur.right = newNode;
|
|
116
|
-
this.
|
|
116
|
+
this._size = this.size + 1;
|
|
117
117
|
this._setCount(this.count + newNode.count);
|
|
118
118
|
traversing = false;
|
|
119
119
|
inserted = cur.right;
|
|
@@ -151,7 +151,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
151
151
|
if (parent.left === undefined) {
|
|
152
152
|
parent.left = newNode;
|
|
153
153
|
if (newNode !== null) {
|
|
154
|
-
this.
|
|
154
|
+
this._size = this.size + 1;
|
|
155
155
|
this._setCount(this.count + newNode.count);
|
|
156
156
|
}
|
|
157
157
|
return parent.left;
|
|
@@ -159,7 +159,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
159
159
|
else if (parent.right === undefined) {
|
|
160
160
|
parent.right = newNode;
|
|
161
161
|
if (newNode !== null) {
|
|
162
|
-
this.
|
|
162
|
+
this._size = (this.size + 1);
|
|
163
163
|
this._setCount(this.count + newNode.count);
|
|
164
164
|
}
|
|
165
165
|
return parent.right;
|
|
@@ -304,7 +304,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
304
304
|
}
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
-
this.
|
|
307
|
+
this._size = this.size - 1;
|
|
308
308
|
// TODO How to handle when the count of target node is lesser than current node's count
|
|
309
309
|
this._setCount(this.count - orgCurrent.count);
|
|
310
310
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { DijkstraResult, VertexKey } from '../../types';
|
|
2
2
|
import { IGraph } from '../../interfaces';
|
|
3
3
|
export declare abstract class AbstractVertex<V = any> {
|
|
4
|
+
key: VertexKey;
|
|
5
|
+
value: V | undefined;
|
|
4
6
|
/**
|
|
5
7
|
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
6
8
|
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
@@ -9,14 +11,10 @@ export declare abstract class AbstractVertex<V = any> {
|
|
|
9
11
|
* vertex. If no value is provided, it will be set to undefined.
|
|
10
12
|
*/
|
|
11
13
|
protected constructor(key: VertexKey, value?: V);
|
|
12
|
-
private _key;
|
|
13
|
-
get key(): VertexKey;
|
|
14
|
-
set key(v: VertexKey);
|
|
15
|
-
private _value;
|
|
16
|
-
get value(): V | undefined;
|
|
17
|
-
set value(value: V | undefined);
|
|
18
14
|
}
|
|
19
15
|
export declare abstract class AbstractEdge<E = any> {
|
|
16
|
+
value: E | undefined;
|
|
17
|
+
weight: number;
|
|
20
18
|
/**
|
|
21
19
|
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
22
20
|
* object.
|
|
@@ -27,27 +25,11 @@ export declare abstract class AbstractEdge<E = any> {
|
|
|
27
25
|
* meaning it can be omitted when creating an instance of the class.
|
|
28
26
|
*/
|
|
29
27
|
protected constructor(weight?: number, value?: E);
|
|
30
|
-
private _value;
|
|
31
|
-
get value(): E | undefined;
|
|
32
|
-
set value(value: E | undefined);
|
|
33
|
-
private _weight;
|
|
34
|
-
get weight(): number;
|
|
35
|
-
set weight(v: number);
|
|
36
28
|
protected _hashCode: string;
|
|
37
29
|
get hashCode(): string;
|
|
38
|
-
/**
|
|
39
|
-
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
40
|
-
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* The function sets the value of the _hashCode property to the provided string.
|
|
44
|
-
* @param {string} v - The parameter "v" is of type string and represents the value that will be assigned to the
|
|
45
|
-
* "_hashCode" property.
|
|
46
|
-
*/
|
|
47
|
-
protected _setHashCode(v: string): void;
|
|
48
30
|
}
|
|
49
31
|
export declare abstract class AbstractGraph<V = any, E = any, VO extends AbstractVertex<V> = AbstractVertex<V>, EO extends AbstractEdge<E> = AbstractEdge<E>> implements IGraph<V, E, VO, EO> {
|
|
50
|
-
|
|
32
|
+
protected _vertices: Map<VertexKey, VO>;
|
|
51
33
|
get vertices(): Map<VertexKey, VO>;
|
|
52
34
|
/**
|
|
53
35
|
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
@@ -328,5 +310,4 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
|
|
|
328
310
|
protected _addVertexOnly(newVertex: VO): boolean;
|
|
329
311
|
protected _getVertex(vertexOrKey: VertexKey | VO): VO | null;
|
|
330
312
|
protected _getVertexKey(vertexOrKey: VO | VertexKey): VertexKey;
|
|
331
|
-
protected _setVertices(value: Map<VertexKey, VO>): void;
|
|
332
313
|
}
|