data-structure-typed 1.40.0 → 1.41.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  3. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  4. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +14 -3
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.js +52 -10
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/bst.d.ts +2 -2
  8. package/dist/cjs/data-structures/binary-tree/bst.js +3 -3
  9. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +96 -9
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.js +377 -12
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.js +2 -2
  14. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  15. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  16. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  17. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  18. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  19. package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
  20. package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
  21. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  22. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  23. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  24. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  25. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  26. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  27. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  28. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  29. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  30. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  31. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  32. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  33. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
  34. package/dist/cjs/types/data-structures/binary-tree/rb-tree.js +11 -6
  35. package/dist/cjs/types/data-structures/binary-tree/rb-tree.js.map +1 -1
  36. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +14 -3
  37. package/dist/mjs/data-structures/binary-tree/binary-tree.js +51 -10
  38. package/dist/mjs/data-structures/binary-tree/bst.d.ts +2 -2
  39. package/dist/mjs/data-structures/binary-tree/bst.js +3 -3
  40. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +96 -9
  41. package/dist/mjs/data-structures/binary-tree/rb-tree.js +382 -13
  42. package/dist/mjs/data-structures/binary-tree/tree-multiset.js +2 -2
  43. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
  44. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +11 -6
  45. package/dist/umd/data-structure-typed.min.js +1 -1
  46. package/dist/umd/data-structure-typed.min.js.map +1 -1
  47. package/package.json +9 -9
  48. package/src/data-structures/binary-tree/avl-tree.ts +3 -2
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  50. package/src/data-structures/binary-tree/binary-tree.ts +87 -17
  51. package/src/data-structures/binary-tree/bst.ts +10 -7
  52. package/src/data-structures/binary-tree/rb-tree.ts +396 -349
  53. package/src/data-structures/binary-tree/tree-multiset.ts +4 -3
  54. package/src/data-structures/graph/abstract-graph.ts +11 -12
  55. package/src/data-structures/graph/directed-graph.ts +7 -6
  56. package/src/data-structures/graph/undirected-graph.ts +7 -6
  57. package/src/data-structures/hash/hash-map.ts +1 -1
  58. package/src/data-structures/hash/tree-map.ts +1 -2
  59. package/src/data-structures/hash/tree-set.ts +1 -2
  60. package/src/data-structures/heap/heap.ts +2 -2
  61. package/src/data-structures/heap/max-heap.ts +1 -1
  62. package/src/data-structures/heap/min-heap.ts +1 -1
  63. package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
  64. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  65. package/src/data-structures/matrix/matrix.ts +1 -1
  66. package/src/data-structures/matrix/vector2d.ts +1 -2
  67. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  68. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  69. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  70. package/src/data-structures/queue/deque.ts +3 -4
  71. package/src/data-structures/queue/queue.ts +1 -1
  72. package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
  73. package/src/types/data-structures/matrix/navigator.ts +1 -1
  74. package/src/types/utils/utils.ts +1 -1
  75. package/src/types/utils/validate-type.ts +2 -2
  76. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +6 -6
  77. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +11 -11
  78. package/test/unit/data-structures/binary-tree/bst.test.ts +22 -20
  79. package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
  80. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +248 -79
  81. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +8 -8
  82. package/test/utils/big-o.js +10 -0
@@ -1,11 +1,98 @@
1
- import { BTNKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
- import { IBinaryTree } from '../../interfaces';
3
- import { BST, BSTNode } from './bst';
4
- export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
5
- constructor(key: BTNKey, value?: V);
6
- color: RBColor;
1
+ import { RBTNColor } from '../../types';
2
+ export declare class RBTreeNode {
3
+ key: number;
4
+ parent: RBTreeNode;
5
+ left: RBTreeNode;
6
+ right: RBTreeNode;
7
+ color: number;
8
+ constructor(key: number, color?: RBTNColor);
7
9
  }
8
- export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
9
- constructor(options?: RBTreeOptions);
10
- createNode(key: BTNKey, value?: V): N;
10
+ export declare const SN: RBTreeNode;
11
+ export declare class RedBlackTree {
12
+ constructor();
13
+ protected _root: RBTreeNode;
14
+ get root(): RBTreeNode;
15
+ /**
16
+ * The `insert` function inserts a new node with a given key into a red-black tree and fixes any
17
+ * violations of the red-black tree properties.
18
+ * @param {number} key - The key parameter is a number that represents the value to be inserted into
19
+ * the RBTree.
20
+ * @returns The function does not explicitly return anything.
21
+ */
22
+ insert(key: number): void;
23
+ /**
24
+ * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
25
+ * tree.
26
+ * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
27
+ * node being processed in the delete operation.
28
+ * @returns The `delete` function does not return anything. It has a return type of `void`.
29
+ */
30
+ delete(key: number): void;
31
+ isRealNode(node: RBTreeNode): node is RBTreeNode;
32
+ /**
33
+ * The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
34
+ * given key in a red-black tree.
35
+ * @param {number} key - The key parameter is a number that represents the value we are searching for
36
+ * in the RBTree.
37
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
38
+ * point for the search in the binary search tree. If no value is provided for `beginRoot`, it
39
+ * defaults to the root of the binary search tree (`this.root`).
40
+ * @returns a RBTreeNode.
41
+ */
42
+ getNode(key: number, beginRoot?: RBTreeNode): RBTreeNode;
43
+ /**
44
+ * The function returns the leftmost node in a red-black tree.
45
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
46
+ * a Red-Black Tree.
47
+ * @returns The leftmost node in the given RBTreeNode.
48
+ */
49
+ getLeftMost(node?: RBTreeNode): RBTreeNode;
50
+ /**
51
+ * The function returns the rightmost node in a red-black tree.
52
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
53
+ * @returns the rightmost node in a red-black tree.
54
+ */
55
+ getRightMost(node: RBTreeNode): RBTreeNode;
56
+ /**
57
+ * The function returns the successor of a given node in a red-black tree.
58
+ * @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
59
+ * @returns the successor of the given RBTreeNode.
60
+ */
61
+ getSuccessor(x: RBTreeNode): RBTreeNode;
62
+ /**
63
+ * The function returns the predecessor of a given node in a red-black tree.
64
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
65
+ * Red-Black Tree.
66
+ * @returns the predecessor of the given RBTreeNode 'x'.
67
+ */
68
+ getPredecessor(x: RBTreeNode): RBTreeNode;
69
+ /**
70
+ * The function performs a left rotation on a red-black tree node.
71
+ * @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
72
+ */
73
+ protected _leftRotate(x: RBTreeNode): void;
74
+ /**
75
+ * The function performs a right rotation on a red-black tree node.
76
+ * @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
77
+ * rotated.
78
+ */
79
+ protected _rightRotate(x: RBTreeNode): void;
80
+ /**
81
+ * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
82
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
83
+ * red-black tree.
84
+ */
85
+ protected _fixDelete(x: RBTreeNode): void;
86
+ /**
87
+ * The function `_rbTransplant` replaces one node in a red-black tree with another node.
88
+ * @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
89
+ * @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
90
+ */
91
+ protected _rbTransplant(u: RBTreeNode, v: RBTreeNode): void;
92
+ /**
93
+ * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
94
+ * @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
95
+ * red-black tree.
96
+ */
97
+ protected _fixInsert(k: RBTreeNode): void;
11
98
  }
@@ -1,22 +1,391 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RBTree = exports.RBTreeNode = void 0;
3
+ exports.RedBlackTree = exports.SN = exports.RBTreeNode = void 0;
4
4
  const types_1 = require("../../types");
5
- const bst_1 = require("./bst");
6
- class RBTreeNode extends bst_1.BSTNode {
7
- constructor(key, value) {
8
- super(key, value);
9
- this.color = types_1.RBColor.RED;
5
+ class RBTreeNode {
6
+ key;
7
+ parent;
8
+ left;
9
+ right;
10
+ color = types_1.RBTNColor.BLACK;
11
+ constructor(key, color = types_1.RBTNColor.BLACK) {
12
+ this.key = key;
13
+ this.color = color;
14
+ this.parent = null;
15
+ this.left = null;
16
+ this.right = null;
10
17
  }
11
- color;
12
18
  }
13
19
  exports.RBTreeNode = RBTreeNode;
14
- class RBTree extends bst_1.BST {
15
- constructor(options) {
16
- super(options);
20
+ exports.SN = new RBTreeNode(0);
21
+ class RedBlackTree {
22
+ constructor() {
23
+ this._root = exports.SN;
17
24
  }
18
- createNode(key, value) {
19
- return new RBTreeNode(key, value);
25
+ _root;
26
+ get root() {
27
+ return this._root;
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(key, types_1.RBTNColor.RED);
38
+ node.left = exports.SN;
39
+ node.right = exports.SN;
40
+ let y = null;
41
+ let x = this.root;
42
+ while (x !== exports.SN) {
43
+ y = x;
44
+ if (node.key < x.key) {
45
+ x = x.left;
46
+ }
47
+ else {
48
+ x = x.right;
49
+ }
50
+ }
51
+ node.parent = y;
52
+ if (y === null) {
53
+ this._root = node;
54
+ }
55
+ else if (node.key < y.key) {
56
+ y.left = node;
57
+ }
58
+ else {
59
+ y.right = node;
60
+ }
61
+ if (node.parent === null) {
62
+ node.color = types_1.RBTNColor.BLACK;
63
+ return;
64
+ }
65
+ if (node.parent.parent === null) {
66
+ return;
67
+ }
68
+ this._fixInsert(node);
69
+ }
70
+ /**
71
+ * The `delete` function in TypeScript is used to remove a node with a specific key from a red-black
72
+ * tree.
73
+ * @param {RBTreeNode} node - The `node` parameter is of type `RBTreeNode` and represents the current
74
+ * node being processed in the delete operation.
75
+ * @returns The `delete` function does not return anything. It has a return type of `void`.
76
+ */
77
+ delete(key) {
78
+ const helper = (node) => {
79
+ let z = exports.SN;
80
+ let x, y;
81
+ while (node !== exports.SN) {
82
+ if (node.key === key) {
83
+ z = node;
84
+ }
85
+ if (node.key <= key) {
86
+ node = node.right;
87
+ }
88
+ else {
89
+ node = node.left;
90
+ }
91
+ }
92
+ if (z === exports.SN) {
93
+ return;
94
+ }
95
+ y = z;
96
+ let yOriginalColor = y.color;
97
+ if (z.left === exports.SN) {
98
+ x = z.right;
99
+ this._rbTransplant(z, z.right);
100
+ }
101
+ else if (z.right === exports.SN) {
102
+ x = z.left;
103
+ this._rbTransplant(z, z.left);
104
+ }
105
+ else {
106
+ y = this.getLeftMost(z.right);
107
+ yOriginalColor = y.color;
108
+ x = y.right;
109
+ if (y.parent === z) {
110
+ x.parent = y;
111
+ }
112
+ else {
113
+ this._rbTransplant(y, y.right);
114
+ y.right = z.right;
115
+ y.right.parent = y;
116
+ }
117
+ this._rbTransplant(z, y);
118
+ y.left = z.left;
119
+ y.left.parent = y;
120
+ y.color = z.color;
121
+ }
122
+ if (yOriginalColor === types_1.RBTNColor.BLACK) {
123
+ this._fixDelete(x);
124
+ }
125
+ };
126
+ helper(this.root);
127
+ }
128
+ isRealNode(node) {
129
+ return node !== exports.SN && node !== null;
130
+ }
131
+ /**
132
+ * The function `getNode` is a recursive depth-first search algorithm that searches for a node with a
133
+ * given key in a red-black tree.
134
+ * @param {number} key - The key parameter is a number that represents the value we are searching for
135
+ * in the RBTree.
136
+ * @param beginRoot - The `beginRoot` parameter is an optional parameter that represents the starting
137
+ * point for the search in the binary search tree. If no value is provided for `beginRoot`, it
138
+ * defaults to the root of the binary search tree (`this.root`).
139
+ * @returns a RBTreeNode.
140
+ */
141
+ getNode(key, beginRoot = this.root) {
142
+ const dfs = (node) => {
143
+ if (this.isRealNode(node)) {
144
+ if (key === node.key) {
145
+ return node;
146
+ }
147
+ if (key < node.key)
148
+ return dfs(node.left);
149
+ return dfs(node.right);
150
+ }
151
+ else {
152
+ return null;
153
+ }
154
+ };
155
+ return dfs(beginRoot);
156
+ }
157
+ /**
158
+ * The function returns the leftmost node in a red-black tree.
159
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode, which represents a node in
160
+ * a Red-Black Tree.
161
+ * @returns The leftmost node in the given RBTreeNode.
162
+ */
163
+ getLeftMost(node = this.root) {
164
+ while (node.left !== null && node.left !== exports.SN) {
165
+ node = node.left;
166
+ }
167
+ return node;
168
+ }
169
+ /**
170
+ * The function returns the rightmost node in a red-black tree.
171
+ * @param {RBTreeNode} node - The parameter "node" is of type RBTreeNode.
172
+ * @returns the rightmost node in a red-black tree.
173
+ */
174
+ getRightMost(node) {
175
+ while (node.right !== null && node.right !== exports.SN) {
176
+ node = node.right;
177
+ }
178
+ return node;
179
+ }
180
+ /**
181
+ * The function returns the successor of a given node in a red-black tree.
182
+ * @param {RBTreeNode} x - RBTreeNode - The node for which we want to find the successor.
183
+ * @returns the successor of the given RBTreeNode.
184
+ */
185
+ getSuccessor(x) {
186
+ if (x.right !== exports.SN) {
187
+ return this.getLeftMost(x.right);
188
+ }
189
+ let y = x.parent;
190
+ while (y !== exports.SN && y !== null && x === y.right) {
191
+ x = y;
192
+ y = y.parent;
193
+ }
194
+ return y;
195
+ }
196
+ /**
197
+ * The function returns the predecessor of a given node in a red-black tree.
198
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
199
+ * Red-Black Tree.
200
+ * @returns the predecessor of the given RBTreeNode 'x'.
201
+ */
202
+ getPredecessor(x) {
203
+ if (x.left !== exports.SN) {
204
+ return this.getRightMost(x.left);
205
+ }
206
+ let y = x.parent;
207
+ while (y !== exports.SN && x === y.left) {
208
+ x = y;
209
+ y = y.parent;
210
+ }
211
+ return y;
212
+ }
213
+ /**
214
+ * The function performs a left rotation on a red-black tree node.
215
+ * @param {RBTreeNode} x - The parameter `x` is a RBTreeNode object.
216
+ */
217
+ _leftRotate(x) {
218
+ const y = x.right;
219
+ x.right = y.left;
220
+ if (y.left !== exports.SN) {
221
+ y.left.parent = x;
222
+ }
223
+ y.parent = x.parent;
224
+ if (x.parent === null) {
225
+ this._root = y;
226
+ }
227
+ else if (x === x.parent.left) {
228
+ x.parent.left = y;
229
+ }
230
+ else {
231
+ x.parent.right = y;
232
+ }
233
+ y.left = x;
234
+ x.parent = y;
235
+ }
236
+ /**
237
+ * The function performs a right rotation on a red-black tree node.
238
+ * @param {RBTreeNode} x - x is a RBTreeNode, which represents the node that needs to be right
239
+ * rotated.
240
+ */
241
+ _rightRotate(x) {
242
+ const y = x.left;
243
+ x.left = y.right;
244
+ if (y.right !== exports.SN) {
245
+ y.right.parent = x;
246
+ }
247
+ y.parent = x.parent;
248
+ if (x.parent === null) {
249
+ this._root = y;
250
+ }
251
+ else if (x === x.parent.right) {
252
+ x.parent.right = y;
253
+ }
254
+ else {
255
+ x.parent.left = y;
256
+ }
257
+ y.right = x;
258
+ x.parent = y;
259
+ }
260
+ /**
261
+ * The _fixDelete function is used to rebalance the Red-Black Tree after a node deletion.
262
+ * @param {RBTreeNode} x - The parameter `x` is of type `RBTreeNode`, which represents a node in a
263
+ * red-black tree.
264
+ */
265
+ _fixDelete(x) {
266
+ let s;
267
+ while (x !== this.root && x.color === types_1.RBTNColor.BLACK) {
268
+ if (x === x.parent.left) {
269
+ s = x.parent.right;
270
+ if (s.color === 1) {
271
+ s.color = types_1.RBTNColor.BLACK;
272
+ x.parent.color = types_1.RBTNColor.RED;
273
+ this._leftRotate(x.parent);
274
+ s = x.parent.right;
275
+ }
276
+ if (s.left !== null && s.left.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
277
+ s.color = types_1.RBTNColor.RED;
278
+ x = x.parent;
279
+ }
280
+ else {
281
+ if (s.right.color === types_1.RBTNColor.BLACK) {
282
+ s.left.color = types_1.RBTNColor.BLACK;
283
+ s.color = types_1.RBTNColor.RED;
284
+ this._rightRotate(s);
285
+ s = x.parent.right;
286
+ }
287
+ s.color = x.parent.color;
288
+ x.parent.color = types_1.RBTNColor.BLACK;
289
+ s.right.color = types_1.RBTNColor.BLACK;
290
+ this._leftRotate(x.parent);
291
+ x = this.root;
292
+ }
293
+ }
294
+ else {
295
+ s = x.parent.left;
296
+ if (s.color === 1) {
297
+ s.color = types_1.RBTNColor.BLACK;
298
+ x.parent.color = types_1.RBTNColor.RED;
299
+ this._rightRotate(x.parent);
300
+ s = x.parent.left;
301
+ }
302
+ if (s.right.color === types_1.RBTNColor.BLACK && s.right.color === types_1.RBTNColor.BLACK) {
303
+ s.color = types_1.RBTNColor.RED;
304
+ x = x.parent;
305
+ }
306
+ else {
307
+ if (s.left.color === types_1.RBTNColor.BLACK) {
308
+ s.right.color = types_1.RBTNColor.BLACK;
309
+ s.color = types_1.RBTNColor.RED;
310
+ this._leftRotate(s);
311
+ s = x.parent.left;
312
+ }
313
+ s.color = x.parent.color;
314
+ x.parent.color = types_1.RBTNColor.BLACK;
315
+ s.left.color = types_1.RBTNColor.BLACK;
316
+ this._rightRotate(x.parent);
317
+ x = this.root;
318
+ }
319
+ }
320
+ }
321
+ x.color = types_1.RBTNColor.BLACK;
322
+ }
323
+ /**
324
+ * The function `_rbTransplant` replaces one node in a red-black tree with another node.
325
+ * @param {RBTreeNode} u - The parameter "u" represents a RBTreeNode object.
326
+ * @param {RBTreeNode} v - The parameter "v" is a RBTreeNode object.
327
+ */
328
+ _rbTransplant(u, v) {
329
+ if (u.parent === null) {
330
+ this._root = v;
331
+ }
332
+ else if (u === u.parent.left) {
333
+ u.parent.left = v;
334
+ }
335
+ else {
336
+ u.parent.right = v;
337
+ }
338
+ v.parent = u.parent;
339
+ }
340
+ /**
341
+ * The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
342
+ * @param {RBTreeNode} k - The parameter `k` is a RBTreeNode object, which represents a node in a
343
+ * red-black tree.
344
+ */
345
+ _fixInsert(k) {
346
+ let u;
347
+ while (k.parent.color === 1) {
348
+ if (k.parent === k.parent.parent.right) {
349
+ u = k.parent.parent.left;
350
+ if (u.color === 1) {
351
+ u.color = types_1.RBTNColor.BLACK;
352
+ k.parent.color = types_1.RBTNColor.BLACK;
353
+ k.parent.parent.color = types_1.RBTNColor.RED;
354
+ k = k.parent.parent;
355
+ }
356
+ else {
357
+ if (k === k.parent.left) {
358
+ k = k.parent;
359
+ this._rightRotate(k);
360
+ }
361
+ k.parent.color = types_1.RBTNColor.BLACK;
362
+ k.parent.parent.color = types_1.RBTNColor.RED;
363
+ this._leftRotate(k.parent.parent);
364
+ }
365
+ }
366
+ else {
367
+ u = k.parent.parent.right;
368
+ if (u.color === 1) {
369
+ u.color = types_1.RBTNColor.BLACK;
370
+ k.parent.color = types_1.RBTNColor.BLACK;
371
+ k.parent.parent.color = types_1.RBTNColor.RED;
372
+ k = k.parent.parent;
373
+ }
374
+ else {
375
+ if (k === k.parent.right) {
376
+ k = k.parent;
377
+ this._leftRotate(k);
378
+ }
379
+ k.parent.color = types_1.RBTNColor.BLACK;
380
+ k.parent.parent.color = types_1.RBTNColor.RED;
381
+ this._rightRotate(k.parent.parent);
382
+ }
383
+ }
384
+ if (k === this.root) {
385
+ break;
386
+ }
387
+ }
388
+ this.root.color = types_1.RBTNColor.BLACK;
20
389
  }
21
390
  }
22
- exports.RBTree = RBTree;
391
+ exports.RedBlackTree = RedBlackTree;
@@ -160,7 +160,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
160
160
  else if (parent.right === undefined) {
161
161
  parent.right = newNode;
162
162
  if (newNode !== null) {
163
- this._size = (this.size + 1);
163
+ this._size = this.size + 1;
164
164
  this._setCount(this.count + newNode.count);
165
165
  }
166
166
  return parent.right;
@@ -263,7 +263,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
263
263
  const bstDeletedResult = [];
264
264
  if (!this.root)
265
265
  return bstDeletedResult;
266
- const curr = this.get(identifier, callback);
266
+ const curr = this.getNode(identifier, callback);
267
267
  if (!curr)
268
268
  return bstDeletedResult;
269
269
  const parent = curr?.parent ? curr.parent : null;
@@ -1,8 +1,4 @@
1
- import { BinaryTreeOptions } from './binary-tree';
2
- import { RBTreeNode } from '../../../data-structures';
3
- export declare enum RBColor {
4
- RED = "RED",
5
- BLACK = "BLACK"
1
+ export declare enum RBTNColor {
2
+ RED = 1,
3
+ BLACK = 0
6
4
  }
7
- export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
8
- export type RBTreeOptions = BinaryTreeOptions & {};
@@ -1,8 +1,13 @@
1
1
  "use strict";
2
+ // import {BinaryTreeOptions} from './binary-tree';
3
+ // import {RBTreeNode} from '../../../data-structures';
2
4
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RBColor = void 0;
4
- var RBColor;
5
- (function (RBColor) {
6
- RBColor["RED"] = "RED";
7
- RBColor["BLACK"] = "BLACK";
8
- })(RBColor || (exports.RBColor = RBColor = {}));
5
+ exports.RBTNColor = void 0;
6
+ var RBTNColor;
7
+ (function (RBTNColor) {
8
+ RBTNColor[RBTNColor["RED"] = 1] = "RED";
9
+ RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
10
+ })(RBTNColor || (exports.RBTNColor = RBTNColor = {}));
11
+ // export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
12
+ //
13
+ // export type RBTreeOptions = BinaryTreeOptions & {}