data-structure-typed 2.4.4 → 2.5.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 (80) hide show
  1. package/CHANGELOG.md +22 -1
  2. package/README.md +34 -1
  3. package/dist/cjs/index.cjs +10639 -2151
  4. package/dist/cjs-legacy/index.cjs +10694 -2195
  5. package/dist/esm/index.mjs +10639 -2150
  6. package/dist/esm-legacy/index.mjs +10694 -2194
  7. package/dist/types/common/error.d.ts +23 -0
  8. package/dist/types/common/index.d.ts +1 -0
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  10. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  11. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  15. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  16. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  17. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  18. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  19. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  20. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  22. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  23. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  24. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  25. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  26. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  27. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  28. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  29. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  30. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  31. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  32. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  33. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  34. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  35. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  36. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  37. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  38. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  39. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  40. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  41. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  42. package/dist/umd/data-structure-typed.js +10725 -2221
  43. package/dist/umd/data-structure-typed.min.js +4 -2
  44. package/package.json +5 -4
  45. package/src/common/error.ts +60 -0
  46. package/src/common/index.ts +2 -0
  47. package/src/data-structures/base/iterable-element-base.ts +2 -2
  48. package/src/data-structures/binary-tree/avl-tree.ts +146 -51
  49. package/src/data-structures/binary-tree/binary-indexed-tree.ts +317 -247
  50. package/src/data-structures/binary-tree/binary-tree.ts +567 -121
  51. package/src/data-structures/binary-tree/bst.ts +370 -37
  52. package/src/data-structures/binary-tree/red-black-tree.ts +328 -96
  53. package/src/data-structures/binary-tree/segment-tree.ts +378 -248
  54. package/src/data-structures/binary-tree/tree-map.ts +1411 -13
  55. package/src/data-structures/binary-tree/tree-multi-map.ts +1218 -215
  56. package/src/data-structures/binary-tree/tree-multi-set.ts +959 -69
  57. package/src/data-structures/binary-tree/tree-set.ts +1257 -15
  58. package/src/data-structures/graph/abstract-graph.ts +106 -1
  59. package/src/data-structures/graph/directed-graph.ts +233 -47
  60. package/src/data-structures/graph/map-graph.ts +59 -1
  61. package/src/data-structures/graph/undirected-graph.ts +308 -59
  62. package/src/data-structures/hash/hash-map.ts +254 -79
  63. package/src/data-structures/heap/heap.ts +305 -102
  64. package/src/data-structures/heap/max-heap.ts +48 -3
  65. package/src/data-structures/heap/min-heap.ts +59 -0
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
  67. package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
  68. package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
  69. package/src/data-structures/matrix/matrix.ts +433 -22
  70. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  71. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  72. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  73. package/src/data-structures/queue/deque.ts +358 -68
  74. package/src/data-structures/queue/queue.ts +223 -42
  75. package/src/data-structures/stack/stack.ts +184 -32
  76. package/src/data-structures/trie/trie.ts +227 -44
  77. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  78. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  79. package/src/types/data-structures/queue/deque.ts +7 -0
  80. package/src/utils/utils.ts +4 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "2.4.4",
3
+ "version": "2.5.0",
4
4
  "description": "Standard data structure",
5
5
  "browser": "dist/umd/data-structure-typed.min.js",
6
6
  "umd:main": "dist/umd/data-structure-typed.min.js",
@@ -44,8 +44,9 @@
44
44
  "build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
45
45
  "build:ecut": "npm run build:node && npm run build:types && npm run build:umd",
46
46
  "build:leetcode": "tsup --config tsup.leetcode.config.js",
47
- "build:docs": "npm run gen:examples && npm run generate:schema && typedoc --out docs/api ./src",
48
- "build:docs-class": "npm run gen:examples && typedoc --out docs/api ./src/data-structures",
47
+ "build:typedoc-plugin": "tsc scripts/typedoc-plugin-example-rewrite.ts --outDir scripts --esModuleInterop --module commonjs --target es2020 --moduleResolution node --skipLibCheck",
48
+ "build:docs": "npm run gen:examples && npm run generate:schema && npm run build:typedoc-plugin && typedoc --plugin ./scripts/typedoc-plugin-example-rewrite.js --out docs/api ./src",
49
+ "build:docs-class": "npm run gen:examples && npm run build:typedoc-plugin && typedoc --plugin ./scripts/typedoc-plugin-example-rewrite.js --out docs/api ./src/data-structures",
49
50
  "gen:examples": "ts-node scripts/test-to-example.ts",
50
51
  "test:in-band": "jest --runInBand",
51
52
  "test": "npm run test:in-band",
@@ -105,7 +106,7 @@
105
106
  "benchmark": "^2.1.4",
106
107
  "binary-tree-typed": "^1.54.3",
107
108
  "bst-typed": "^1.54.3",
108
- "data-structure-typed": "^2.4.3",
109
+ "data-structure-typed": "^2.4.5",
109
110
  "dependency-cruiser": "^16.5.0",
110
111
  "doctoc": "^2.2.1",
111
112
  "eslint": "^9.13.0",
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Centralized error message templates.
3
+ * Keep using native Error/TypeError/RangeError — this only standardizes messages.
4
+ */
5
+ export const ERR = {
6
+ // Range / index
7
+ indexOutOfRange: (index: number, min: number, max: number, ctx?: string) =>
8
+ `${ctx ? ctx + ': ' : ''}Index ${index} is out of range [${min}, ${max}].`,
9
+
10
+ invalidIndex: (ctx?: string) =>
11
+ `${ctx ? ctx + ': ' : ''}Index must be an integer.`,
12
+
13
+ // Type / argument
14
+ invalidArgument: (reason: string, ctx?: string) =>
15
+ `${ctx ? ctx + ': ' : ''}${reason}`,
16
+
17
+ comparatorRequired: (ctx?: string) =>
18
+ `${ctx ? ctx + ': ' : ''}Comparator is required for non-number/non-string/non-Date keys.`,
19
+
20
+ invalidKey: (reason: string, ctx?: string) =>
21
+ `${ctx ? ctx + ': ' : ''}${reason}`,
22
+
23
+ notAFunction: (name: string, ctx?: string) =>
24
+ `${ctx ? ctx + ': ' : ''}${name} must be a function.`,
25
+
26
+ invalidEntry: (ctx?: string) =>
27
+ `${ctx ? ctx + ': ' : ''}Each entry must be a [key, value] tuple.`,
28
+
29
+ invalidNaN: (ctx?: string) =>
30
+ `${ctx ? ctx + ': ' : ''}NaN is not a valid key.`,
31
+
32
+ invalidDate: (ctx?: string) =>
33
+ `${ctx ? ctx + ': ' : ''}Invalid Date key.`,
34
+
35
+ reduceEmpty: (ctx?: string) =>
36
+ `${ctx ? ctx + ': ' : ''}Reduce of empty structure with no initial value.`,
37
+
38
+ callbackReturnType: (expected: string, got: string, ctx?: string) =>
39
+ `${ctx ? ctx + ': ' : ''}Callback must return ${expected}; got ${got}.`,
40
+
41
+ // State / operation
42
+ invalidOperation: (reason: string, ctx?: string) =>
43
+ `${ctx ? ctx + ': ' : ''}${reason}`,
44
+
45
+ // Matrix
46
+ matrixDimensionMismatch: (op: string) =>
47
+ `Matrix: Dimensions must be compatible for ${op}.`,
48
+
49
+ matrixSingular: () =>
50
+ 'Matrix: Singular matrix, inverse does not exist.',
51
+
52
+ matrixNotSquare: () =>
53
+ 'Matrix: Must be square for inversion.',
54
+
55
+ matrixNotRectangular: () =>
56
+ 'Matrix: Must be rectangular for transposition.',
57
+
58
+ matrixRowMismatch: (expected: number, got: number) =>
59
+ `Matrix: Expected row length ${expected}, but got ${got}.`
60
+ } as const;
@@ -1,3 +1,5 @@
1
+ export { ERR } from './error';
2
+
1
3
  export enum DFSOperation {
2
4
  VISIT = 0,
3
5
  PROCESS = 1
@@ -35,7 +35,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
35
35
  * @remarks
36
36
  * Time O(1), Space O(1).
37
37
  */
38
- protected readonly _toElementFn?: (rawElement: R) => E;
38
+ protected _toElementFn?: (rawElement: R) => E;
39
39
 
40
40
  /**
41
41
  * Exposes the current `toElementFn`, if configured.
@@ -229,7 +229,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
229
229
  index = 1;
230
230
  }
231
231
 
232
- for (const value of iter) {
232
+ for (const value of iter as unknown as Iterable<E>) {
233
233
  acc = callbackfn(acc, value, index++, this);
234
234
  }
235
235
  return acc;
@@ -123,16 +123,12 @@ export class AVLTreeNode<K = any, V = any> {
123
123
  *
124
124
  * @returns The node's color.
125
125
  */
126
+ /* istanbul ignore next -- inherited field, used by RedBlackTree subclass */ /* istanbul ignore next -- inherited field, not used by AVLTree */
126
127
  get color(): RBTNColor {
127
128
  return this._color;
128
129
  }
129
130
 
130
- /**
131
- * Sets the color of the node.
132
- * @remarks Time O(1), Space O(1)
133
- *
134
- * @param value - The new color.
135
- */
131
+ /* istanbul ignore next -- inherited field, not used by AVLTree */
136
132
  set color(value: RBTNColor) {
137
133
  this._color = value;
138
134
  }
@@ -145,16 +141,12 @@ export class AVLTreeNode<K = any, V = any> {
145
141
  *
146
142
  * @returns The subtree node count.
147
143
  */
144
+ /* istanbul ignore next -- inherited field, not used by AVLTree */
148
145
  get count(): number {
149
146
  return this._count;
150
147
  }
151
148
 
152
- /**
153
- * Sets the count of nodes in the subtree.
154
- * @remarks Time O(1), Space O(1)
155
- *
156
- * @param value - The new count.
157
- */
149
+ /* istanbul ignore next -- inherited field, not used by AVLTree */
158
150
  set count(value: number) {
159
151
  this._count = value;
160
152
  }
@@ -176,6 +168,7 @@ export class AVLTreeNode<K = any, V = any> {
176
168
  return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
177
169
  }
178
170
 
171
+ /* istanbul ignore next -- defensive: unreachable if tree structure is correct */
179
172
  return 'MAL_NODE';
180
173
  }
181
174
  }
@@ -197,28 +190,6 @@ export class AVLTreeNode<K = any, V = any> {
197
190
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
198
191
  *
199
192
  * @example
200
- * // basic AVLTree creation and add operation
201
- * // Create a simple AVLTree with initial values
202
- * const tree = new AVLTree([5, 2, 8, 1, 9]);
203
- *
204
- * tree.print();
205
- * // _2___
206
- * // / \
207
- * // 1 _8_
208
- * // / \
209
- * // 5 9
210
- *
211
- * // Verify the tree maintains sorted order
212
- * console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
213
- *
214
- * // Check size
215
- * console.log(tree.size); // 5;
216
- *
217
- * // Add a new element
218
- * tree.set(3);
219
- * console.log(tree.size); // 6;
220
- * console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
221
- * @example
222
193
  * // AVLTree has and get operations
223
194
  * const tree = new AVLTree<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
224
195
  *
@@ -233,23 +204,6 @@ export class AVLTreeNode<K = any, V = any> {
233
204
  * // Verify tree is balanced
234
205
  * console.log(tree.isAVLBalanced()); // true;
235
206
  * @example
236
- * // AVLTree delete and balance verification
237
- * const tree = new AVLTree([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
238
- *
239
- * // Delete an element
240
- * tree.delete(10);
241
- * console.log(tree.has(10)); // false;
242
- *
243
- * // Tree should remain balanced after deletion
244
- * console.log(tree.isAVLBalanced()); // true;
245
- *
246
- * // Size decreased
247
- * console.log(tree.size); // 15;
248
- *
249
- * // Remaining elements are still sorted
250
- * const keys = [...tree.keys()];
251
- * console.log(keys); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16];
252
- * @example
253
207
  * // AVLTree for university ranking system with strict balance
254
208
  * interface University {
255
209
  * name: string;
@@ -406,6 +360,52 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
406
360
  * @param keyNodeOrEntry - The key, node, or entry to set.
407
361
  * @param [value] - The value, if providing just a key.
408
362
  * @returns True if the addition was successful, false otherwise.
363
+
364
+
365
+
366
+
367
+
368
+
369
+
370
+
371
+
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+ * @example
404
+ * // Set a key-value pair
405
+ * const avl = new AVLTree<number, string>();
406
+ * avl.set(1, 'one');
407
+ * avl.set(2, 'two');
408
+ * console.log(avl.get(1)); // 'one';
409
409
  */
410
410
  override set(
411
411
  keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
@@ -424,6 +424,48 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
424
424
  *
425
425
  * @param keyNodeOrEntry - The node to delete.
426
426
  * @returns An array containing deletion results.
427
+
428
+
429
+
430
+
431
+
432
+
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+ * @example
464
+ * // Remove nodes and verify structure
465
+ * const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
466
+ * avl.delete(3);
467
+ * console.log(avl.has(3)); // false;
468
+ * console.log(avl.size); // 6;
427
469
  */
428
470
  override delete(
429
471
  keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
@@ -445,6 +487,28 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
445
487
  *
446
488
  * @param [iterationType=this.iterationType] - The traversal method for the initial node export.
447
489
  * @returns True if successful, false if the tree was empty.
490
+
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+
502
+
503
+
504
+ * @example
505
+ * // Rebalance the tree
506
+ * const avl = new AVLTree<number>();
507
+ * // Insert in sorted order (worst case for BST)
508
+ * for (let i = 1; i <= 7; i++) avl.add(i);
509
+ * console.log(avl.isAVLBalanced()); // false;
510
+ * avl.perfectlyBalance();
511
+ * console.log(avl.isAVLBalanced()); // true;
448
512
  */
449
513
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
450
514
  const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
@@ -486,6 +550,36 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
486
550
  * @param [options] - Options for the new AVLTree.
487
551
  * @param [thisArg] - `this` context for the callback.
488
552
  * @returns A new, mapped AVLTree.
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+ * @example
579
+ * // Transform to new tree
580
+ * const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
581
+ * const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
582
+ * console.log([...doubled.values()]); // [20, 40, 60];
489
583
  */
490
584
  override map<MK = K, MV = V, MR = any>(
491
585
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
@@ -574,6 +668,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
574
668
 
575
669
  return destNodeEnsured;
576
670
  }
671
+ /* istanbul ignore next -- defensive: srcNode/destNode are always valid when called internally */
577
672
  return undefined;
578
673
  }
579
674