binary-tree-typed 2.5.1 → 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 (73) hide show
  1. package/dist/cjs/index.cjs +146 -52
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +145 -51
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +146 -53
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +145 -52
  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 +36 -0
  12. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +42 -0
  13. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +77 -2
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -0
  15. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +57 -0
  16. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +18 -0
  17. package/dist/types/data-structures/binary-tree/tree-map.d.ts +409 -0
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +411 -6
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +339 -6
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +391 -0
  21. package/dist/types/data-structures/graph/directed-graph.d.ts +30 -0
  22. package/dist/types/data-structures/graph/undirected-graph.d.ts +27 -0
  23. package/dist/types/data-structures/hash/hash-map.d.ts +33 -0
  24. package/dist/types/data-structures/heap/heap.d.ts +42 -0
  25. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +51 -0
  26. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +45 -0
  27. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +54 -0
  28. package/dist/types/data-structures/matrix/matrix.d.ts +24 -0
  29. package/dist/types/data-structures/queue/deque.d.ts +45 -0
  30. package/dist/types/data-structures/queue/queue.d.ts +36 -0
  31. package/dist/types/data-structures/stack/stack.d.ts +30 -0
  32. package/dist/types/data-structures/trie/trie.d.ts +36 -0
  33. package/dist/types/types/data-structures/binary-tree/bst.d.ts +1 -0
  34. package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +5 -0
  35. package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +4 -0
  36. package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +4 -0
  37. package/dist/umd/binary-tree-typed.js +143 -50
  38. package/dist/umd/binary-tree-typed.js.map +1 -1
  39. package/dist/umd/binary-tree-typed.min.js +3 -3
  40. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  41. package/package.json +2 -2
  42. package/src/common/error.ts +19 -1
  43. package/src/common/index.ts +1 -1
  44. package/src/data-structures/base/iterable-element-base.ts +3 -2
  45. package/src/data-structures/binary-tree/avl-tree.ts +47 -0
  46. package/src/data-structures/binary-tree/binary-indexed-tree.ts +46 -4
  47. package/src/data-structures/binary-tree/binary-tree.ts +79 -4
  48. package/src/data-structures/binary-tree/bst.ts +441 -6
  49. package/src/data-structures/binary-tree/red-black-tree.ts +73 -0
  50. package/src/data-structures/binary-tree/segment-tree.ts +18 -0
  51. package/src/data-structures/binary-tree/tree-map.ts +434 -9
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +426 -5
  53. package/src/data-structures/binary-tree/tree-multi-set.ts +350 -6
  54. package/src/data-structures/binary-tree/tree-set.ts +410 -8
  55. package/src/data-structures/graph/abstract-graph.ts +2 -2
  56. package/src/data-structures/graph/directed-graph.ts +30 -0
  57. package/src/data-structures/graph/undirected-graph.ts +27 -0
  58. package/src/data-structures/hash/hash-map.ts +35 -4
  59. package/src/data-structures/heap/heap.ts +46 -4
  60. package/src/data-structures/heap/max-heap.ts +2 -2
  61. package/src/data-structures/linked-list/doubly-linked-list.ts +51 -0
  62. package/src/data-structures/linked-list/singly-linked-list.ts +45 -0
  63. package/src/data-structures/linked-list/skip-linked-list.ts +59 -5
  64. package/src/data-structures/matrix/matrix.ts +33 -9
  65. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -2
  66. package/src/data-structures/queue/deque.ts +45 -0
  67. package/src/data-structures/queue/queue.ts +36 -0
  68. package/src/data-structures/stack/stack.ts +30 -0
  69. package/src/data-structures/trie/trie.ts +38 -2
  70. package/src/types/data-structures/binary-tree/bst.ts +1 -0
  71. package/src/types/data-structures/binary-tree/tree-map.ts +6 -0
  72. package/src/types/data-structures/binary-tree/tree-multi-set.ts +5 -0
  73. 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.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.1"
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,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
  }
@@ -467,6 +467,18 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
467
467
 
468
468
 
469
469
 
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
470
482
 
471
483
 
472
484
 
@@ -590,6 +602,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
590
602
 
591
603
 
592
604
 
605
+
606
+
607
+
608
+
609
+
610
+
611
+
612
+
613
+
593
614
 
594
615
 
595
616
 
@@ -674,6 +695,12 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
674
695
 
675
696
 
676
697
 
698
+
699
+
700
+
701
+
702
+
703
+
677
704
 
678
705
 
679
706
 
@@ -805,6 +832,15 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
805
832
 
806
833
 
807
834
 
835
+
836
+
837
+
838
+
839
+
840
+
841
+
842
+
843
+
808
844
 
809
845
 
810
846
 
@@ -970,6 +1006,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
970
1006
  }
971
1007
  this._updateHeight(A);
972
1008
  if (B) this._updateHeight(B);
1009
+ this._updateCount(A);
1010
+ if (B) this._updateCount(B);
973
1011
  }
974
1012
 
975
1013
  /**
@@ -1022,6 +1060,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1022
1060
  this._updateHeight(A);
1023
1061
  if (B) this._updateHeight(B);
1024
1062
  if (C) this._updateHeight(C);
1063
+ this._updateCount(A);
1064
+ if (B) this._updateCount(B);
1065
+ if (C) this._updateCount(C);
1025
1066
  }
1026
1067
 
1027
1068
  /**
@@ -1061,6 +1102,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1061
1102
  }
1062
1103
  this._updateHeight(A);
1063
1104
  if (B) this._updateHeight(B);
1105
+ this._updateCount(A);
1106
+ if (B) this._updateCount(B);
1064
1107
  }
1065
1108
 
1066
1109
  /**
@@ -1112,6 +1155,9 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1112
1155
  this._updateHeight(A);
1113
1156
  if (B) this._updateHeight(B);
1114
1157
  if (C) this._updateHeight(C);
1158
+ this._updateCount(A);
1159
+ if (B) this._updateCount(B);
1160
+ if (C) this._updateCount(C);
1115
1161
  }
1116
1162
 
1117
1163
  /**
@@ -1130,6 +1176,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
1130
1176
  const A = path[i];
1131
1177
  if (A) {
1132
1178
  this._updateHeight(A);
1179
+ this._updateCount(A);
1133
1180
 
1134
1181
  // Check the balance factor
1135
1182
  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);
@@ -105,6 +105,12 @@ export class BinaryIndexedTree implements Iterable<number> {
105
105
 
106
106
 
107
107
 
108
+
109
+
110
+
111
+
112
+
113
+
108
114
 
109
115
 
110
116
 
@@ -176,6 +182,12 @@ export class BinaryIndexedTree implements Iterable<number> {
176
182
 
177
183
 
178
184
 
185
+
186
+
187
+
188
+
189
+
190
+
179
191
 
180
192
 
181
193
 
@@ -247,6 +259,12 @@ export class BinaryIndexedTree implements Iterable<number> {
247
259
 
248
260
 
249
261
 
262
+
263
+
264
+
265
+
266
+
267
+
250
268
 
251
269
 
252
270
 
@@ -318,6 +336,12 @@ export class BinaryIndexedTree implements Iterable<number> {
318
336
 
319
337
 
320
338
 
339
+
340
+
341
+
342
+
343
+
344
+
321
345
 
322
346
 
323
347
 
@@ -387,6 +411,12 @@ export class BinaryIndexedTree implements Iterable<number> {
387
411
 
388
412
 
389
413
 
414
+
415
+
416
+
417
+
418
+
419
+
390
420
 
391
421
 
392
422
 
@@ -464,6 +494,12 @@ export class BinaryIndexedTree implements Iterable<number> {
464
494
 
465
495
 
466
496
 
497
+
498
+
499
+
500
+
501
+
502
+
467
503
 
468
504
 
469
505
 
@@ -517,6 +553,9 @@ export class BinaryIndexedTree implements Iterable<number> {
517
553
 
518
554
 
519
555
 
556
+
557
+
558
+
520
559
 
521
560
 
522
561
 
@@ -583,6 +622,9 @@ export class BinaryIndexedTree implements Iterable<number> {
583
622
 
584
623
 
585
624
 
625
+
626
+
627
+
586
628
 
587
629
 
588
630
 
@@ -665,10 +707,10 @@ export class BinaryIndexedTree implements Iterable<number> {
665
707
 
666
708
  protected _checkIndex(index: number): void {
667
709
  if (!Number.isInteger(index)) {
668
- throw new TypeError(ERR.invalidIndex('BinaryIndexedTree'));
710
+ raise(TypeError, ERR.invalidIndex('BinaryIndexedTree'));
669
711
  }
670
712
  if (index < 0 || index >= this._size) {
671
- throw new RangeError(ERR.indexOutOfRange(index, 0, this._size - 1, 'BinaryIndexedTree'));
713
+ raise(RangeError, ERR.indexOutOfRange(index, 0, this._size - 1, 'BinaryIndexedTree'));
672
714
  }
673
715
  }
674
716
 
@@ -28,7 +28,7 @@ import { IBinaryTree } from '../../interfaces';
28
28
  import { isComparable, makeTrampoline, makeTrampolineThunk } from '../../utils';
29
29
  import { Queue } from '../queue';
30
30
  import { IterableEntryBase } from '../base';
31
- import { DFSOperation, ERR, Range } from '../../common';
31
+ import { DFSOperation, ERR, raise, Range } from '../../common';
32
32
 
33
33
  /**
34
34
  * @template K - The type of the key.
@@ -216,7 +216,7 @@ export class BinaryTreeNode<K = any, V = any> {
216
216
  * node?: BinaryTreeNode<string> | null,
217
217
  * conditions?: { [key: string]: boolean }
218
218
  * ): string {
219
- * if (!node) throw new Error('Invalid node');
219
+ * if (!node) raise(Error, 'Invalid node');
220
220
  *
221
221
  * // If it's a leaf node, return the decision result
222
222
  * if (!node.left && !node.right) return node.key;
@@ -261,7 +261,7 @@ export class BinaryTreeNode<K = any, V = any> {
261
261
  * case '/':
262
262
  * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
263
263
  * default:
264
- * throw new Error(`Unsupported operator: ${node.key}`);
264
+ * raise(Error, `Unsupported operator: ${node.key}`);
265
265
  * }
266
266
  * }
267
267
  *
@@ -293,7 +293,7 @@ export class BinaryTree<K = any, V = any, R = any>
293
293
  if (isMapMode !== undefined) this._isMapMode = isMapMode;
294
294
  if (isDuplicate !== undefined) this._isDuplicate = isDuplicate;
295
295
  if (typeof toEntryFn === 'function') this._toEntryFn = toEntryFn;
296
- else if (toEntryFn) throw new TypeError(ERR.notAFunction('toEntryFn', 'BinaryTree'));
296
+ else if (toEntryFn) raise(TypeError, ERR.notAFunction('toEntryFn', 'BinaryTree'));
297
297
  }
298
298
 
299
299
  if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
@@ -589,6 +589,9 @@ export class BinaryTree<K = any, V = any, R = any>
589
589
 
590
590
 
591
591
 
592
+
593
+
594
+
592
595
 
593
596
 
594
597
 
@@ -643,6 +646,9 @@ export class BinaryTree<K = any, V = any, R = any>
643
646
 
644
647
 
645
648
 
649
+
650
+
651
+
646
652
 
647
653
 
648
654
 
@@ -759,6 +765,9 @@ export class BinaryTree<K = any, V = any, R = any>
759
765
 
760
766
 
761
767
 
768
+
769
+
770
+
762
771
 
763
772
 
764
773
 
@@ -803,6 +812,9 @@ export class BinaryTree<K = any, V = any, R = any>
803
812
 
804
813
 
805
814
 
815
+
816
+
817
+
806
818
 
807
819
 
808
820
 
@@ -873,6 +885,9 @@ export class BinaryTree<K = any, V = any, R = any>
873
885
 
874
886
 
875
887
 
888
+
889
+
890
+
876
891
 
877
892
 
878
893
 
@@ -939,6 +954,9 @@ export class BinaryTree<K = any, V = any, R = any>
939
954
 
940
955
 
941
956
 
957
+
958
+
959
+
942
960
 
943
961
 
944
962
 
@@ -1035,6 +1053,9 @@ export class BinaryTree<K = any, V = any, R = any>
1035
1053
 
1036
1054
 
1037
1055
 
1056
+
1057
+
1058
+
1038
1059
 
1039
1060
 
1040
1061
 
@@ -1168,6 +1189,9 @@ export class BinaryTree<K = any, V = any, R = any>
1168
1189
 
1169
1190
 
1170
1191
 
1192
+
1193
+
1194
+
1171
1195
 
1172
1196
 
1173
1197
 
@@ -1240,6 +1264,9 @@ export class BinaryTree<K = any, V = any, R = any>
1240
1264
 
1241
1265
 
1242
1266
 
1267
+
1268
+
1269
+
1243
1270
 
1244
1271
 
1245
1272
 
@@ -1306,6 +1333,9 @@ export class BinaryTree<K = any, V = any, R = any>
1306
1333
 
1307
1334
 
1308
1335
 
1336
+
1337
+
1338
+
1309
1339
 
1310
1340
 
1311
1341
 
@@ -1365,6 +1395,9 @@ export class BinaryTree<K = any, V = any, R = any>
1365
1395
 
1366
1396
 
1367
1397
 
1398
+
1399
+
1400
+
1368
1401
 
1369
1402
 
1370
1403
 
@@ -1460,6 +1493,9 @@ export class BinaryTree<K = any, V = any, R = any>
1460
1493
 
1461
1494
 
1462
1495
 
1496
+
1497
+
1498
+
1463
1499
 
1464
1500
 
1465
1501
 
@@ -1506,6 +1542,9 @@ export class BinaryTree<K = any, V = any, R = any>
1506
1542
 
1507
1543
 
1508
1544
 
1545
+
1546
+
1547
+
1509
1548
 
1510
1549
 
1511
1550
 
@@ -1564,6 +1603,9 @@ export class BinaryTree<K = any, V = any, R = any>
1564
1603
 
1565
1604
 
1566
1605
 
1606
+
1607
+
1608
+
1567
1609
 
1568
1610
 
1569
1611
 
@@ -1652,6 +1694,9 @@ export class BinaryTree<K = any, V = any, R = any>
1652
1694
 
1653
1695
 
1654
1696
 
1697
+
1698
+
1699
+
1655
1700
 
1656
1701
 
1657
1702
 
@@ -1714,6 +1759,9 @@ export class BinaryTree<K = any, V = any, R = any>
1714
1759
 
1715
1760
 
1716
1761
 
1762
+
1763
+
1764
+
1717
1765
 
1718
1766
 
1719
1767
 
@@ -2017,6 +2065,9 @@ export class BinaryTree<K = any, V = any, R = any>
2017
2065
 
2018
2066
 
2019
2067
 
2068
+
2069
+
2070
+
2020
2071
 
2021
2072
 
2022
2073
 
@@ -2102,6 +2153,9 @@ export class BinaryTree<K = any, V = any, R = any>
2102
2153
 
2103
2154
 
2104
2155
 
2156
+
2157
+
2158
+
2105
2159
 
2106
2160
 
2107
2161
 
@@ -2246,6 +2300,9 @@ export class BinaryTree<K = any, V = any, R = any>
2246
2300
 
2247
2301
 
2248
2302
 
2303
+
2304
+
2305
+
2249
2306
 
2250
2307
 
2251
2308
 
@@ -2343,6 +2400,9 @@ export class BinaryTree<K = any, V = any, R = any>
2343
2400
 
2344
2401
 
2345
2402
 
2403
+
2404
+
2405
+
2346
2406
 
2347
2407
 
2348
2408
 
@@ -2458,6 +2518,9 @@ export class BinaryTree<K = any, V = any, R = any>
2458
2518
 
2459
2519
 
2460
2520
 
2521
+
2522
+
2523
+
2461
2524
 
2462
2525
 
2463
2526
 
@@ -2617,6 +2680,9 @@ export class BinaryTree<K = any, V = any, R = any>
2617
2680
 
2618
2681
 
2619
2682
 
2683
+
2684
+
2685
+
2620
2686
 
2621
2687
 
2622
2688
 
@@ -2667,6 +2733,9 @@ export class BinaryTree<K = any, V = any, R = any>
2667
2733
 
2668
2734
 
2669
2735
 
2736
+
2737
+
2738
+
2670
2739
 
2671
2740
 
2672
2741
 
@@ -2721,6 +2790,9 @@ export class BinaryTree<K = any, V = any, R = any>
2721
2790
 
2722
2791
 
2723
2792
 
2793
+
2794
+
2795
+
2724
2796
 
2725
2797
 
2726
2798
 
@@ -2808,6 +2880,9 @@ export class BinaryTree<K = any, V = any, R = any>
2808
2880
 
2809
2881
 
2810
2882
 
2883
+
2884
+
2885
+
2811
2886
 
2812
2887
 
2813
2888