binary-tree-typed 2.5.0 → 2.5.2

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 (90) hide show
  1. package/dist/cjs/index.cjs +771 -68
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +771 -68
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +771 -69
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +771 -69
  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/base/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-entry-base.d.ts +8 -8
  13. package/dist/types/data-structures/base/linear-base.d.ts +3 -3
  14. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +288 -0
  15. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +336 -0
  16. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +618 -18
  17. package/dist/types/data-structures/binary-tree/bst.d.ts +676 -1
  18. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +456 -0
  19. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +144 -1
  20. package/dist/types/data-structures/binary-tree/tree-map.d.ts +3307 -399
  21. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3285 -360
  22. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +2674 -325
  23. package/dist/types/data-structures/binary-tree/tree-set.d.ts +3072 -287
  24. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -4
  25. package/dist/types/data-structures/graph/directed-graph.d.ts +240 -0
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +216 -0
  27. package/dist/types/data-structures/hash/hash-map.d.ts +274 -10
  28. package/dist/types/data-structures/heap/heap.d.ts +336 -0
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +411 -3
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +363 -3
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +434 -2
  32. package/dist/types/data-structures/matrix/matrix.d.ts +192 -0
  33. package/dist/types/data-structures/queue/deque.d.ts +364 -4
  34. package/dist/types/data-structures/queue/queue.d.ts +288 -0
  35. package/dist/types/data-structures/stack/stack.d.ts +240 -0
  36. package/dist/types/data-structures/trie/trie.d.ts +292 -4
  37. package/dist/types/interfaces/graph.d.ts +1 -1
  38. package/dist/types/types/common.d.ts +2 -2
  39. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  40. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  41. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  42. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  43. package/dist/types/types/data-structures/heap/heap.d.ts +1 -0
  44. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
  45. package/dist/types/types/utils/validate-type.d.ts +4 -4
  46. package/dist/umd/binary-tree-typed.js +773 -71
  47. package/dist/umd/binary-tree-typed.js.map +1 -1
  48. package/dist/umd/binary-tree-typed.min.js +5 -5
  49. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/common/error.ts +19 -1
  52. package/src/common/index.ts +1 -1
  53. package/src/data-structures/base/index.ts +1 -0
  54. package/src/data-structures/base/iterable-element-base.ts +3 -2
  55. package/src/data-structures/base/iterable-entry-base.ts +8 -8
  56. package/src/data-structures/base/linear-base.ts +3 -3
  57. package/src/data-structures/binary-tree/avl-tree.ts +299 -0
  58. package/src/data-structures/binary-tree/binary-indexed-tree.ts +341 -5
  59. package/src/data-structures/binary-tree/binary-tree.ts +606 -6
  60. package/src/data-structures/binary-tree/bst.ts +946 -7
  61. package/src/data-structures/binary-tree/red-black-tree.ts +472 -0
  62. package/src/data-structures/binary-tree/segment-tree.ts +145 -2
  63. package/src/data-structures/binary-tree/tree-map.ts +3423 -499
  64. package/src/data-structures/binary-tree/tree-multi-map.ts +3537 -596
  65. package/src/data-structures/binary-tree/tree-multi-set.ts +2855 -495
  66. package/src/data-structures/binary-tree/tree-set.ts +3209 -413
  67. package/src/data-structures/graph/abstract-graph.ts +6 -6
  68. package/src/data-structures/graph/directed-graph.ts +240 -0
  69. package/src/data-structures/graph/undirected-graph.ts +216 -0
  70. package/src/data-structures/hash/hash-map.ts +281 -19
  71. package/src/data-structures/heap/heap.ts +340 -4
  72. package/src/data-structures/heap/max-heap.ts +2 -2
  73. package/src/data-structures/linked-list/doubly-linked-list.ts +411 -3
  74. package/src/data-structures/linked-list/singly-linked-list.ts +363 -3
  75. package/src/data-structures/linked-list/skip-linked-list.ts +439 -7
  76. package/src/data-structures/matrix/matrix.ts +202 -10
  77. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  78. package/src/data-structures/queue/deque.ts +365 -5
  79. package/src/data-structures/queue/queue.ts +288 -0
  80. package/src/data-structures/stack/stack.ts +240 -0
  81. package/src/data-structures/trie/trie.ts +295 -7
  82. package/src/interfaces/graph.ts +1 -1
  83. package/src/types/common.ts +2 -2
  84. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  85. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  86. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  87. package/src/types/data-structures/binary-tree/tree-set.ts +5 -0
  88. package/src/types/data-structures/heap/heap.ts +1 -0
  89. package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
  90. package/src/types/utils/validate-type.ts +4 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binary-tree-typed",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
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.0"
181
+ "data-structure-typed": "^2.5.2"
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,2 +1,3 @@
1
1
  export * from './iterable-entry-base';
2
2
  export * from './iterable-element-base';
3
+ export * from './linear-base';
@@ -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
  }
@@ -19,7 +19,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
19
19
  * @returns Iterator of `[K, V]`.
20
20
  * @remarks Time O(n) to iterate, Space O(1)
21
21
  */
22
- *[Symbol.iterator](...args: any[]): IterableIterator<[K, V]> {
22
+ *[Symbol.iterator](...args: unknown[]): IterableIterator<[K, V]> {
23
23
  yield* this._getIterator(...args);
24
24
  }
25
25
 
@@ -63,7 +63,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
63
63
  * @returns `true` if all pass; otherwise `false`.
64
64
  * @remarks Time O(n), Space O(1)
65
65
  */
66
- every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
66
+ every(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): boolean {
67
67
  let index = 0;
68
68
  for (const item of this) {
69
69
  if (!predicate.call(thisArg, item[1], item[0], index++, this)) {
@@ -80,7 +80,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
80
80
  * @returns `true` if any passes; otherwise `false`.
81
81
  * @remarks Time O(n), Space O(1)
82
82
  */
83
- some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
83
+ some(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): boolean {
84
84
  let index = 0;
85
85
  for (const item of this) {
86
86
  if (predicate.call(thisArg, item[1], item[0], index++, this)) {
@@ -96,7 +96,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
96
96
  * @param thisArg - Optional `this` for callback.
97
97
  * @remarks Time O(n), Space O(1)
98
98
  */
99
- forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void {
99
+ forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: unknown): void {
100
100
  let index = 0;
101
101
  for (const item of this) {
102
102
  const [key, value] = item;
@@ -111,7 +111,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
111
111
  * @returns Matching `[key, value]` or `undefined`.
112
112
  * @remarks Time O(n), Space O(1)
113
113
  */
114
- find(callbackfn: EntryCallback<K, V, boolean>, thisArg?: any): [K, V] | undefined {
114
+ find(callbackfn: EntryCallback<K, V, boolean>, thisArg?: unknown): [K, V] | undefined {
115
115
  let index = 0;
116
116
  for (const item of this) {
117
117
  const [key, value] = item;
@@ -228,19 +228,19 @@ export abstract class IterableEntryBase<K = any, V = any> {
228
228
  * Map entries using an implementation-specific strategy.
229
229
  * @remarks Time O(n), Space O(n)
230
230
  */
231
- abstract map(...args: any[]): any;
231
+ abstract map(...args: unknown[]): unknown;
232
232
 
233
233
  /**
234
234
  * Filter entries and return the same-species structure.
235
235
  * @returns A new instance of the same concrete class (`this` type).
236
236
  * @remarks Time O(n), Space O(n)
237
237
  */
238
- abstract filter(...args: any[]): this;
238
+ abstract filter(...args: unknown[]): this;
239
239
 
240
240
  /**
241
241
  * Underlying iterator for the default iteration protocol.
242
242
  * @returns Iterator of `[K, V]`.
243
243
  * @remarks Time O(n), Space O(1)
244
244
  */
245
- protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
245
+ protected abstract _getIterator(...args: unknown[]): IterableIterator<[K, V]>;
246
246
  }
@@ -148,7 +148,7 @@ export abstract class LinearBase<
148
148
  * @returns Index or `-1`.
149
149
  * @remarks Time O(n), Space O(1)
150
150
  */
151
- findIndex(predicate: ElementCallback<E, R, boolean>, thisArg?: any): number {
151
+ findIndex(predicate: ElementCallback<E, R, boolean>, thisArg?: unknown): number {
152
152
  for (let i = 0; i < this.length; i++) {
153
153
  const item = this.at(i);
154
154
  if (item !== undefined && predicate.call(thisArg, item, i, this)) return i;
@@ -389,7 +389,7 @@ export abstract class LinearBase<
389
389
  * @returns Iterator of elements from tail to head.
390
390
  * @remarks Time O(n), Space O(1)
391
391
  */
392
- protected abstract _getReverseIterator(...args: any[]): IterableIterator<E>;
392
+ protected abstract _getReverseIterator(...args: unknown[]): IterableIterator<E>;
393
393
  }
394
394
 
395
395
  /**
@@ -621,7 +621,7 @@ export abstract class LinearLinkedBase<
621
621
  * @returns Iterator over nodes.
622
622
  * @remarks Time O(n), Space O(1)
623
623
  */
624
- protected abstract _getNodeIterator(...args: any[]): IterableIterator<NODE>;
624
+ protected abstract _getNodeIterator(...args: unknown[]): IterableIterator<NODE>;
625
625
 
626
626
  /**
627
627
  * Get previous node of a given node.
@@ -375,6 +375,102 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
375
375
 
376
376
 
377
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
+
404
+
405
+
406
+
407
+
408
+
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+
418
+
419
+
420
+
421
+
422
+
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
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
378
474
 
379
475
 
380
476
 
@@ -432,6 +528,78 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
432
528
 
433
529
 
434
530
 
531
+
532
+
533
+
534
+
535
+
536
+
537
+
538
+
539
+
540
+
541
+
542
+
543
+
544
+
545
+
546
+
547
+
548
+
549
+
550
+
551
+
552
+
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
+
579
+
580
+
581
+
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+
590
+
591
+
592
+
593
+
594
+
595
+
596
+
597
+
598
+
599
+
600
+
601
+
602
+
435
603
 
436
604
 
437
605
 
@@ -492,6 +660,54 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
492
660
 
493
661
 
494
662
 
663
+
664
+
665
+
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+
678
+
679
+
680
+
681
+
682
+
683
+
684
+
685
+
686
+
687
+
688
+
689
+
690
+
691
+
692
+
693
+
694
+
695
+
696
+
697
+
698
+
699
+
700
+
701
+
702
+
703
+
704
+
705
+
706
+
707
+
708
+
709
+
710
+
495
711
  * @example
496
712
  * // Rebalance the tree
497
713
  * const avl = new AVLTree<number>();
@@ -561,6 +777,78 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
561
777
 
562
778
 
563
779
 
780
+
781
+
782
+
783
+
784
+
785
+
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+
795
+
796
+
797
+
798
+
799
+
800
+
801
+
802
+
803
+
804
+
805
+
806
+
807
+
808
+
809
+
810
+
811
+
812
+
813
+
814
+
815
+
816
+
817
+
818
+
819
+
820
+
821
+
822
+
823
+
824
+
825
+
826
+
827
+
828
+
829
+
830
+
831
+
832
+
833
+
834
+
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
844
+
845
+
846
+
847
+
848
+
849
+
850
+
851
+
564
852
 
565
853
 
566
854
  * @example
@@ -718,6 +1006,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
718
1006
  }
719
1007
  this._updateHeight(A);
720
1008
  if (B) this._updateHeight(B);
1009
+ this._updateCount(A);
1010
+ if (B) this._updateCount(B);
721
1011
  }
722
1012
 
723
1013
  /**
@@ -770,6 +1060,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
770
1060
  this._updateHeight(A);
771
1061
  if (B) this._updateHeight(B);
772
1062
  if (C) this._updateHeight(C);
1063
+ this._updateCount(A);
1064
+ if (B) this._updateCount(B);
1065
+ if (C) this._updateCount(C);
773
1066
  }
774
1067
 
775
1068
  /**
@@ -809,6 +1102,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
809
1102
  }
810
1103
  this._updateHeight(A);
811
1104
  if (B) this._updateHeight(B);
1105
+ this._updateCount(A);
1106
+ if (B) this._updateCount(B);
812
1107
  }
813
1108
 
814
1109
  /**
@@ -860,6 +1155,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
860
1155
  this._updateHeight(A);
861
1156
  if (B) this._updateHeight(B);
862
1157
  if (C) this._updateHeight(C);
1158
+ this._updateCount(A);
1159
+ if (B) this._updateCount(B);
1160
+ if (C) this._updateCount(C);
863
1161
  }
864
1162
 
865
1163
  /**
@@ -878,6 +1176,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
878
1176
  const A = path[i];
879
1177
  if (A) {
880
1178
  this._updateHeight(A);
1179
+ this._updateCount(A);
881
1180
 
882
1181
  // Check the balance factor
883
1182
  switch (this._balanceFactor(A)) {