binary-tree-typed 2.5.1 → 2.5.3

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 (75) hide show
  1. package/dist/cjs/index.cjs +340 -107
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +339 -106
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +340 -108
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +339 -107
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/common/error.d.ts +9 -0
  10. package/dist/types/common/index.d.ts +1 -1
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +86 -2
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +98 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +189 -13
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +270 -3
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +136 -8
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +42 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1089 -161
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1243 -350
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +980 -255
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1174 -284
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +70 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +63 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +84 -6
  24. package/dist/types/data-structures/heap/heap.d.ts +140 -12
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +126 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +106 -1
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +126 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +56 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +127 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +97 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +72 -2
  32. package/dist/types/data-structures/trie/trie.d.ts +84 -0
  33. package/dist/types/interfaces/binary-tree.d.ts +2 -3
  34. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  37. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  38. package/dist/umd/binary-tree-typed.js +337 -105
  39. package/dist/umd/binary-tree-typed.js.map +1 -1
  40. package/dist/umd/binary-tree-typed.min.js +5 -5
  41. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  42. package/package.json +2 -2
  43. package/src/common/error.ts +19 -1
  44. package/src/common/index.ts +1 -1
  45. package/src/data-structures/base/iterable-element-base.ts +3 -2
  46. package/src/data-structures/binary-tree/avl-tree.ts +99 -5
  47. package/src/data-structures/binary-tree/binary-indexed-tree.ts +102 -4
  48. package/src/data-structures/binary-tree/binary-tree.ts +239 -78
  49. package/src/data-structures/binary-tree/bst.ts +542 -13
  50. package/src/data-structures/binary-tree/red-black-tree.ts +155 -15
  51. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  52. package/src/data-structures/binary-tree/tree-map.ts +1223 -261
  53. package/src/data-structures/binary-tree/tree-multi-map.ts +939 -30
  54. package/src/data-structures/binary-tree/tree-multi-set.ts +746 -10
  55. package/src/data-structures/binary-tree/tree-set.ts +1018 -99
  56. package/src/data-structures/graph/abstract-graph.ts +2 -2
  57. package/src/data-structures/graph/directed-graph.ts +71 -1
  58. package/src/data-structures/graph/undirected-graph.ts +64 -1
  59. package/src/data-structures/hash/hash-map.ts +102 -16
  60. package/src/data-structures/heap/heap.ts +153 -23
  61. package/src/data-structures/heap/max-heap.ts +2 -2
  62. package/src/data-structures/linked-list/doubly-linked-list.ts +139 -0
  63. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  64. package/src/data-structures/linked-list/skip-linked-list.ts +131 -5
  65. package/src/data-structures/matrix/matrix.ts +65 -9
  66. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  67. package/src/data-structures/queue/deque.ts +130 -0
  68. package/src/data-structures/queue/queue.ts +109 -0
  69. package/src/data-structures/stack/stack.ts +75 -5
  70. package/src/data-structures/trie/trie.ts +86 -2
  71. package/src/interfaces/binary-tree.ts +1 -9
  72. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  73. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  74. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  75. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binary-tree-typed",
3
- "version": "2.5.1",
3
+ "version": "2.5.3",
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.5.1"
181
+ "data-structure-typed": "^2.5.3"
182
182
  }
183
183
  }
@@ -1,3 +1,17 @@
1
+ /**
2
+ * Centralized error dispatch.
3
+ * All library errors go through this function for consistent messaging and easy grep.
4
+ * @remarks Always throws — data structure errors are never recoverable.
5
+ * @param ErrorClass - The error constructor (Error, TypeError, RangeError, etc.)
6
+ * @param message - The error message.
7
+ */
8
+ export function raise(
9
+ ErrorClass: new (msg: string) => Error,
10
+ message: string
11
+ ): never {
12
+ throw new ErrorClass(message);
13
+ }
14
+
1
15
  /**
2
16
  * Centralized error message templates.
3
17
  * Keep using native Error/TypeError/RangeError — this only standardizes messages.
@@ -56,5 +70,9 @@ export const ERR = {
56
70
  'Matrix: Must be rectangular for transposition.',
57
71
 
58
72
  matrixRowMismatch: (expected: number, got: number) =>
59
- `Matrix: Expected row length ${expected}, but got ${got}.`
73
+ `Matrix: Expected row length ${expected}, but got ${got}.`,
74
+
75
+ // Order statistic
76
+ orderStatisticNotEnabled: (method: string, ctx?: string) =>
77
+ `${ctx ? ctx + ': ' : ''}${method}() requires enableOrderStatistic: true.`
60
78
  } as const;
@@ -1,4 +1,4 @@
1
- export { ERR } from './error';
1
+ export { ERR, raise } from './error';
2
2
 
3
3
  export enum DFSOperation {
4
4
  VISIT = 0,
@@ -1,4 +1,5 @@
1
1
  import type { ElementCallback, IterableElementBaseOptions, ReduceElementCallback } from '../../types';
2
+ import { raise } from '../../common';
2
3
 
3
4
  /**
4
5
  * Base class that makes a data structure iterable and provides common
@@ -25,7 +26,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
25
26
  if (options) {
26
27
  const { toElementFn } = options;
27
28
  if (typeof toElementFn === 'function') this._toElementFn = toElementFn;
28
- else if (toElementFn) throw new TypeError('toElementFn must be a function type');
29
+ else if (toElementFn) raise(TypeError, 'toElementFn must be a function type');
29
30
  }
30
31
  }
31
32
 
@@ -224,7 +225,7 @@ export abstract class IterableElementBase<E, R> implements Iterable<E> {
224
225
  acc = initialValue as U;
225
226
  } else {
226
227
  const first = iter.next();
227
- if (first.done) throw new TypeError('Reduce of empty structure with no initial value');
228
+ if (first.done) raise(TypeError, 'Reduce of empty structure with no initial value');
228
229
  acc = first.value as unknown as U;
229
230
  index = 1;
230
231
  }
@@ -9,7 +9,6 @@
9
9
  import { BST } from './bst';
10
10
  import type {
11
11
  AVLTreeOptions,
12
- BinaryTreeDeleteResult,
13
12
  BinaryTreeOptions,
14
13
  BSTNOptKeyOrNode,
15
14
  EntryCallback,
@@ -459,6 +458,34 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
459
458
 
460
459
 
461
460
 
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+
484
+
485
+
486
+
487
+
488
+
462
489
 
463
490
 
464
491
 
@@ -579,6 +606,27 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
579
606
 
580
607
 
581
608
 
609
+
610
+
611
+
612
+
613
+
614
+
615
+
616
+
617
+
618
+
619
+
620
+
621
+
622
+
623
+
624
+
625
+
626
+
627
+
628
+
629
+
582
630
 
583
631
 
584
632
 
@@ -609,15 +657,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
609
657
  */
610
658
  override delete(
611
659
  keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
612
- ): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
613
- const deletedResults = super.delete(keyNodeOrEntry);
660
+ ): boolean {
661
+ const deletedResults = this._deleteInternal(keyNodeOrEntry);
614
662
  // After deletion, balance the path from the parent of the *physically deleted* node.
615
663
  for (const { needBalanced } of deletedResults) {
616
664
  if (needBalanced) {
617
- this._balancePath(needBalanced);
665
+ this._balancePath(needBalanced as AVLTreeNode<K, V>);
618
666
  }
619
667
  }
620
- return deletedResults;
668
+ return deletedResults.length > 0;
621
669
  }
622
670
 
623
671
  /**
@@ -666,6 +714,20 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
666
714
 
667
715
 
668
716
 
717
+
718
+
719
+
720
+
721
+
722
+
723
+
724
+
725
+
726
+
727
+
728
+
729
+
730
+
669
731
 
670
732
 
671
733
 
@@ -794,6 +856,27 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
794
856
 
795
857
 
796
858
 
859
+
860
+
861
+
862
+
863
+
864
+
865
+
866
+
867
+
868
+
869
+
870
+
871
+
872
+
873
+
874
+
875
+
876
+
877
+
878
+
879
+
797
880
 
798
881
 
799
882
 
@@ -970,6 +1053,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
970
1053
  }
971
1054
  this._updateHeight(A);
972
1055
  if (B) this._updateHeight(B);
1056
+ this._updateCount(A);
1057
+ if (B) this._updateCount(B);
973
1058
  }
974
1059
 
975
1060
  /**
@@ -1022,6 +1107,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1022
1107
  this._updateHeight(A);
1023
1108
  if (B) this._updateHeight(B);
1024
1109
  if (C) this._updateHeight(C);
1110
+ this._updateCount(A);
1111
+ if (B) this._updateCount(B);
1112
+ if (C) this._updateCount(C);
1025
1113
  }
1026
1114
 
1027
1115
  /**
@@ -1061,6 +1149,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1061
1149
  }
1062
1150
  this._updateHeight(A);
1063
1151
  if (B) this._updateHeight(B);
1152
+ this._updateCount(A);
1153
+ if (B) this._updateCount(B);
1064
1154
  }
1065
1155
 
1066
1156
  /**
@@ -1112,6 +1202,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1112
1202
  this._updateHeight(A);
1113
1203
  if (B) this._updateHeight(B);
1114
1204
  if (C) this._updateHeight(C);
1205
+ this._updateCount(A);
1206
+ if (B) this._updateCount(B);
1207
+ if (C) this._updateCount(C);
1115
1208
  }
1116
1209
 
1117
1210
  /**
@@ -1130,6 +1223,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1130
1223
  const A = path[i];
1131
1224
  if (A) {
1132
1225
  this._updateHeight(A);
1226
+ this._updateCount(A);
1133
1227
 
1134
1228
  // Check the balance factor
1135
1229
  switch (this._balanceFactor(A)) {
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { ERR } from '../../common';
8
+ import { ERR, raise } from '../../common';
9
9
 
10
10
  /**
11
11
  * Binary Indexed Tree (Fenwick Tree).
@@ -44,7 +44,7 @@ export class BinaryIndexedTree implements Iterable<number> {
44
44
  }
45
45
  } else {
46
46
  if (!Number.isInteger(sizeOrElements) || sizeOrElements < 0) {
47
- throw new RangeError(ERR.invalidArgument('size must be a non-negative integer', 'BinaryIndexedTree'));
47
+ raise(RangeError, ERR.invalidArgument('size must be a non-negative integer', 'BinaryIndexedTree'));
48
48
  }
49
49
  this._size = sizeOrElements;
50
50
  this._tree = new Array(this._size + 1).fill(0);
@@ -97,6 +97,20 @@ export class BinaryIndexedTree implements Iterable<number> {
97
97
 
98
98
 
99
99
 
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
100
114
 
101
115
 
102
116
 
@@ -168,6 +182,20 @@ export class BinaryIndexedTree implements Iterable<number> {
168
182
 
169
183
 
170
184
 
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+
198
+
171
199
 
172
200
 
173
201
 
@@ -239,6 +267,20 @@ export class BinaryIndexedTree implements Iterable<number> {
239
267
 
240
268
 
241
269
 
270
+
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
242
284
 
243
285
 
244
286
 
@@ -310,6 +352,20 @@ export class BinaryIndexedTree implements Iterable<number> {
310
352
 
311
353
 
312
354
 
355
+
356
+
357
+
358
+
359
+
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
313
369
 
314
370
 
315
371
 
@@ -379,6 +435,20 @@ export class BinaryIndexedTree implements Iterable<number> {
379
435
 
380
436
 
381
437
 
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
382
452
 
383
453
 
384
454
 
@@ -456,6 +526,20 @@ export class BinaryIndexedTree implements Iterable<number> {
456
526
 
457
527
 
458
528
 
529
+
530
+
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
459
543
 
460
544
 
461
545
 
@@ -513,6 +597,13 @@ export class BinaryIndexedTree implements Iterable<number> {
513
597
 
514
598
 
515
599
 
600
+
601
+
602
+
603
+
604
+
605
+
606
+
516
607
 
517
608
 
518
609
 
@@ -579,6 +670,13 @@ export class BinaryIndexedTree implements Iterable<number> {
579
670
 
580
671
 
581
672
 
673
+
674
+
675
+
676
+
677
+
678
+
679
+
582
680
 
583
681
 
584
682
 
@@ -665,10 +763,10 @@ export class BinaryIndexedTree implements Iterable<number> {
665
763
 
666
764
  protected _checkIndex(index: number): void {
667
765
  if (!Number.isInteger(index)) {
668
- throw new TypeError(ERR.invalidIndex('BinaryIndexedTree'));
766
+ raise(TypeError, ERR.invalidIndex('BinaryIndexedTree'));
669
767
  }
670
768
  if (index < 0 || index >= this._size) {
671
- throw new RangeError(ERR.indexOutOfRange(index, 0, this._size - 1, 'BinaryIndexedTree'));
769
+ raise(RangeError, ERR.indexOutOfRange(index, 0, this._size - 1, 'BinaryIndexedTree'));
672
770
  }
673
771
  }
674
772