min-heap-typed 1.50.2 → 1.50.4

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 (92) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +6 -0
  2. package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +43 -10
  3. package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +49 -11
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +29 -1
  5. package/dist/data-structures/binary-tree/avl-tree.js +33 -1
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  8. package/dist/data-structures/binary-tree/binary-tree.d.ts +1 -1
  9. package/dist/data-structures/binary-tree/binary-tree.js +1 -1
  10. package/dist/data-structures/binary-tree/bst.d.ts +46 -13
  11. package/dist/data-structures/binary-tree/bst.js +51 -20
  12. package/dist/data-structures/binary-tree/index.d.ts +2 -1
  13. package/dist/data-structures/binary-tree/index.js +2 -1
  14. package/dist/data-structures/binary-tree/rb-tree.d.ts +54 -2
  15. package/dist/data-structures/binary-tree/rb-tree.js +90 -24
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +99 -6
  17. package/dist/data-structures/binary-tree/segment-tree.js +127 -10
  18. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  19. package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
  20. package/dist/data-structures/graph/abstract-graph.d.ts +0 -78
  21. package/dist/data-structures/graph/abstract-graph.js +0 -189
  22. package/dist/data-structures/graph/directed-graph.d.ts +59 -0
  23. package/dist/data-structures/graph/directed-graph.js +105 -0
  24. package/dist/data-structures/graph/undirected-graph.d.ts +60 -7
  25. package/dist/data-structures/graph/undirected-graph.js +126 -18
  26. package/dist/data-structures/hash/hash-map.d.ts +143 -23
  27. package/dist/data-structures/hash/hash-map.js +196 -62
  28. package/dist/data-structures/heap/heap.d.ts +29 -19
  29. package/dist/data-structures/heap/heap.js +29 -20
  30. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
  31. package/dist/data-structures/linked-list/doubly-linked-list.js +83 -25
  32. package/dist/data-structures/linked-list/singly-linked-list.d.ts +26 -3
  33. package/dist/data-structures/linked-list/singly-linked-list.js +34 -3
  34. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  35. package/dist/data-structures/linked-list/skip-linked-list.js +2 -2
  36. package/dist/data-structures/matrix/matrix.d.ts +1 -1
  37. package/dist/data-structures/matrix/matrix.js +1 -1
  38. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  39. package/dist/data-structures/priority-queue/max-priority-queue.js +10 -0
  40. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  41. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -0
  42. package/dist/data-structures/priority-queue/priority-queue.d.ts +8 -0
  43. package/dist/data-structures/priority-queue/priority-queue.js +8 -0
  44. package/dist/data-structures/queue/deque.d.ts +95 -21
  45. package/dist/data-structures/queue/deque.js +100 -16
  46. package/dist/data-structures/queue/queue.d.ts +65 -45
  47. package/dist/data-structures/queue/queue.js +65 -45
  48. package/dist/data-structures/stack/stack.d.ts +36 -22
  49. package/dist/data-structures/stack/stack.js +36 -22
  50. package/dist/data-structures/tree/tree.d.ts +57 -3
  51. package/dist/data-structures/tree/tree.js +77 -11
  52. package/dist/data-structures/trie/trie.d.ts +100 -36
  53. package/dist/data-structures/trie/trie.js +115 -36
  54. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  55. package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
  56. package/dist/types/data-structures/binary-tree/index.js +2 -1
  57. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  58. package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
  59. package/package.json +2 -2
  60. package/src/data-structures/base/iterable-base.ts +12 -0
  61. package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +59 -20
  62. package/src/data-structures/binary-tree/avl-tree.ts +37 -3
  63. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  64. package/src/data-structures/binary-tree/binary-tree.ts +1 -1
  65. package/src/data-structures/binary-tree/bst.ts +51 -19
  66. package/src/data-structures/binary-tree/index.ts +2 -1
  67. package/src/data-structures/binary-tree/rb-tree.ts +99 -28
  68. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  69. package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
  70. package/src/data-structures/graph/abstract-graph.ts +0 -211
  71. package/src/data-structures/graph/directed-graph.ts +122 -0
  72. package/src/data-structures/graph/undirected-graph.ts +143 -19
  73. package/src/data-structures/hash/hash-map.ts +228 -76
  74. package/src/data-structures/heap/heap.ts +31 -20
  75. package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
  76. package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
  77. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  78. package/src/data-structures/matrix/matrix.ts +1 -1
  79. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  80. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  81. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  82. package/src/data-structures/queue/deque.ts +118 -22
  83. package/src/data-structures/queue/queue.ts +68 -45
  84. package/src/data-structures/stack/stack.ts +39 -23
  85. package/src/data-structures/tree/tree.ts +89 -15
  86. package/src/data-structures/trie/trie.ts +131 -40
  87. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
  88. package/src/types/data-structures/binary-tree/index.ts +2 -1
  89. package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
  90. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  91. package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
  92. /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
@@ -34,10 +34,19 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
34
34
 
35
35
  protected override _left?: NODE;
36
36
 
37
+ /**
38
+ * The function returns the value of the `_left` property.
39
+ * @returns The `_left` property of the current object is being returned.
40
+ */
37
41
  override get left(): NODE | undefined {
38
42
  return this._left;
39
43
  }
40
44
 
45
+ /**
46
+ * The function sets the left child of a node and updates the parent reference of the child.
47
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
48
+ * instance of the `NODE` class or `undefined`.
49
+ */
41
50
  override set left(v: NODE | undefined) {
42
51
  if (v) {
43
52
  v.parent = this as unknown as NODE;
@@ -47,10 +56,20 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
47
56
 
48
57
  protected override _right?: NODE;
49
58
 
59
+ /**
60
+ * The function returns the right node of a binary tree or undefined if there is no right node.
61
+ * @returns The method is returning the value of the `_right` property, which is of type `NODE` or
62
+ * `undefined`.
63
+ */
50
64
  override get right(): NODE | undefined {
51
65
  return this._right;
52
66
  }
53
67
 
68
+ /**
69
+ * The function sets the right child of a node and updates the parent reference of the child.
70
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
71
+ * `NODE` object or `undefined`.
72
+ */
54
73
  override set right(v: NODE | undefined) {
55
74
  if (v) {
56
75
  v.parent = this as unknown as NODE;
@@ -77,10 +96,10 @@ export class BST<
77
96
  extends BinaryTree<K, V, NODE, TREE>
78
97
  implements IBinaryTree<K, V, NODE, TREE> {
79
98
  /**
80
- * This is the constructor function for a binary search tree class in TypeScript, which initializes
81
- * the tree with optional keysOrNodesOrEntries and options.
82
- * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects that will be added to the
83
- * binary search tree.
99
+ * This is the constructor function for a TypeScript class that initializes a binary search tree with
100
+ * optional keys or nodes or entries and options.
101
+ * @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
102
+ * to initialize the binary search tree with the provided keys, nodes, or entries.
84
103
  * @param [options] - The `options` parameter is an optional object that can contain additional
85
104
  * configuration options for the binary search tree. It can have the following properties:
86
105
  */
@@ -99,23 +118,31 @@ export class BST<
99
118
 
100
119
  protected override _root?: NODE;
101
120
 
121
+ /**
122
+ * The function returns the root node of a tree structure.
123
+ * @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
124
+ */
102
125
  override get root(): NODE | undefined {
103
126
  return this._root;
104
127
  }
105
128
 
106
129
  protected _variant = BSTVariant.STANDARD;
107
130
 
131
+ /**
132
+ * The function returns the value of the _variant property.
133
+ * @returns The value of the `_variant` property.
134
+ */
108
135
  get variant() {
109
136
  return this._variant;
110
137
  }
111
138
 
112
139
  /**
113
- * The function creates a new binary search tree node with the given key and value.
114
- * @param {K} key - The key parameter is the key value that will be associated with
115
- * the new node. It is used to determine the position of the node in the binary search tree.
116
- * @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
117
- * represents the value associated with the node in a binary search tree.
118
- * @returns a new instance of the BSTNode class with the specified key and value.
140
+ * The function creates a new BSTNode with the given key and value and returns it.
141
+ * @param {K} key - The key parameter is of type K, which represents the type of the key for the node
142
+ * being created.
143
+ * @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
144
+ * value associated with the key in the node being created.
145
+ * @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
119
146
  */
120
147
  override createNode(key: K, value?: V): NODE {
121
148
  return new BSTNode<K, V, NODE>(key, value) as NODE;
@@ -124,9 +151,10 @@ export class BST<
124
151
  /**
125
152
  * The function creates a new binary search tree with the specified options.
126
153
  * @param [options] - The `options` parameter is an optional object that allows you to customize the
127
- * behavior of the `createTree` method. It accepts a partial `BSTOptions` object, which is a type
128
- * that defines various options for creating a binary search tree.
129
- * @returns a new instance of the BST class with the specified options.
154
+ * behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
155
+ * partial object of type `BSTOptions<K>`.
156
+ * @returns a new instance of the BST class, with the provided options merged with the default
157
+ * options. The returned value is casted as TREE.
130
158
  */
131
159
  override createTree(options?: Partial<BSTOptions<K>>): TREE {
132
160
  return new BST<K, V, NODE, TREE>([], {
@@ -680,9 +708,8 @@ export class BST<
680
708
  const compared = this._compare(cur.key, targetKey);
681
709
  if (compared === lesserOrGreater) ans.push(callback(cur));
682
710
 
683
- if (!cur.left && !cur.right) return;
684
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) _traverse(cur.left);
685
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) _traverse(cur.right);
711
+ if (this.isRealNode(cur.left)) _traverse(cur.left);
712
+ if (this.isRealNode(cur.right)) _traverse(cur.right);
686
713
  };
687
714
 
688
715
  _traverse(this.root);
@@ -691,12 +718,12 @@ export class BST<
691
718
  const queue = new Queue<NODE>([this.root]);
692
719
  while (queue.size > 0) {
693
720
  const cur = queue.shift();
694
- if (cur) {
721
+ if (this.isRealNode(cur)) {
695
722
  const compared = this._compare(cur.key, targetKey);
696
723
  if (compared === lesserOrGreater) ans.push(callback(cur));
697
724
 
698
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) queue.push(cur.left);
699
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) queue.push(cur.right);
725
+ if (this.isRealNode(cur.left)) queue.push(cur.left);
726
+ if (this.isRealNode(cur.right)) queue.push(cur.right);
700
727
  }
701
728
  }
702
729
  return ans;
@@ -824,6 +851,11 @@ export class BST<
824
851
  return balanced;
825
852
  }
826
853
 
854
+ /**
855
+ * The function sets the root property of an object and updates the parent property of the new root.
856
+ * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
857
+ * can either be an object of type `NODE` or it can be `undefined`.
858
+ */
827
859
  protected _setRoot(v: NODE | undefined) {
828
860
  if (v) {
829
861
  v.parent = undefined;
@@ -4,4 +4,5 @@ export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
5
  export * from './avl-tree';
6
6
  export * from './rb-tree';
7
- export * from './tree-multimap';
7
+ export * from './avl-tree-multi-map';
8
+ export * from './tree-multi-map';
@@ -24,11 +24,38 @@ export class RedBlackTreeNode<
24
24
  V = any,
25
25
  NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
26
26
  > extends BSTNode<K, V, NODE> {
27
- color: RBTNColor;
28
-
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
+ */
29
38
  constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
30
39
  super(key, value);
31
- this.color = color;
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;
32
59
  }
33
60
  }
34
61
 
@@ -47,8 +74,6 @@ export class RedBlackTree<
47
74
  >
48
75
  extends BST<K, V, NODE, TREE>
49
76
  implements IBinaryTree<K, V, NODE, TREE> {
50
- Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
51
-
52
77
  /**
53
78
  * This is the constructor function for a Red-Black Tree data structure in TypeScript, which
54
79
  * initializes the tree with optional nodes and options.
@@ -63,18 +88,36 @@ export class RedBlackTree<
63
88
  constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
64
89
  super([], options);
65
90
 
66
- this._root = this.Sentinel;
91
+ this._root = this._Sentinel;
67
92
  if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
68
93
  }
69
94
 
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
+
70
105
  protected _root: NODE;
71
106
 
107
+ /**
108
+ * The function returns the root node.
109
+ * @returns The root node of the data structure.
110
+ */
72
111
  get root(): NODE {
73
112
  return this._root;
74
113
  }
75
114
 
76
115
  protected _size: number = 0;
77
116
 
117
+ /**
118
+ * The function returns the size of an object.
119
+ * @returns The size of the object, which is a number.
120
+ */
78
121
  get size(): number {
79
122
  return this._size;
80
123
  }
@@ -149,8 +192,14 @@ export class RedBlackTree<
149
192
  return keyOrNodeOrEntry instanceof RedBlackTreeNode;
150
193
  }
151
194
 
195
+ /**
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.
200
+ */
152
201
  override isRealNode(node: NODE | undefined): node is NODE {
153
- if (node === this.Sentinel || node === undefined) return false;
202
+ if (node === this._Sentinel || node === undefined) return false;
154
203
  return node instanceof RedBlackTreeNode;
155
204
  }
156
205
 
@@ -176,13 +225,13 @@ export class RedBlackTree<
176
225
  const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
177
226
  if (newNode === undefined) return false;
178
227
 
179
- newNode.left = this.Sentinel;
180
- newNode.right = this.Sentinel;
228
+ newNode.left = this._Sentinel;
229
+ newNode.right = this._Sentinel;
181
230
 
182
231
  let y: NODE | undefined = undefined;
183
232
  let x: NODE | undefined = this.root;
184
233
 
185
- while (x !== this.Sentinel) {
234
+ while (x !== this._Sentinel) {
186
235
  y = x;
187
236
  if (x) {
188
237
  if (newNode.key < x.key) {
@@ -250,9 +299,9 @@ export class RedBlackTree<
250
299
  const ans: BinaryTreeDeleteResult<NODE>[] = [];
251
300
  if (identifier === null) return ans;
252
301
  const helper = (node: NODE | undefined): void => {
253
- let z: NODE = this.Sentinel;
302
+ let z: NODE = this._Sentinel;
254
303
  let x: NODE | undefined, y: NODE;
255
- while (node !== this.Sentinel) {
304
+ while (node !== this._Sentinel) {
256
305
  if (node && callback(node) === identifier) {
257
306
  z = node;
258
307
  }
@@ -264,17 +313,17 @@ export class RedBlackTree<
264
313
  }
265
314
  }
266
315
 
267
- if (z === this.Sentinel) {
316
+ if (z === this._Sentinel) {
268
317
  this._size--;
269
318
  return;
270
319
  }
271
320
 
272
321
  y = z;
273
322
  let yOriginalColor: number = y.color;
274
- if (z.left === this.Sentinel) {
323
+ if (z.left === this._Sentinel) {
275
324
  x = z.right;
276
325
  this._rbTransplant(z, z.right!);
277
- } else if (z.right === this.Sentinel) {
326
+ } else if (z.right === this._Sentinel) {
278
327
  x = z.left;
279
328
  this._rbTransplant(z, z.left!);
280
329
  } else {
@@ -346,8 +395,14 @@ export class RedBlackTree<
346
395
  * Space Complexity: O(1)
347
396
  */
348
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
+ */
349
404
  override clear() {
350
- this._root = this.Sentinel;
405
+ this._root = this._Sentinel;
351
406
  this._size = 0;
352
407
  }
353
408
 
@@ -379,6 +434,12 @@ export class RedBlackTree<
379
434
  return y!;
380
435
  }
381
436
 
437
+ /**
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.
442
+ */
382
443
  protected override _setRoot(v: NODE) {
383
444
  if (v) {
384
445
  v.parent = undefined;
@@ -402,7 +463,7 @@ export class RedBlackTree<
402
463
  if (x.right) {
403
464
  const y: NODE = x.right;
404
465
  x.right = y.left;
405
- if (y.left !== this.Sentinel) {
466
+ if (y.left !== this._Sentinel) {
406
467
  if (y.left) y.left.parent = x;
407
468
  }
408
469
  y.parent = x.parent;
@@ -435,7 +496,7 @@ export class RedBlackTree<
435
496
  if (x.left) {
436
497
  const y: NODE = x.left;
437
498
  x.left = y.right;
438
- if (y.right !== this.Sentinel) {
499
+ if (y.right !== this._Sentinel) {
439
500
  if (y.right) y.right.parent = x;
440
501
  }
441
502
  y.parent = x.parent;
@@ -466,12 +527,14 @@ export class RedBlackTree<
466
527
  */
467
528
  protected _fixInsert(k: NODE): void {
468
529
  let u: NODE | undefined;
469
- while (k.parent && k.parent.color === 1) {
530
+ while (k.parent && k.parent.color === RBTNColor.RED) {
470
531
  if (k.parent.parent && k.parent === k.parent.parent.right) {
471
532
  u = k.parent.parent.left;
472
- if (u && u.color === 1) {
473
- u.color = RBTNColor.BLACK;
533
+
534
+ if (u && u.color === RBTNColor.RED) {
535
+ // Delay color flip
474
536
  k.parent.color = RBTNColor.BLACK;
537
+ u.color = RBTNColor.BLACK;
475
538
  k.parent.parent.color = RBTNColor.RED;
476
539
  k = k.parent.parent;
477
540
  } else {
@@ -480,16 +543,20 @@ export class RedBlackTree<
480
543
  this._rightRotate(k);
481
544
  }
482
545
 
483
- k.parent!.color = RBTNColor.BLACK;
484
- k.parent!.parent!.color = RBTNColor.RED;
546
+ // Check color before rotation
547
+ if (k.parent!.color === RBTNColor.RED) {
548
+ k.parent!.color = RBTNColor.BLACK;
549
+ k.parent!.parent!.color = RBTNColor.RED;
550
+ }
485
551
  this._leftRotate(k.parent!.parent!);
486
552
  }
487
553
  } else {
488
- u = k.parent.parent!.right;
554
+ u = k.parent!.parent!.right;
489
555
 
490
- if (u && u.color === 1) {
491
- u.color = RBTNColor.BLACK;
556
+ if (u && u.color === RBTNColor.RED) {
557
+ // Delay color flip
492
558
  k.parent.color = RBTNColor.BLACK;
559
+ u.color = RBTNColor.BLACK;
493
560
  k.parent.parent!.color = RBTNColor.RED;
494
561
  k = k.parent.parent!;
495
562
  } else {
@@ -498,11 +565,15 @@ export class RedBlackTree<
498
565
  this._leftRotate(k);
499
566
  }
500
567
 
501
- k.parent!.color = RBTNColor.BLACK;
502
- k.parent!.parent!.color = RBTNColor.RED;
568
+ // Check color before rotation
569
+ if (k.parent!.color === RBTNColor.RED) {
570
+ k.parent!.color = RBTNColor.BLACK;
571
+ k.parent!.parent!.color = RBTNColor.RED;
572
+ }
503
573
  this._rightRotate(k.parent!.parent!);
504
574
  }
505
575
  }
576
+
506
577
  if (k === this.root) {
507
578
  break;
508
579
  }
@@ -9,18 +9,136 @@
9
9
  import type { SegmentTreeNodeVal } from '../../types';
10
10
 
11
11
  export class SegmentTreeNode {
12
- start = 0;
13
- end = 0;
14
- value: SegmentTreeNodeVal | undefined = undefined;
15
- sum = 0;
16
- left: SegmentTreeNode | undefined = undefined;
17
- right: SegmentTreeNode | undefined = undefined;
18
-
12
+ /**
13
+ * The constructor initializes the properties of a SegmentTreeNode object.
14
+ * @param {number} start - The `start` parameter represents the starting index of the segment covered
15
+ * by this node in a segment tree.
16
+ * @param {number} end - The `end` parameter represents the end index of the segment covered by this
17
+ * node in a segment tree.
18
+ * @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
19
+ * the segment tree node.
20
+ * @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
21
+ * of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
22
+ */
19
23
  constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
20
- this.start = start;
21
- this.end = end;
22
- this.sum = sum;
23
- this.value = value || undefined;
24
+ this._start = start;
25
+ this._end = end;
26
+ this._sum = sum;
27
+ this._value = value || undefined;
28
+ }
29
+
30
+ protected _start = 0;
31
+
32
+ /**
33
+ * The function returns the value of the protected variable _start.
34
+ * @returns The start value, which is of type number.
35
+ */
36
+ get start(): number {
37
+ return this._start;
38
+ }
39
+
40
+ /**
41
+ * The above function sets the value of the "start" property.
42
+ * @param {number} value - The value parameter is of type number.
43
+ */
44
+ set start(value: number) {
45
+ this._start = value;
46
+ }
47
+
48
+ protected _end = 0;
49
+
50
+ /**
51
+ * The function returns the value of the protected variable `_end`.
52
+ * @returns The value of the protected property `_end`.
53
+ */
54
+ get end(): number {
55
+ return this._end;
56
+ }
57
+
58
+ /**
59
+ * The above function sets the value of the "end" property.
60
+ * @param {number} value - The value parameter is a number that represents the new value for the end
61
+ * property.
62
+ */
63
+ set end(value: number) {
64
+ this._end = value;
65
+ }
66
+
67
+ protected _value: SegmentTreeNodeVal | undefined = undefined;
68
+
69
+ /**
70
+ * The function returns the value of a segment tree node.
71
+ * @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
72
+ */
73
+ get value(): SegmentTreeNodeVal | undefined {
74
+ return this._value;
75
+ }
76
+
77
+ /**
78
+ * The function sets the value of a segment tree node.
79
+ * @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
80
+ * `SegmentTreeNodeVal` or `undefined`.
81
+ */
82
+ set value(value: SegmentTreeNodeVal | undefined) {
83
+ this._value = value;
84
+ }
85
+
86
+ protected _sum = 0;
87
+
88
+ /**
89
+ * The function returns the value of the sum property.
90
+ * @returns The method is returning the value of the variable `_sum`.
91
+ */
92
+ get sum(): number {
93
+ return this._sum;
94
+ }
95
+
96
+ /**
97
+ * The above function sets the value of the sum property.
98
+ * @param {number} value - The parameter "value" is of type "number".
99
+ */
100
+ set sum(value: number) {
101
+ this._sum = value;
102
+ }
103
+
104
+ protected _left: SegmentTreeNode | undefined = undefined;
105
+
106
+ /**
107
+ * The function returns the left child of a segment tree node.
108
+ * @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
109
+ * `SegmentTreeNode` or `undefined`.
110
+ */
111
+ get left(): SegmentTreeNode | undefined {
112
+ return this._left;
113
+ }
114
+
115
+ /**
116
+ * The function sets the value of the left property of a SegmentTreeNode object.
117
+ * @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
118
+ * undefined.
119
+ */
120
+ set left(value: SegmentTreeNode | undefined) {
121
+ this._left = value;
122
+ }
123
+
124
+ protected _right: SegmentTreeNode | undefined = undefined;
125
+
126
+ /**
127
+ * The function returns the right child of a segment tree node.
128
+ * @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
129
+ */
130
+ get right(): SegmentTreeNode | undefined {
131
+ return this._right;
132
+ }
133
+
134
+ /**
135
+ * The function sets the right child of a segment tree node.
136
+ * @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
137
+ * undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
138
+ * value.
139
+ */
140
+ set right(value: SegmentTreeNode | undefined) {
141
+ this._right = value;
24
142
  }
25
143
  }
26
144
 
@@ -51,24 +169,40 @@ export class SegmentTree {
51
169
 
52
170
  protected _values: number[] = [];
53
171
 
172
+ /**
173
+ * The function returns an array of numbers.
174
+ * @returns An array of numbers is being returned.
175
+ */
54
176
  get values(): number[] {
55
177
  return this._values;
56
178
  }
57
179
 
58
180
  protected _start = 0;
59
181
 
182
+ /**
183
+ * The function returns the value of the protected variable _start.
184
+ * @returns The start value, which is of type number.
185
+ */
60
186
  get start(): number {
61
187
  return this._start;
62
188
  }
63
189
 
64
190
  protected _end: number;
65
191
 
192
+ /**
193
+ * The function returns the value of the protected variable `_end`.
194
+ * @returns The value of the protected property `_end`.
195
+ */
66
196
  get end(): number {
67
197
  return this._end;
68
198
  }
69
199
 
70
200
  protected _root: SegmentTreeNode | undefined;
71
201
 
202
+ /**
203
+ * The function returns the root node of a segment tree.
204
+ * @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
205
+ */
72
206
  get root(): SegmentTreeNode | undefined {
73
207
  return this._root;
74
208
  }