stack-typed 1.53.9 → 1.54.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.
Files changed (33) hide show
  1. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -17
  2. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -24
  4. package/dist/data-structures/binary-tree/avl-tree.js +18 -34
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +40 -37
  6. package/dist/data-structures/binary-tree/binary-tree.js +68 -53
  7. package/dist/data-structures/binary-tree/bst.d.ts +36 -50
  8. package/dist/data-structures/binary-tree/bst.js +45 -60
  9. package/dist/data-structures/binary-tree/red-black-tree.d.ts +29 -43
  10. package/dist/data-structures/binary-tree/red-black-tree.js +45 -59
  11. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
  12. package/dist/data-structures/binary-tree/tree-multi-map.js +70 -83
  13. package/dist/interfaces/binary-tree.d.ts +4 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
  15. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
  18. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
  19. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
  20. package/package.json +2 -2
  21. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +23 -31
  22. package/src/data-structures/binary-tree/avl-tree.ts +35 -46
  23. package/src/data-structures/binary-tree/binary-tree.ts +105 -66
  24. package/src/data-structures/binary-tree/bst.ts +105 -104
  25. package/src/data-structures/binary-tree/red-black-tree.ts +68 -73
  26. package/src/data-structures/binary-tree/tree-multi-map.ts +98 -102
  27. package/src/interfaces/binary-tree.ts +16 -3
  28. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +4 -2
  29. package/src/types/data-structures/binary-tree/avl-tree.ts +4 -2
  30. package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
  31. package/src/types/data-structures/binary-tree/bst.ts +4 -2
  32. package/src/types/data-structures/binary-tree/rb-tree.ts +6 -4
  33. package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -2
@@ -7,6 +7,7 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type {
10
+ AVLTreeNested,
10
11
  AVLTreeNodeNested,
11
12
  AVLTreeOptions,
12
13
  BinaryTreeDeleteResult,
@@ -31,26 +32,6 @@ export class AVLTreeNode<
31
32
  */
32
33
  constructor(key: K, value?: V) {
33
34
  super(key, value);
34
- this._height = 0;
35
- }
36
-
37
- protected _height: number;
38
-
39
- /**
40
- * The function returns the value of the height property.
41
- * @returns The height of the object.
42
- */
43
- get height(): number {
44
- return this._height;
45
- }
46
-
47
- /**
48
- * The above function sets the value of the height property.
49
- * @param {number} value - The value parameter is a number that represents the new height value to be
50
- * set.
51
- */
52
- set height(value: number) {
53
- this._height = value;
54
35
  }
55
36
  }
56
37
 
@@ -67,10 +48,23 @@ export class AVLTree<
67
48
  K = any,
68
49
  V = any,
69
50
  R = object,
70
- NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>
51
+ MK = any,
52
+ MV = any,
53
+ MR = object,
54
+ NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
55
+ TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
56
+ K,
57
+ V,
58
+ R,
59
+ MK,
60
+ MV,
61
+ MR,
62
+ NODE,
63
+ AVLTreeNested<K, V, R, MK, MV, MR, NODE>
64
+ >
71
65
  >
72
- extends BST<K, V, R, NODE>
73
- implements IBinaryTree<K, V, R, NODE>
66
+ extends BST<K, V, R, MK, MV, MR, NODE, TREE>
67
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
74
68
  {
75
69
  /**
76
70
  * This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
@@ -102,25 +96,21 @@ export class AVLTree<
102
96
  }
103
97
 
104
98
  /**
105
- * The function `createTree` in TypeScript overrides the default AVLTree creation with the provided
106
- * options.
107
- * @param [options] - The `options` parameter in the `createTree` function is an object that contains
108
- * configuration options for creating an AVL tree. These options can include properties such as
109
- * `iterationType`, `isMapMode`, `specifyComparable`, `toEntryFn`, and `isReverse`. The function
110
- * creates a
111
- * @returns An AVLTree object is being returned with the specified options and properties inherited
112
- * from the current object.
99
+ * The function creates a new AVL tree with the specified options and returns it.
100
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
101
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
102
+ * being created.
103
+ * @returns a new AVLTree object.
113
104
  */
114
- // @ts-ignore
115
- override createTree(options?: AVLTreeOptions<K, V, R>) {
116
- return new AVLTree<K, V, R, NODE>([], {
105
+ override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
106
+ return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
117
107
  iterationType: this.iterationType,
118
108
  isMapMode: this._isMapMode,
119
109
  specifyComparable: this._specifyComparable,
120
110
  toEntryFn: this._toEntryFn,
121
111
  isReverse: this._isReverse,
122
112
  ...options
123
- });
113
+ }) as TREE;
124
114
  }
125
115
 
126
116
  /**
@@ -177,12 +167,11 @@ export class AVLTree<
177
167
  return deletedResults;
178
168
  }
179
169
 
180
- // @ts-ignore
181
- override map<MK, MV, MR>(
170
+ override map(
182
171
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
183
172
  options?: AVLTreeOptions<MK, MV, MR>,
184
173
  thisArg?: any
185
- ) {
174
+ ): AVLTree<MK, MV, MR> {
186
175
  const newTree = new AVLTree<MK, MV, MR>([], options);
187
176
  let index = 0;
188
177
  for (const [key, value] of this) {
@@ -279,7 +268,7 @@ export class AVLTree<
279
268
  protected _balanceLL(A: NODE): void {
280
269
  const parentOfA = A.parent;
281
270
  const B = A.left;
282
- A.parent = B;
271
+ if (B !== null) A.parent = B;
283
272
  if (B && B.right) {
284
273
  B.right.parent = A;
285
274
  }
@@ -316,12 +305,12 @@ export class AVLTree<
316
305
  if (B) {
317
306
  C = B.right;
318
307
  }
319
- if (A) A.parent = C;
320
- if (B) B.parent = C;
308
+ if (A && C !== null) A.parent = C;
309
+ if (B && C !== null) B.parent = C;
321
310
 
322
311
  if (C) {
323
312
  if (C.left) {
324
- C.left.parent = B;
313
+ if (B !== null) C.left.parent = B;
325
314
  }
326
315
  if (C.right) {
327
316
  C.right.parent = A;
@@ -363,7 +352,7 @@ export class AVLTree<
363
352
  protected _balanceRR(A: NODE): void {
364
353
  const parentOfA = A.parent;
365
354
  const B = A.right;
366
- A.parent = B;
355
+ if (B !== null) A.parent = B;
367
356
  if (B) {
368
357
  if (B.left) {
369
358
  B.left.parent = A;
@@ -406,15 +395,15 @@ export class AVLTree<
406
395
  C = B.left;
407
396
  }
408
397
 
409
- A.parent = C;
410
- if (B) B.parent = C;
398
+ if (C !== null) A.parent = C;
399
+ if (B && C !== null) B.parent = C;
411
400
 
412
401
  if (C) {
413
402
  if (C.left) {
414
403
  C.left.parent = A;
415
404
  }
416
405
  if (C.right) {
417
- C.right.parent = B;
406
+ if (B !== null) C.right.parent = B;
418
407
  }
419
408
  C.parent = parentOfA;
420
409
  }
@@ -8,6 +8,7 @@
8
8
 
9
9
  import {
10
10
  BinaryTreeDeleteResult,
11
+ BinaryTreeNested,
11
12
  BinaryTreeNodeNested,
12
13
  BinaryTreeOptions,
13
14
  BinaryTreePrintOptions,
@@ -22,6 +23,7 @@ import {
22
23
  NodeDisplayLayout,
23
24
  NodePredicate,
24
25
  OptNodeOrNull,
26
+ type RBTNColor,
25
27
  ToEntryFn
26
28
  } from '../../types';
27
29
  import { IBinaryTree } from '../../interfaces';
@@ -51,7 +53,7 @@ export class BinaryTreeNode<
51
53
  this.value = value;
52
54
  }
53
55
 
54
- protected _left?: OptNodeOrNull<NODE>;
56
+ _left?: OptNodeOrNull<NODE>;
55
57
 
56
58
  get left(): OptNodeOrNull<NODE> {
57
59
  return this._left;
@@ -64,7 +66,7 @@ export class BinaryTreeNode<
64
66
  this._left = v;
65
67
  }
66
68
 
67
- protected _right?: OptNodeOrNull<NODE>;
69
+ _right?: OptNodeOrNull<NODE>;
68
70
 
69
71
  get right(): OptNodeOrNull<NODE> {
70
72
  return this._right;
@@ -77,6 +79,36 @@ export class BinaryTreeNode<
77
79
  this._right = v;
78
80
  }
79
81
 
82
+ _height: number = 0;
83
+
84
+ get height(): number {
85
+ return this._height;
86
+ }
87
+
88
+ set height(value: number) {
89
+ this._height = value;
90
+ }
91
+
92
+ _color: RBTNColor = 'BLACK';
93
+
94
+ get color(): RBTNColor {
95
+ return this._color;
96
+ }
97
+
98
+ set color(value: RBTNColor) {
99
+ this._color = value;
100
+ }
101
+
102
+ _count: number = 1;
103
+
104
+ get count(): number {
105
+ return this._count;
106
+ }
107
+
108
+ set count(value: number) {
109
+ this._count = value;
110
+ }
111
+
80
112
  get familyPosition(): FamilyPosition {
81
113
  const that = this as unknown as NODE;
82
114
  if (!this.parent) {
@@ -104,10 +136,23 @@ export class BinaryTree<
104
136
  K = any,
105
137
  V = any,
106
138
  R = object,
107
- NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>
139
+ MK = any,
140
+ MV = any,
141
+ MR = object,
142
+ NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
143
+ TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<
144
+ K,
145
+ V,
146
+ R,
147
+ MK,
148
+ MV,
149
+ MR,
150
+ NODE,
151
+ BinaryTreeNested<K, V, R, MK, MV, MR, NODE>
152
+ >
108
153
  >
109
154
  extends IterableEntryBase<K, V | undefined>
110
- implements IBinaryTree<K, V, R, NODE>
155
+ implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
111
156
  {
112
157
  iterationType: IterationType = 'ITERATIVE';
113
158
 
@@ -186,26 +231,63 @@ export class BinaryTree<
186
231
  }
187
232
 
188
233
  /**
189
- * Time Complexity: O(1)
190
- * Space Complexity: O(1)
191
- *
192
- * The `createTree` function creates a new binary tree based on the provided options.
193
- * @param [options] - The `options` parameter in the `createTree` method is of type
194
- * `BinaryTreeOptions<K, V, R>`. This type likely contains configuration options for creating a
195
- * binary tree, such as the iteration type, whether the tree is in map mode, and functions for
196
- * converting entries.
197
- * @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
198
- * provided options. The method is creating a new `BinaryTree` object with an empty array as the
199
- * initial data, and then setting various options such as `iterationType`, `isMapMode`, and
200
- * `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
234
+ * The function creates a binary tree with the specified options.
235
+ * @param [options] - The `options` parameter in the `createTree` function is an optional parameter
236
+ * that allows you to provide partial configuration options for creating a binary tree. It is of type
237
+ * `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
238
+ * of properties
239
+ * @returns A new instance of a binary tree with the specified options is being returned.
201
240
  */
202
- createTree(options?: BinaryTreeOptions<K, V, R>): typeof this {
203
- return new BinaryTree<K, V, R>([], {
241
+ createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
242
+ return new BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
204
243
  iterationType: this.iterationType,
205
244
  isMapMode: this._isMapMode,
206
245
  toEntryFn: this._toEntryFn,
207
246
  ...options
208
- }) as unknown as typeof this;
247
+ }) as TREE;
248
+ }
249
+
250
+ /**
251
+ * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
252
+ * or returns null.
253
+ * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
254
+ * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
255
+ * can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
256
+ * node, an entry
257
+ * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
258
+ * an optional parameter of type `V`. It represents the value associated with the key in the node
259
+ * being created. If a `value` is provided, it will be used when creating the node. If
260
+ * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
261
+ * (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
262
+ * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
263
+ * value.
264
+ */
265
+ protected _keyValueNodeEntryRawToNodeAndValue(
266
+ keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
267
+ value?: V
268
+ ): [OptNodeOrNull<NODE>, V | undefined] {
269
+ if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
270
+ if (keyNodeEntryOrRaw === null) return [null, undefined];
271
+
272
+ if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
273
+
274
+ if (this.isEntry(keyNodeEntryOrRaw)) {
275
+ const [key, entryValue] = keyNodeEntryOrRaw;
276
+ if (key === undefined) return [undefined, undefined];
277
+ else if (key === null) return [null, undefined];
278
+ const finalValue = value ?? entryValue;
279
+ return [this.createNode(key, finalValue), finalValue];
280
+ }
281
+
282
+ if (this.isRaw(keyNodeEntryOrRaw)) {
283
+ const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
284
+ const finalValue = value ?? entryValue;
285
+ if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
286
+ }
287
+
288
+ if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
289
+
290
+ return [undefined, undefined];
209
291
  }
210
292
 
211
293
  /**
@@ -490,7 +572,7 @@ export class BinaryTree<
490
572
  * elements from the other tree.
491
573
  * @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
492
574
  */
493
- merge(anotherTree: this) {
575
+ merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>) {
494
576
  this.addMany(anotherTree, []);
495
577
  }
496
578
 
@@ -1599,7 +1681,7 @@ export class BinaryTree<
1599
1681
  * original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
1600
1682
  * the original tree is null, a null node is added to the cloned tree. If a node
1601
1683
  */
1602
- clone() {
1684
+ clone(): TREE {
1603
1685
  const cloned = this.createTree();
1604
1686
  this.bfs(
1605
1687
  node => {
@@ -1663,11 +1745,11 @@ export class BinaryTree<
1663
1745
  * @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
1664
1746
  * the result of applying the provided `callback` function to each entry in the original tree.
1665
1747
  */
1666
- map<MK, MV, MR>(
1748
+ map(
1667
1749
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
1668
1750
  options?: BinaryTreeOptions<MK, MV, MR>,
1669
1751
  thisArg?: any
1670
- ) {
1752
+ ): BinaryTree<MK, MV, MR> {
1671
1753
  const newTree = new BinaryTree<MK, MV, MR>([], options);
1672
1754
  let index = 0;
1673
1755
  for (const [key, value] of this) {
@@ -1736,49 +1818,6 @@ export class BinaryTree<
1736
1818
  console.log(this.toVisual(startNode, options));
1737
1819
  }
1738
1820
 
1739
- /**
1740
- * The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
1741
- * or returns null.
1742
- * @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
1743
- * `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
1744
- * can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
1745
- * node, an entry
1746
- * @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
1747
- * an optional parameter of type `V`. It represents the value associated with the key in the node
1748
- * being created. If a `value` is provided, it will be used when creating the node. If
1749
- * @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
1750
- * (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
1751
- * input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
1752
- * value.
1753
- */
1754
- protected _keyValueNodeEntryRawToNodeAndValue(
1755
- keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
1756
- value?: V
1757
- ): [OptNodeOrNull<NODE>, V | undefined] {
1758
- if (keyNodeEntryOrRaw === undefined) return [undefined, undefined];
1759
- if (keyNodeEntryOrRaw === null) return [null, undefined];
1760
-
1761
- if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
1762
-
1763
- if (this.isEntry(keyNodeEntryOrRaw)) {
1764
- const [key, entryValue] = keyNodeEntryOrRaw;
1765
- if (key === undefined) return [undefined, undefined];
1766
- else if (key === null) return [null, undefined];
1767
- const finalValue = value ?? entryValue;
1768
- return [this.createNode(key, finalValue), finalValue];
1769
- }
1770
-
1771
- if (this.isRaw(keyNodeEntryOrRaw)) {
1772
- const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
1773
- const finalValue = value ?? entryValue;
1774
- if (this.isKey(key)) return [this.createNode(key, finalValue), finalValue];
1775
- }
1776
-
1777
- if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value), value];
1778
-
1779
- return [undefined, undefined];
1780
- }
1781
-
1782
1821
  /**
1783
1822
  * Time complexity: O(n)
1784
1823
  * Space complexity: O(n)