binary-tree-typed 2.5.2 → 2.6.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 (63) hide show
  1. package/dist/cjs/index.cjs +320 -55
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +320 -55
  4. package/dist/cjs-legacy/index.cjs.map +1 -1
  5. package/dist/esm/index.mjs +320 -55
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +320 -55
  8. package/dist/esm-legacy/index.mjs.map +1 -1
  9. package/dist/types/data-structures/base/iterable-element-base.d.ts +17 -0
  10. package/dist/types/data-structures/base/linear-base.d.ts +6 -0
  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 +191 -15
  14. package/dist/types/data-structures/binary-tree/bst.d.ts +171 -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 +1061 -167
  18. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1232 -355
  19. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +916 -194
  20. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1078 -141
  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 +150 -2
  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 +171 -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/umd/binary-tree-typed.js +320 -55
  35. package/dist/umd/binary-tree-typed.js.map +1 -1
  36. package/dist/umd/binary-tree-typed.min.js +5 -5
  37. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/data-structures/base/iterable-element-base.ts +32 -0
  40. package/src/data-structures/base/linear-base.ts +11 -0
  41. package/src/data-structures/binary-tree/avl-tree.ts +88 -5
  42. package/src/data-structures/binary-tree/binary-indexed-tree.ts +98 -0
  43. package/src/data-structures/binary-tree/binary-tree.ts +242 -81
  44. package/src/data-structures/binary-tree/bst.ts +173 -7
  45. package/src/data-structures/binary-tree/red-black-tree.ts +139 -15
  46. package/src/data-structures/binary-tree/segment-tree.ts +42 -0
  47. package/src/data-structures/binary-tree/tree-map.ts +948 -36
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +893 -13
  49. package/src/data-structures/binary-tree/tree-multi-set.ts +761 -33
  50. package/src/data-structures/binary-tree/tree-set.ts +1260 -251
  51. package/src/data-structures/graph/directed-graph.ts +71 -1
  52. package/src/data-structures/graph/undirected-graph.ts +64 -1
  53. package/src/data-structures/hash/hash-map.ts +100 -12
  54. package/src/data-structures/heap/heap.ts +149 -19
  55. package/src/data-structures/linked-list/doubly-linked-list.ts +178 -2
  56. package/src/data-structures/linked-list/singly-linked-list.ts +106 -1
  57. package/src/data-structures/linked-list/skip-linked-list.ts +126 -0
  58. package/src/data-structures/matrix/matrix.ts +56 -0
  59. package/src/data-structures/queue/deque.ts +187 -0
  60. package/src/data-structures/queue/queue.ts +109 -0
  61. package/src/data-structures/stack/stack.ts +75 -5
  62. package/src/data-structures/trie/trie.ts +84 -0
  63. package/src/interfaces/binary-tree.ts +1 -9
@@ -181,6 +181,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
181
181
 
182
182
 
183
183
 
184
+
185
+
186
+
187
+
188
+
189
+
190
+
184
191
 
185
192
 
186
193
 
@@ -240,6 +247,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
240
247
 
241
248
 
242
249
 
250
+
251
+
252
+
253
+
254
+
255
+
256
+
243
257
 
244
258
 
245
259
 
@@ -301,6 +315,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
301
315
 
302
316
 
303
317
 
318
+
319
+
320
+
321
+
322
+
323
+
324
+
304
325
 
305
326
 
306
327
 
@@ -350,6 +371,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
350
371
 
351
372
 
352
373
 
374
+
375
+
376
+
377
+
378
+
379
+
380
+
353
381
 
354
382
 
355
383
 
@@ -398,6 +426,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
398
426
 
399
427
 
400
428
 
429
+
430
+
431
+
432
+
433
+
434
+
435
+
401
436
 
402
437
 
403
438
 
@@ -474,6 +509,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
474
509
 
475
510
 
476
511
 
512
+
513
+
514
+
515
+
516
+
517
+
518
+
477
519
 
478
520
 
479
521
 
@@ -532,6 +574,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
532
574
 
533
575
 
534
576
 
577
+
578
+
579
+
580
+
581
+
582
+
583
+
535
584
 
536
585
 
537
586
 
@@ -576,6 +625,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
576
625
 
577
626
 
578
627
 
628
+
629
+
630
+
631
+
632
+
633
+
634
+
579
635
 
580
636
 
581
637
 
@@ -647,6 +703,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
647
703
 
648
704
 
649
705
 
706
+
707
+
708
+
709
+
710
+
711
+
712
+
650
713
 
651
714
 
652
715
 
@@ -714,6 +777,13 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
714
777
 
715
778
 
716
779
 
780
+
781
+
782
+
783
+
784
+
785
+
786
+
717
787
 
718
788
 
719
789
 
@@ -198,6 +198,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
198
198
 
199
199
 
200
200
 
201
+
202
+
203
+
204
+
205
+
206
+
207
+
201
208
 
202
209
 
203
210
 
@@ -256,6 +263,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
256
263
 
257
264
 
258
265
 
266
+
267
+
268
+
269
+
270
+
271
+
272
+
259
273
 
260
274
 
261
275
 
@@ -322,6 +336,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
322
336
 
323
337
 
324
338
 
339
+
340
+
341
+
342
+
343
+
344
+
345
+
325
346
 
326
347
 
327
348
 
@@ -382,6 +403,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
382
403
 
383
404
 
384
405
 
406
+
407
+
408
+
409
+
410
+
411
+
412
+
385
413
 
386
414
 
387
415
 
@@ -428,6 +456,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
428
456
 
429
457
 
430
458
 
459
+
460
+
461
+
462
+
463
+
464
+
465
+
431
466
 
432
467
 
433
468
 
@@ -521,6 +556,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
521
556
 
522
557
 
523
558
 
559
+
560
+
561
+
562
+
563
+
564
+
565
+
524
566
 
525
567
 
526
568
 
@@ -582,6 +624,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
582
624
 
583
625
 
584
626
 
627
+
628
+
629
+
630
+
631
+
632
+
633
+
585
634
 
586
635
 
587
636
 
@@ -631,6 +680,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
631
680
 
632
681
 
633
682
 
683
+
684
+
685
+
686
+
687
+
688
+
689
+
634
690
 
635
691
 
636
692
 
@@ -679,6 +735,13 @@ export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVert
679
735
 
680
736
 
681
737
 
738
+
739
+
740
+
741
+
742
+
743
+
744
+
682
745
 
683
746
 
684
747
 
@@ -161,6 +161,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
161
161
 
162
162
 
163
163
 
164
+
165
+
166
+
167
+
168
+
169
+
170
+
164
171
 
165
172
 
166
173
 
@@ -204,6 +211,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
204
211
 
205
212
 
206
213
 
214
+
215
+
216
+
217
+
218
+
219
+
220
+
207
221
 
208
222
 
209
223
 
@@ -286,6 +300,20 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
286
300
 
287
301
 
288
302
 
303
+
304
+
305
+
306
+
307
+
308
+
309
+
310
+
311
+
312
+
313
+
314
+
315
+
316
+
289
317
 
290
318
 
291
319
 
@@ -320,7 +348,7 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
320
348
  * // Verify entries
321
349
  * console.log([...map.entries()]); // length: 4;
322
350
  */
323
- set(key: K, value: V): boolean;
351
+ set(key: K, value: V): this;
324
352
  /**
325
353
  * Insert many entries from an iterable.
326
354
  * @remarks Time O(N), Space O(N)
@@ -351,6 +379,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
351
379
 
352
380
 
353
381
 
382
+
383
+
384
+
385
+
386
+
387
+
388
+
354
389
 
355
390
 
356
391
 
@@ -398,6 +433,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
398
433
 
399
434
 
400
435
 
436
+
437
+
438
+
439
+
440
+
441
+
442
+
401
443
 
402
444
 
403
445
 
@@ -461,6 +503,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
461
503
 
462
504
 
463
505
 
506
+
507
+
508
+
509
+
510
+
511
+
512
+
464
513
 
465
514
 
466
515
 
@@ -509,6 +558,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
509
558
 
510
559
 
511
560
 
561
+
562
+
563
+
564
+
565
+
566
+
567
+
512
568
 
513
569
 
514
570
 
@@ -562,6 +618,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
562
618
 
563
619
 
564
620
 
621
+
622
+
623
+
624
+
625
+
626
+
627
+
565
628
 
566
629
 
567
630
 
@@ -612,6 +675,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
612
675
 
613
676
 
614
677
 
678
+
679
+
680
+
681
+
682
+
683
+
684
+
615
685
 
616
686
 
617
687
 
@@ -662,6 +732,13 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
662
732
 
663
733
 
664
734
 
735
+
736
+
737
+
738
+
739
+
740
+
741
+
665
742
 
666
743
 
667
744
 
@@ -696,7 +773,7 @@ export declare class HashMap<K = any, V = any, R = [K, V]> extends IterableEntry
696
773
  * console.log(values); // contains 3; // 'Bob', 'Eve'
697
774
  * console.log(values); // contains 7;
698
775
  */
699
- filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): any;
776
+ filter(predicate: EntryCallback<K, V, boolean>, thisArg?: unknown): this;
700
777
  /**
701
778
  * (Protected) Create a like-kind instance and seed it from an iterable.
702
779
  * @remarks Time O(N), Space O(N)
@@ -796,9 +873,9 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
796
873
  * @remarks Time O(1), Space O(1)
797
874
  * @param key - Key.
798
875
  * @param [value] - Value.
799
- * @returns True when the operation succeeds.
876
+ * @returns This map (for chaining).
800
877
  */
801
- set(key: K, value?: V): boolean;
878
+ set(key: K, value?: V): this;
802
879
  setMany(entryOrRawElements: Iterable<R | [K, V]>): boolean[];
803
880
  has(key: K): boolean;
804
881
  get(key: K): V | undefined;
@@ -821,9 +898,10 @@ export declare class LinkedHashMap<K = any, V = any, R = [K, V]> extends Iterabl
821
898
  * Delete the entry at a given index.
822
899
  * @remarks Time O(N), Space O(1)
823
900
  * @param index - Zero-based index.
824
- * @returns True if removed.
901
+ * @returns The removed entry [key, value].
902
+ * @throws {RangeError} If index is out of bounds.
825
903
  */
826
- deleteAt(index: number): boolean;
904
+ deleteAt(index: number): [K, V | undefined];
827
905
  isEmpty(): boolean;
828
906
  isEntry(rawElement: unknown): rawElement is [K, V];
829
907
  clear(): void;
@@ -9,7 +9,7 @@ import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '
9
9
  import { IterableElementBase } from '../base';
10
10
  /**
11
11
  * Binary heap with pluggable comparator; supports fast insertion and removal of the top element.
12
- * @remarks Time O(1), Space O(1)
12
+ * @remarks Typical operations: O(log N) insert/remove, O(1) peek. Space O(N).
13
13
  * @template E
14
14
  * @template R
15
15
  * 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
@@ -192,6 +192,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
192
192
 
193
193
 
194
194
 
195
+
196
+
197
+
198
+
199
+
200
+
201
+
195
202
 
196
203
 
197
204
 
@@ -240,7 +247,7 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
240
247
  static heapify<EE = any, RR = any>(elements: Iterable<EE>, options: HeapOptions<EE, RR>): Heap<EE, RR>;
241
248
  /**
242
249
  * Insert an element.
243
- * @remarks Time O(1) amortized, Space O(1)
250
+ * @remarks Time O(log N) amortized, Space O(1)
244
251
  * @param element - Element to insert.
245
252
  * @returns True.
246
253
 
@@ -270,6 +277,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
270
277
 
271
278
 
272
279
 
280
+
281
+
282
+
283
+
284
+
285
+
286
+
273
287
 
274
288
 
275
289
 
@@ -324,6 +338,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
324
338
 
325
339
 
326
340
 
341
+
342
+
343
+
344
+
345
+
346
+
347
+
327
348
 
328
349
 
329
350
 
@@ -376,6 +397,40 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
376
397
 
377
398
 
378
399
 
400
+
401
+
402
+
403
+
404
+
405
+
406
+ * @example
407
+ * // Heap with custom comparator (MaxHeap behavior)
408
+ * interface Task {
409
+ * id: number;
410
+ * priority: number;
411
+ * name: string;
412
+ * }
413
+ *
414
+ * // Custom comparator for max heap behavior (higher priority first)
415
+ * const tasks: Task[] = [
416
+ * { id: 1, priority: 5, name: 'Email' },
417
+ * { id: 2, priority: 3, name: 'Chat' },
418
+ * { id: 3, priority: 8, name: 'Alert' }
419
+ * ];
420
+ *
421
+ * const maxHeap = new Heap(tasks, {
422
+ * comparator: (a: Task, b: Task) => b.priority - a.priority
423
+ * });
424
+ *
425
+ * console.log(maxHeap.size); // 3;
426
+ *
427
+ * // Peek returns highest priority task
428
+ * const topTask = maxHeap.peek();
429
+ * console.log(topTask?.priority); // 8;
430
+ * console.log(topTask?.name); // 'Alert';
431
+ */
432
+ /**
433
+ * @deprecated Use `pop` instead. Will be removed in a future major version.
379
434
 
380
435
 
381
436
 
@@ -406,6 +461,12 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
406
461
  * console.log(topTask?.name); // 'Alert';
407
462
  */
408
463
  poll(): E | undefined;
464
+ /**
465
+ * Remove and return the top element (min or max depending on comparator).
466
+ * @remarks Time O(log N) amortized, Space O(1)
467
+ * @returns The removed top element, or undefined if empty.
468
+ */
469
+ pop(): E | undefined;
409
470
  /**
410
471
  * Get the current top element without removing it.
411
472
  * @remarks Time O(1), Space O(1)
@@ -437,6 +498,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
437
498
 
438
499
 
439
500
 
501
+
502
+
503
+
504
+
505
+
506
+
507
+
440
508
 
441
509
 
442
510
 
@@ -535,6 +603,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
535
603
 
536
604
 
537
605
 
606
+
607
+
608
+
609
+
610
+
611
+
612
+
538
613
 
539
614
 
540
615
 
@@ -580,6 +655,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
580
655
 
581
656
 
582
657
 
658
+
659
+
660
+
661
+
662
+
663
+
664
+
583
665
 
584
666
 
585
667
 
@@ -595,13 +677,6 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
595
677
  * console.log(heap.isEmpty()); // true;
596
678
  */
597
679
  clear(): void;
598
- /**
599
- * Replace the backing array and rebuild the heap.
600
- * @remarks Time O(N), Space O(N)
601
- * @param elements - Iterable used to refill the heap.
602
- * @returns Array of per-node results from fixing steps.
603
- */
604
- refill(elements: Iterable<E>): boolean[];
605
680
  /**
606
681
  * Check if an equal element exists in the heap.
607
682
  * @remarks Time O(N), Space O(1)
@@ -625,6 +700,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
625
700
 
626
701
 
627
702
 
703
+
704
+
705
+
706
+
707
+
708
+
709
+
628
710
 
629
711
 
630
712
 
@@ -669,6 +751,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
669
751
 
670
752
 
671
753
 
754
+
755
+
756
+
757
+
758
+
759
+
760
+
672
761
 
673
762
 
674
763
 
@@ -684,13 +773,17 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
684
773
  * console.log(heap.toArray().includes(4)); // false;
685
774
  */
686
775
  delete(element: E): boolean;
776
+ /**
777
+ * @deprecated Use `deleteWhere` instead. Will be removed in a future major version.
778
+ */
779
+ deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean;
687
780
  /**
688
781
  * Delete the first element that matches a predicate.
689
782
  * @remarks Time O(N), Space O(1)
690
783
  * @param predicate - Function (element, index, heap) → boolean.
691
784
  * @returns True if an element was removed.
692
785
  */
693
- deleteBy(predicate: (element: E, index: number, heap: this) => boolean): boolean;
786
+ deleteWhere(predicate: (element: E, index: number, heap: this) => boolean): boolean;
694
787
  /**
695
788
  * Set the equality comparator used by has/delete operations.
696
789
  * @remarks Time O(1), Space O(1)
@@ -721,6 +814,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
721
814
 
722
815
 
723
816
 
817
+
818
+
819
+
820
+
821
+
822
+
823
+
724
824
 
725
825
 
726
826
 
@@ -773,6 +873,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
773
873
 
774
874
 
775
875
 
876
+
877
+
878
+
879
+
880
+
881
+
882
+
776
883
 
777
884
 
778
885
 
@@ -817,6 +924,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
817
924
 
818
925
 
819
926
 
927
+
928
+
929
+
930
+
931
+
932
+
933
+
820
934
 
821
935
 
822
936
 
@@ -865,6 +979,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
865
979
 
866
980
 
867
981
 
982
+
983
+
984
+
985
+
986
+
987
+
988
+
868
989
 
869
990
 
870
991
 
@@ -913,6 +1034,13 @@ export declare class Heap<E = any, R = any> extends IterableElementBase<E, R> {
913
1034
 
914
1035
 
915
1036
 
1037
+
1038
+
1039
+
1040
+
1041
+
1042
+
1043
+
916
1044
 
917
1045
 
918
1046
 
@@ -1029,9 +1157,9 @@ export declare class FibonacciHeap<E> {
1029
1157
  * Push an element into the root list.
1030
1158
  * @remarks Time O(1) amortized, Space O(1)
1031
1159
  * @param element - Element to insert.
1032
- * @returns This heap.
1160
+ * @returns True when the element is added.
1033
1161
  */
1034
- push(element: E): this;
1162
+ push(element: E): boolean;
1035
1163
  peek(): E | undefined;
1036
1164
  /**
1037
1165
  * Collect nodes from a circular doubly linked list starting at head.