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
@@ -254,6 +254,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
254
254
  */
255
255
  constructor(keysNodesEntriesOrRaws?: Iterable<K | BSTNode | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BSTOptions<K, V, R>);
256
256
  protected _root?: BSTNode<K, V>;
257
+ protected _enableOrderStatistic: boolean;
257
258
  /**
258
259
  * Gets the root node of the tree.
259
260
  * @remarks Time O(1)
@@ -367,6 +368,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
367
368
 
368
369
 
369
370
 
371
+
372
+
373
+
374
+
375
+
376
+
370
377
 
371
378
 
372
379
 
@@ -433,6 +440,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
433
440
 
434
441
 
435
442
 
443
+
444
+
445
+
446
+
447
+
448
+
436
449
 
437
450
 
438
451
 
@@ -502,6 +515,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
502
515
 
503
516
 
504
517
 
518
+
519
+
520
+
521
+
522
+
523
+
505
524
 
506
525
 
507
526
 
@@ -582,6 +601,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
582
601
 
583
602
 
584
603
 
604
+
605
+
606
+
607
+
608
+
609
+
585
610
 
586
611
 
587
612
 
@@ -646,6 +671,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
646
671
 
647
672
 
648
673
 
674
+
675
+
676
+
677
+
678
+
679
+
649
680
 
650
681
 
651
682
 
@@ -691,6 +722,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
691
722
 
692
723
 
693
724
 
725
+
726
+
727
+
694
728
 
695
729
 
696
730
 
@@ -702,6 +736,69 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
702
736
  */
703
737
  rangeSearch(range: Range<K> | [K, K]): (K | undefined)[];
704
738
  rangeSearch<C extends NodeCallback<BSTNode<K, V>>>(range: Range<K> | [K, K], callback: C, startNode?: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
739
+ /**
740
+ * Returns the element at the k-th position in tree order (0-indexed).
741
+ * @remarks Time O(log n), Space O(1) iterative / O(log n) recursive. Requires `enableOrderStatistic: true`.
742
+ * Tree order is defined by the comparator — ascending by default, but respects custom comparators (e.g. descending).
743
+ *
744
+ * @param k - The 0-based position in tree order (0 = first element).
745
+ * @returns The key at position k, or `undefined` if out of bounds.
746
+ * @example
747
+ * // Order-statistic on BST
748
+ * const tree = new BST<number>([30, 10, 50, 20, 40], { enableOrderStatistic: true });
749
+ * console.log(tree.getByRank(0)); // 10;
750
+ * console.log(tree.getByRank(4)); // 50;
751
+ * console.log(tree.getRank(30)); // 2;
752
+ */
753
+ getByRank(k: number): K | undefined;
754
+ /**
755
+ * Returns the element at the k-th position in tree order and applies a callback.
756
+ * @remarks Time O(log n), Space O(1) iterative / O(log n) recursive. Requires `enableOrderStatistic: true`.
757
+ *
758
+ * @param k - The 0-based position in tree order (0 = first element).
759
+ * @param callback - Callback to apply to the found node.
760
+ * @param iterationType - Iteration strategy ('ITERATIVE' or 'RECURSIVE').
761
+ * @returns The callback result, or `undefined` if out of bounds.
762
+ */
763
+ getByRank<C extends NodeCallback<BSTNode<K, V>>>(k: number, callback: C, iterationType?: IterationType): ReturnType<C> | undefined;
764
+ /**
765
+ * Returns the 0-based rank of a key (number of elements that precede it in tree order).
766
+ * @remarks Time O(log n), Space O(1) iterative / O(log n) recursive. Requires `enableOrderStatistic: true`.
767
+ * Tree order is defined by the comparator. When the key is not found, returns the insertion position.
768
+ *
769
+ * @param keyNodeEntryOrPredicate - The key, node, entry `[K, V]`, or predicate to find.
770
+ * @returns The rank (0-indexed), or -1 if the tree is empty or input is invalid.
771
+ */
772
+ getRank(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>): number;
773
+ /**
774
+ * Returns the 0-based rank (number of preceding elements in tree order) with explicit iteration type.
775
+ * @remarks Time O(log n), Space O(1) iterative / O(log n) recursive. Requires `enableOrderStatistic: true`.
776
+ *
777
+ * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate to find.
778
+ * @param iterationType - Iteration strategy ('ITERATIVE' or 'RECURSIVE').
779
+ * @returns The rank (0-indexed), or -1 if the tree is empty or input is invalid.
780
+ */
781
+ getRank(keyNodeEntryOrPredicate: K | BSTNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BSTNode<K, V>>, iterationType: IterationType): number;
782
+ /**
783
+ * Returns elements by position range in tree order (0-indexed, inclusive on both ends).
784
+ * @remarks Time O(log n + k), Space O(k), where k = end - start + 1. Requires `enableOrderStatistic: true`.
785
+ *
786
+ * @param start - Start position (inclusive, 0-indexed). Clamped to 0 if negative.
787
+ * @param end - End position (inclusive, 0-indexed). Clamped to size-1 if too large.
788
+ * @returns Array of keys in tree order within the specified range.
789
+ */
790
+ rangeByRank(start: number, end: number): (K | undefined)[];
791
+ /**
792
+ * Returns elements by position range in tree order with callback and optional iteration type.
793
+ * @remarks Time O(log n + k), Space O(k), where k = end - start + 1. Requires `enableOrderStatistic: true`.
794
+ *
795
+ * @param start - Start rank (inclusive, 0-indexed).
796
+ * @param end - End rank (inclusive, 0-indexed).
797
+ * @param callback - Callback to apply to each node in the range.
798
+ * @param iterationType - Iteration strategy ('ITERATIVE' or 'RECURSIVE').
799
+ * @returns Array of callback results for nodes in the rank range.
800
+ */
801
+ rangeByRank<C extends NodeCallback<BSTNode<K, V>>>(start: number, end: number, callback: C, iterationType?: IterationType): ReturnType<C>[];
705
802
  /**
706
803
  * Adds a new node to the BST based on key comparison.
707
804
  * @remarks Time O(log N), where H is tree height. O(N) worst-case (unbalanced tree), O(log N) average. Space O(1).
@@ -787,6 +884,15 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
787
884
 
788
885
 
789
886
 
887
+
888
+
889
+
890
+
891
+
892
+
893
+
894
+
895
+
790
896
 
791
897
 
792
898
 
@@ -862,6 +968,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
862
968
 
863
969
 
864
970
 
971
+
972
+
973
+
974
+
975
+
976
+
865
977
 
866
978
 
867
979
 
@@ -910,6 +1022,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
910
1022
 
911
1023
 
912
1024
 
1025
+
1026
+
1027
+
913
1028
 
914
1029
 
915
1030
 
@@ -961,6 +1076,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
961
1076
 
962
1077
 
963
1078
 
1079
+
1080
+
1081
+
964
1082
 
965
1083
 
966
1084
 
@@ -1011,6 +1129,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1011
1129
 
1012
1130
 
1013
1131
 
1132
+
1133
+
1134
+
1014
1135
 
1015
1136
 
1016
1137
 
@@ -1062,6 +1183,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1062
1183
 
1063
1184
 
1064
1185
 
1186
+
1187
+
1188
+
1065
1189
 
1066
1190
 
1067
1191
 
@@ -1113,6 +1237,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1113
1237
 
1114
1238
 
1115
1239
 
1240
+
1241
+
1242
+
1116
1243
 
1117
1244
 
1118
1245
 
@@ -1159,6 +1286,9 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1159
1286
 
1160
1287
 
1161
1288
 
1289
+
1290
+
1291
+
1162
1292
 
1163
1293
 
1164
1294
 
@@ -1235,6 +1365,12 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1235
1365
 
1236
1366
 
1237
1367
 
1368
+
1369
+
1370
+
1371
+
1372
+
1373
+
1238
1374
 
1239
1375
 
1240
1376
 
@@ -1412,6 +1548,41 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
1412
1548
  *
1413
1549
  * @param v - The node to set as root.
1414
1550
  */
1551
+ /**
1552
+ * (Protected) Recalculates the subtree count for a single node.
1553
+ * @remarks Time O(1). Only active when enableOrderStatistic is true.
1554
+ */
1555
+ protected _updateCount(node: BSTNode<K, V>): void;
1556
+ /**
1557
+ * (Protected) Updates subtree counts from a node up to the root.
1558
+ * @remarks Time O(log n). Only active when enableOrderStatistic is true.
1559
+ */
1560
+ protected _updateCountAlongPath(node: OptNode<BSTNode<K, V>>): void;
1561
+ /**
1562
+ * (Protected) Finds the node at position k in tree order (iterative).
1563
+ * @remarks Time O(log n), Space O(1)
1564
+ */
1565
+ protected _getByRankIterative(node: OptNode<BSTNode<K, V>>, k: number): BSTNode<K, V> | undefined;
1566
+ /**
1567
+ * (Protected) Finds the node at position k in tree order (recursive).
1568
+ * @remarks Time O(log n), Space O(log n) call stack
1569
+ */
1570
+ protected _getByRankRecursive(node: OptNode<BSTNode<K, V>>, k: number): BSTNode<K, V> | undefined;
1571
+ /**
1572
+ * (Protected) Computes the rank of a key iteratively.
1573
+ * @remarks Time O(log n), Space O(1)
1574
+ */
1575
+ protected _getRankIterative(node: OptNode<BSTNode<K, V>>, key: K): number;
1576
+ /**
1577
+ * (Protected) Computes the rank of a key recursively.
1578
+ * @remarks Time O(log n), Space O(log n) call stack
1579
+ */
1580
+ protected _getRankRecursive(node: OptNode<BSTNode<K, V>>, key: K): number;
1581
+ /**
1582
+ * (Protected) Finds the in-order successor of a node.
1583
+ * @remarks Time O(log n), Space O(1)
1584
+ */
1585
+ protected _next(node: BSTNode<K, V>): BSTNode<K, V> | undefined;
1415
1586
  protected _setRoot(v: OptNode<BSTNode<K, V>>): void;
1416
1587
  /**
1417
1588
  * (Protected) Compares two keys using the tree's comparator and reverse setting.
@@ -330,6 +330,18 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
330
330
 
331
331
 
332
332
 
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+
342
+
343
+
344
+
333
345
 
334
346
 
335
347
 
@@ -556,6 +568,18 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
556
568
 
557
569
 
558
570
 
571
+
572
+
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
559
583
 
560
584
 
561
585
 
@@ -702,6 +726,18 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
702
726
 
703
727
 
704
728
 
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+
738
+
739
+
740
+
705
741
 
706
742
 
707
743
 
@@ -816,6 +852,15 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
816
852
 
817
853
 
818
854
 
855
+
856
+
857
+
858
+
859
+
860
+
861
+
862
+
863
+
819
864
 
820
865
 
821
866
 
@@ -941,6 +986,18 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
941
986
 
942
987
 
943
988
 
989
+
990
+
991
+
992
+
993
+
994
+
995
+
996
+
997
+
998
+
999
+
1000
+
944
1001
 
945
1002
 
946
1003
 
@@ -73,6 +73,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
73
73
 
74
74
 
75
75
 
76
+
77
+
78
+
76
79
 
77
80
 
78
81
 
@@ -131,6 +134,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
131
134
 
132
135
 
133
136
 
137
+
138
+
139
+
134
140
 
135
141
 
136
142
 
@@ -185,6 +191,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
185
191
 
186
192
 
187
193
 
194
+
195
+
196
+
188
197
 
189
198
 
190
199
 
@@ -234,6 +243,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
234
243
 
235
244
 
236
245
 
246
+
247
+
248
+
237
249
 
238
250
 
239
251
 
@@ -278,6 +290,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
278
290
 
279
291
 
280
292
 
293
+
294
+
295
+
281
296
 
282
297
 
283
298
 
@@ -325,6 +340,9 @@ export declare class SegmentTree<E = number> implements Iterable<E> {
325
340
 
326
341
 
327
342
 
343
+
344
+
345
+
328
346
 
329
347
 
330
348