binary-tree-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 (85) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +965 -420
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +962 -417
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +965 -421
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +962 -418
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/binary-tree-typed.js +959 -414
  46. package/dist/umd/binary-tree-typed.js.map +1 -1
  47. package/dist/umd/binary-tree-typed.min.js +3 -3
  48. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binary-tree-typed",
3
- "version": "2.4.4",
3
+ "version": "2.5.0",
4
4
  "description": "Binary Tree. Javascript & Typescript Data Structure.",
5
5
  "browser": "dist/umd/binary-tree-typed.min.js",
6
6
  "umd:main": "dist/umd/binary-tree-typed.min.js",
@@ -178,6 +178,6 @@
178
178
  "typescript": "^4.9.5"
179
179
  },
180
180
  "dependencies": {
181
- "data-structure-typed": "^2.4.4"
181
+ "data-structure-typed": "^2.5.0"
182
182
  }
183
183
  }
@@ -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,48 @@ 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
+ * @example
400
+ * // Set a key-value pair
401
+ * const avl = new AVLTree<number, string>();
402
+ * avl.set(1, 'one');
403
+ * avl.set(2, 'two');
404
+ * console.log(avl.get(1)); // 'one';
409
405
  */
410
406
  override set(
411
407
  keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
@@ -424,6 +420,45 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
424
420
  *
425
421
  * @param keyNodeOrEntry - The node to delete.
426
422
  * @returns An array containing deletion results.
423
+
424
+
425
+
426
+
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
+ * @example
457
+ * // Remove nodes and verify structure
458
+ * const avl = new AVLTree<number>([5, 3, 7, 1, 4, 6, 8]);
459
+ * avl.delete(3);
460
+ * console.log(avl.has(3)); // false;
461
+ * console.log(avl.size); // 6;
427
462
  */
428
463
  override delete(
429
464
  keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
@@ -445,6 +480,26 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
445
480
  *
446
481
  * @param [iterationType=this.iterationType] - The traversal method for the initial node export.
447
482
  * @returns True if successful, false if the tree was empty.
483
+
484
+
485
+
486
+
487
+
488
+
489
+
490
+
491
+
492
+
493
+
494
+
495
+ * @example
496
+ * // Rebalance the tree
497
+ * const avl = new AVLTree<number>();
498
+ * // Insert in sorted order (worst case for BST)
499
+ * for (let i = 1; i <= 7; i++) avl.add(i);
500
+ * console.log(avl.isAVLBalanced()); // false;
501
+ * avl.perfectlyBalance();
502
+ * console.log(avl.isAVLBalanced()); // true;
448
503
  */
449
504
  override perfectlyBalance(iterationType: IterationType = this.iterationType): boolean {
450
505
  const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
@@ -486,6 +541,33 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
486
541
  * @param [options] - Options for the new AVLTree.
487
542
  * @param [thisArg] - `this` context for the callback.
488
543
  * @returns A new, mapped AVLTree.
544
+
545
+
546
+
547
+
548
+
549
+
550
+
551
+
552
+
553
+
554
+
555
+
556
+
557
+
558
+
559
+
560
+
561
+
562
+
563
+
564
+
565
+
566
+ * @example
567
+ * // Transform to new tree
568
+ * const avl = new AVLTree<number, number>([[1, 10], [2, 20], [3, 30]]);
569
+ * const doubled = avl.map((value, key) => [key, (value ?? 0) * 2] as [number, number]);
570
+ * console.log([...doubled.values()]); // [20, 40, 60];
489
571
  */
490
572
  override map<MK = K, MV = V, MR = any>(
491
573
  callback: EntryCallback<K, V | undefined, [MK, MV]>,
@@ -574,6 +656,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
574
656
 
575
657
  return destNodeEnsured;
576
658
  }
659
+ /* istanbul ignore next -- defensive: srcNode/destNode are always valid when called internally */
577
660
  return undefined;
578
661
  }
579
662