binary-tree-typed 2.4.4 → 2.5.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 (85) hide show
  1. package/README.md +0 -84
  2. package/dist/cjs/index.cjs +965 -420
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/cjs-legacy/index.cjs +962 -417
  5. package/dist/cjs-legacy/index.cjs.map +1 -1
  6. package/dist/esm/index.mjs +965 -421
  7. package/dist/esm/index.mjs.map +1 -1
  8. package/dist/esm-legacy/index.mjs +962 -418
  9. package/dist/esm-legacy/index.mjs.map +1 -1
  10. package/dist/types/common/error.d.ts +23 -0
  11. package/dist/types/common/index.d.ts +1 -0
  12. package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
  13. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
  14. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
  15. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +439 -78
  16. package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
  17. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +217 -31
  18. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
  19. package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
  20. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
  21. package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
  22. package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
  23. package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -0
  24. package/dist/types/data-structures/graph/directed-graph.d.ts +220 -47
  25. package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
  26. package/dist/types/data-structures/graph/undirected-graph.d.ts +218 -59
  27. package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
  28. package/dist/types/data-structures/heap/heap.d.ts +287 -99
  29. package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
  30. package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
  31. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
  32. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
  33. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
  34. package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
  38. package/dist/types/data-structures/queue/deque.d.ts +313 -66
  39. package/dist/types/data-structures/queue/queue.d.ts +211 -42
  40. package/dist/types/data-structures/stack/stack.d.ts +174 -32
  41. package/dist/types/data-structures/trie/trie.d.ts +213 -43
  42. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
  43. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
  44. package/dist/types/types/data-structures/queue/deque.d.ts +6 -0
  45. package/dist/umd/binary-tree-typed.js +959 -414
  46. package/dist/umd/binary-tree-typed.js.map +1 -1
  47. package/dist/umd/binary-tree-typed.min.js +3 -3
  48. package/dist/umd/binary-tree-typed.min.js.map +1 -1
  49. package/package.json +2 -2
  50. package/src/common/error.ts +60 -0
  51. package/src/common/index.ts +2 -0
  52. package/src/data-structures/base/iterable-element-base.ts +2 -2
  53. package/src/data-structures/binary-tree/avl-tree.ts +134 -51
  54. package/src/data-structures/binary-tree/binary-indexed-tree.ts +303 -247
  55. package/src/data-structures/binary-tree/binary-tree.ts +542 -121
  56. package/src/data-structures/binary-tree/bst.ts +346 -37
  57. package/src/data-structures/binary-tree/red-black-tree.ts +309 -96
  58. package/src/data-structures/binary-tree/segment-tree.ts +372 -248
  59. package/src/data-structures/binary-tree/tree-map.ts +1292 -13
  60. package/src/data-structures/binary-tree/tree-multi-map.ts +1098 -215
  61. package/src/data-structures/binary-tree/tree-multi-set.ts +863 -69
  62. package/src/data-structures/binary-tree/tree-set.ts +1143 -15
  63. package/src/data-structures/graph/abstract-graph.ts +106 -1
  64. package/src/data-structures/graph/directed-graph.ts +223 -47
  65. package/src/data-structures/graph/map-graph.ts +59 -1
  66. package/src/data-structures/graph/undirected-graph.ts +299 -59
  67. package/src/data-structures/hash/hash-map.ts +243 -79
  68. package/src/data-structures/heap/heap.ts +291 -102
  69. package/src/data-structures/heap/max-heap.ts +48 -3
  70. package/src/data-structures/heap/min-heap.ts +59 -0
  71. package/src/data-structures/linked-list/doubly-linked-list.ts +286 -44
  72. package/src/data-structures/linked-list/singly-linked-list.ts +278 -65
  73. package/src/data-structures/linked-list/skip-linked-list.ts +689 -90
  74. package/src/data-structures/matrix/matrix.ts +425 -22
  75. package/src/data-structures/priority-queue/max-priority-queue.ts +59 -3
  76. package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
  77. package/src/data-structures/priority-queue/priority-queue.ts +60 -0
  78. package/src/data-structures/queue/deque.ts +343 -68
  79. package/src/data-structures/queue/queue.ts +211 -42
  80. package/src/data-structures/stack/stack.ts +174 -32
  81. package/src/data-structures/trie/trie.ts +215 -44
  82. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
  83. package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
  84. package/src/types/data-structures/queue/deque.ts +7 -0
  85. package/src/utils/utils.ts +4 -2
@@ -8,6 +8,7 @@
8
8
 
9
9
  import type { ElementCallback, TrieOptions } from '../../types';
10
10
  import { IterableElementBase } from '../base';
11
+ import { ERR } from '../../common';
11
12
 
12
13
  /**
13
14
  * Node used by Trie to store one character and its children.
@@ -112,32 +113,6 @@ export class TrieNode {
112
113
  * 10. IP Routing: Used in certain types of IP routing algorithms.
113
114
  * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
114
115
  * @example
115
- * // basic Trie creation and add words
116
- * // Create a simple Trie with initial words
117
- * const trie = new Trie(['apple', 'app', 'apply']);
118
- *
119
- * // Verify size
120
- * console.log(trie.size); // 3;
121
- *
122
- * // Check if words exist
123
- * console.log(trie.has('apple')); // true;
124
- * console.log(trie.has('app')); // true;
125
- *
126
- * // Add a new word
127
- * trie.add('application');
128
- * console.log(trie.size); // 4;
129
- * @example
130
- * // Trie getWords and prefix search
131
- * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
132
- *
133
- * // Get all words with prefix 'app'
134
- * const appWords = trie.getWords('app');
135
- * console.log(appWords); // contains 'app';
136
- * console.log(appWords); // contains 'apple';
137
- * console.log(appWords); // contains 'apply';
138
- * console.log(appWords); // contains 'application';
139
- * expect(appWords).not.toContain('apricot');
140
- * @example
141
116
  * // Trie isPrefix and isAbsolutePrefix checks
142
117
  * const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
143
118
  *
@@ -153,23 +128,6 @@ export class TrieNode {
153
128
  * // Verify size
154
129
  * console.log(trie.size); // 5;
155
130
  * @example
156
- * // Trie delete and iteration
157
- * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
158
- *
159
- * // Delete a word
160
- * trie.delete('card');
161
- * console.log(trie.has('card')); // false;
162
- *
163
- * // Word with same prefix still exists
164
- * console.log(trie.has('care')); // true;
165
- *
166
- * // Size decreased
167
- * console.log(trie.size); // 5;
168
- *
169
- * // Iterate through all words
170
- * const allWords = [...trie];
171
- * console.log(allWords.length); // 5;
172
- * @example
173
131
  * // Trie for autocomplete search index
174
132
  * // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
175
133
  * const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
@@ -326,6 +284,32 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
326
284
  * @remarks Time O(L), Space O(L)
327
285
  * @param word - Word to insert.
328
286
  * @returns True if the word was newly added.
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+ * @example
299
+ * // basic Trie creation and add words
300
+ * // Create a simple Trie with initial words
301
+ * const trie = new Trie(['apple', 'app', 'apply']);
302
+ *
303
+ * // Verify size
304
+ * console.log(trie.size); // 3;
305
+ *
306
+ * // Check if words exist
307
+ * console.log(trie.has('apple')); // true;
308
+ * console.log(trie.has('app')); // true;
309
+ *
310
+ * // Add a new word
311
+ * trie.add('application');
312
+ * console.log(trie.size); // 4;
329
313
  */
330
314
 
331
315
  add(word: string): boolean {
@@ -353,6 +337,21 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
353
337
  * @remarks Time O(ΣL), Space O(ΣL)
354
338
  * @param words - Iterable of strings (or raw records if toElementFn is provided).
355
339
  * @returns Array of per-word 'added' flags.
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+ * @example
349
+ * // Add multiple words
350
+ * const trie = new Trie();
351
+ * trie.addMany(['cat', 'car', 'card']);
352
+ * console.log(trie.has('cat')); // true;
353
+ * console.log(trie.has('car')); // true;
354
+ * console.log(trie.size); // 3;
356
355
  */
357
356
 
358
357
  addMany(words: Iterable<string> | Iterable<R>): boolean[] {
@@ -372,6 +371,24 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
372
371
  * @remarks Time O(L), Space O(1)
373
372
  * @param word - Word to search for.
374
373
  * @returns True if present.
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+
384
+
385
+ * @example
386
+ * // Check if a word exists
387
+ * const dict = new Trie(['apple', 'app', 'application']);
388
+ *
389
+ * console.log(dict.has('app')); // true;
390
+ * console.log(dict.has('apple')); // true;
391
+ * console.log(dict.has('ap')); // false;
375
392
  */
376
393
 
377
394
  override has(word: string): boolean {
@@ -389,6 +406,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
389
406
  * Check whether the trie is empty.
390
407
  * @remarks Time O(1), Space O(1)
391
408
  * @returns True if size is 0.
409
+
410
+
411
+
412
+
413
+
414
+
415
+
416
+
417
+ * @example
418
+ * // Check if empty
419
+ * const trie = new Trie();
420
+ * console.log(trie.isEmpty()); // true;
421
+ * trie.add('word');
422
+ * console.log(trie.isEmpty()); // false;
392
423
  */
393
424
 
394
425
  isEmpty(): boolean {
@@ -399,6 +430,19 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
399
430
  * Remove all words and reset to a fresh root.
400
431
  * @remarks Time O(1), Space O(1)
401
432
  * @returns void
433
+
434
+
435
+
436
+
437
+
438
+
439
+
440
+
441
+ * @example
442
+ * // Remove all words
443
+ * const trie = new Trie(['a', 'b', 'c']);
444
+ * trie.clear();
445
+ * console.log(trie.isEmpty()); // true;
402
446
  */
403
447
 
404
448
  clear(): void {
@@ -411,6 +455,34 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
411
455
  * @remarks Time O(L), Space O(1)
412
456
  * @param word - Word to delete.
413
457
  * @returns True if a word was removed.
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+ * @example
470
+ * // Trie delete and iteration
471
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
472
+ *
473
+ * // Delete a word
474
+ * trie.delete('card');
475
+ * console.log(trie.has('card')); // false;
476
+ *
477
+ * // Word with same prefix still exists
478
+ * console.log(trie.has('care')); // true;
479
+ *
480
+ * // Size decreased
481
+ * console.log(trie.size); // 5;
482
+ *
483
+ * // Iterate through all words
484
+ * const allWords = [...trie];
485
+ * console.log(allWords.length); // 5;
414
486
  */
415
487
 
416
488
  delete(word: string): boolean {
@@ -498,6 +570,24 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
498
570
  * @remarks Time O(L), Space O(1)
499
571
  * @param input - String to test as prefix.
500
572
  * @returns True if input matches a path from root.
573
+
574
+
575
+
576
+
577
+
578
+
579
+
580
+
581
+
582
+
583
+
584
+ * @example
585
+ * // Check if a prefix exists
586
+ * const trie = new Trie(['hello', 'help', 'world']);
587
+ *
588
+ * console.log(trie.hasPrefix('hel')); // true;
589
+ * console.log(trie.hasPrefix('wor')); // true;
590
+ * console.log(trie.hasPrefix('xyz')); // false;
501
591
  */
502
592
 
503
593
  hasPrefix(input: string): boolean {
@@ -536,6 +626,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
536
626
  * Return the longest common prefix among all words.
537
627
  * @remarks Time O(H), Space O(1)
538
628
  * @returns The longest common prefix string.
629
+
630
+
631
+
632
+
633
+
634
+
635
+
636
+
637
+
638
+
639
+
640
+ * @example
641
+ * // Find shared prefix
642
+ * const trie = new Trie(['flower', 'flow', 'flight']);
643
+ *
644
+ * console.log(trie.getLongestCommonPrefix()); // 'fl';
539
645
  */
540
646
 
541
647
  getLongestCommonPrefix(): string {
@@ -557,6 +663,28 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
557
663
  * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
558
664
  * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
559
665
  * @returns Array of collected words (at most max).
666
+
667
+
668
+
669
+
670
+
671
+
672
+
673
+
674
+
675
+
676
+
677
+ * @example
678
+ * // Trie getWords and prefix search
679
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
680
+ *
681
+ * // Get all words with prefix 'app'
682
+ * const appWords = trie.getWords('app');
683
+ * console.log(appWords); // contains 'app';
684
+ * console.log(appWords); // contains 'apple';
685
+ * console.log(appWords); // contains 'apply';
686
+ * console.log(appWords); // contains 'application';
687
+ * expect(appWords).not.toContain('apricot');
560
688
  */
561
689
 
562
690
  getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
@@ -598,6 +726,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
598
726
  * Deep clone this trie by iterating and inserting all words.
599
727
  * @remarks Time O(ΣL), Space O(ΣL)
600
728
  * @returns A new trie with the same words and options.
729
+
730
+
731
+
732
+
733
+
734
+
735
+
736
+
737
+ * @example
738
+ * // Create independent copy
739
+ * const trie = new Trie(['hello', 'world']);
740
+ * const copy = trie.clone();
741
+ * copy.delete('hello');
742
+ * console.log(trie.has('hello')); // true;
601
743
  */
602
744
 
603
745
  clone(): this {
@@ -612,6 +754,19 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
612
754
  * @param predicate - Predicate (word, index, trie) → boolean to keep word.
613
755
  * @param [thisArg] - Value for `this` inside the predicate.
614
756
  * @returns A new trie containing words that satisfy the predicate.
757
+
758
+
759
+
760
+
761
+
762
+
763
+
764
+
765
+ * @example
766
+ * // Filter words
767
+ * const trie = new Trie(['cat', 'car', 'dog', 'card']);
768
+ * const result = trie.filter(w => w.startsWith('ca'));
769
+ * console.log(result.size); // 3;
615
770
  */
616
771
 
617
772
  filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): this {
@@ -626,6 +781,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
626
781
  return results;
627
782
  }
628
783
 
784
+ /**
785
+ * Transform words
786
+
787
+
788
+
789
+
790
+
791
+
792
+
793
+
794
+ * @example
795
+ * // Transform words
796
+ * const trie = new Trie(['hello', 'world']);
797
+ * const upper = trie.map(w => w.toUpperCase());
798
+ * console.log(upper.has('HELLO')); // true;
799
+ */
629
800
  map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: any): Trie<RM>;
630
801
 
631
802
  /**
@@ -651,7 +822,7 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
651
822
  for (const x of this) {
652
823
  const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
653
824
  if (typeof v !== 'string') {
654
- throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
825
+ throw new TypeError(ERR.callbackReturnType('string', typeof v, 'Trie.map'));
655
826
  }
656
827
  newTrie.add(v);
657
828
  }
@@ -1 +1 @@
1
- export type SegmentTreeNodeVal = number;
1
+ export {};
@@ -1 +1,2 @@
1
- export type SkipLinkedListOptions = { maxLevel?: number; probability?: number };
1
+ // SkipList types are co-located with the implementation in src/data-structures/linked-list/skip-linked-list.ts
2
+ export {};
@@ -2,4 +2,11 @@ import { LinearBaseOptions } from '../base';
2
2
 
3
3
  export type DequeOptions<E, R> = {
4
4
  bucketSize?: number;
5
+
6
+ /**
7
+ * When the ratio of used buckets to total buckets falls below this threshold
8
+ * after a shift/pop, auto-compact is triggered. Set to 0 to disable.
9
+ * Default: 0.5 (compact when less than half the buckets are in use).
10
+ */
11
+ autoCompactRatio?: number;
5
12
  } & LinearBaseOptions<E, R>;
@@ -77,8 +77,10 @@ export const getMSB = (value: number): number => {
77
77
  * error message to be thrown if the index is out of bounds. By default, if no message is provided when
78
78
  * calling the `rangeCheck` function, the message "Index out of bounds." will be used.
79
79
  */
80
- export const rangeCheck = (index: number, min: number, max: number, message = 'Index out of bounds.'): void => {
81
- if (index < min || index > max) throw new RangeError(message);
80
+ export const rangeCheck = (index: number, min: number, max: number, message?: string): void => {
81
+ if (index < min || index > max) {
82
+ throw new RangeError(message ?? `Index ${index} is out of range [${min}, ${max}].`);
83
+ }
82
84
  };
83
85
 
84
86
  /**