data-structure-typed 2.4.5 → 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.
- package/CHANGELOG.md +1 -1
- package/README.md +15 -5
- package/dist/cjs/index.cjs +10240 -2079
- package/dist/cjs-legacy/index.cjs +10305 -2135
- package/dist/esm/index.mjs +10241 -2078
- package/dist/esm-legacy/index.mjs +10306 -2134
- package/dist/types/data-structures/base/iterable-element-base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +128 -51
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +210 -164
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +429 -78
- package/dist/types/data-structures/binary-tree/bst.d.ts +311 -28
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +212 -32
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +218 -152
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +1281 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1087 -201
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +858 -65
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +1133 -5
- package/dist/types/data-structures/graph/directed-graph.d.ts +219 -47
- package/dist/types/data-structures/graph/map-graph.d.ts +59 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +204 -59
- package/dist/types/data-structures/hash/hash-map.d.ts +230 -77
- package/dist/types/data-structures/heap/heap.d.ts +287 -99
- package/dist/types/data-structures/heap/max-heap.d.ts +46 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +59 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +286 -44
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +278 -65
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +415 -12
- package/dist/types/data-structures/matrix/matrix.d.ts +331 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +57 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +60 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +60 -0
- package/dist/types/data-structures/queue/deque.d.ts +272 -65
- package/dist/types/data-structures/queue/queue.d.ts +211 -42
- package/dist/types/data-structures/stack/stack.d.ts +174 -32
- package/dist/types/data-structures/trie/trie.d.ts +213 -43
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -1
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +1 -4
- package/dist/umd/data-structure-typed.js +10308 -2133
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/package.json +5 -4
- package/src/data-structures/base/iterable-element-base.ts +4 -5
- package/src/data-structures/binary-tree/avl-tree.ts +146 -51
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +316 -247
- package/src/data-structures/binary-tree/binary-tree.ts +454 -79
- package/src/data-structures/binary-tree/bst.ts +359 -34
- package/src/data-structures/binary-tree/red-black-tree.ts +309 -97
- package/src/data-structures/binary-tree/segment-tree.ts +378 -248
- package/src/data-structures/binary-tree/tree-map.ts +1403 -6
- package/src/data-structures/binary-tree/tree-multi-map.ts +1214 -211
- package/src/data-structures/binary-tree/tree-multi-set.ts +954 -65
- package/src/data-structures/binary-tree/tree-set.ts +1250 -9
- package/src/data-structures/graph/directed-graph.ts +229 -47
- package/src/data-structures/graph/map-graph.ts +59 -1
- package/src/data-structures/graph/undirected-graph.ts +213 -59
- package/src/data-structures/hash/hash-map.ts +241 -77
- package/src/data-structures/heap/heap.ts +301 -99
- package/src/data-structures/heap/max-heap.ts +46 -0
- package/src/data-structures/heap/min-heap.ts +59 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +303 -44
- package/src/data-structures/linked-list/singly-linked-list.ts +293 -65
- package/src/data-structures/linked-list/skip-linked-list.ts +707 -90
- package/src/data-structures/matrix/matrix.ts +424 -12
- package/src/data-structures/priority-queue/max-priority-queue.ts +57 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +60 -0
- package/src/data-structures/priority-queue/priority-queue.ts +60 -0
- package/src/data-structures/queue/deque.ts +287 -65
- package/src/data-structures/queue/queue.ts +223 -42
- package/src/data-structures/stack/stack.ts +184 -32
- package/src/data-structures/trie/trie.ts +225 -43
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -1
- package/src/types/data-structures/linked-list/skip-linked-list.ts +2 -1
|
@@ -113,32 +113,6 @@ export class TrieNode {
|
|
|
113
113
|
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
114
114
|
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
115
115
|
* @example
|
|
116
|
-
* // basic Trie creation and add words
|
|
117
|
-
* // Create a simple Trie with initial words
|
|
118
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
119
|
-
*
|
|
120
|
-
* // Verify size
|
|
121
|
-
* console.log(trie.size); // 3;
|
|
122
|
-
*
|
|
123
|
-
* // Check if words exist
|
|
124
|
-
* console.log(trie.has('apple')); // true;
|
|
125
|
-
* console.log(trie.has('app')); // true;
|
|
126
|
-
*
|
|
127
|
-
* // Add a new word
|
|
128
|
-
* trie.add('application');
|
|
129
|
-
* console.log(trie.size); // 4;
|
|
130
|
-
* @example
|
|
131
|
-
* // Trie getWords and prefix search
|
|
132
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
133
|
-
*
|
|
134
|
-
* // Get all words with prefix 'app'
|
|
135
|
-
* const appWords = trie.getWords('app');
|
|
136
|
-
* console.log(appWords); // contains 'app';
|
|
137
|
-
* console.log(appWords); // contains 'apple';
|
|
138
|
-
* console.log(appWords); // contains 'apply';
|
|
139
|
-
* console.log(appWords); // contains 'application';
|
|
140
|
-
* expect(appWords).not.toContain('apricot');
|
|
141
|
-
* @example
|
|
142
116
|
* // Trie isPrefix and isAbsolutePrefix checks
|
|
143
117
|
* const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
|
|
144
118
|
*
|
|
@@ -154,23 +128,6 @@ export class TrieNode {
|
|
|
154
128
|
* // Verify size
|
|
155
129
|
* console.log(trie.size); // 5;
|
|
156
130
|
* @example
|
|
157
|
-
* // Trie delete and iteration
|
|
158
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
159
|
-
*
|
|
160
|
-
* // Delete a word
|
|
161
|
-
* trie.delete('card');
|
|
162
|
-
* console.log(trie.has('card')); // false;
|
|
163
|
-
*
|
|
164
|
-
* // Word with same prefix still exists
|
|
165
|
-
* console.log(trie.has('care')); // true;
|
|
166
|
-
*
|
|
167
|
-
* // Size decreased
|
|
168
|
-
* console.log(trie.size); // 5;
|
|
169
|
-
*
|
|
170
|
-
* // Iterate through all words
|
|
171
|
-
* const allWords = [...trie];
|
|
172
|
-
* console.log(allWords.length); // 5;
|
|
173
|
-
* @example
|
|
174
131
|
* // Trie for autocomplete search index
|
|
175
132
|
* // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
|
|
176
133
|
* const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
|
|
@@ -327,6 +284,33 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
327
284
|
* @remarks Time O(L), Space O(L)
|
|
328
285
|
* @param word - Word to insert.
|
|
329
286
|
* @returns True if the word was newly added.
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
* @example
|
|
300
|
+
* // basic Trie creation and add words
|
|
301
|
+
* // Create a simple Trie with initial words
|
|
302
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
303
|
+
*
|
|
304
|
+
* // Verify size
|
|
305
|
+
* console.log(trie.size); // 3;
|
|
306
|
+
*
|
|
307
|
+
* // Check if words exist
|
|
308
|
+
* console.log(trie.has('apple')); // true;
|
|
309
|
+
* console.log(trie.has('app')); // true;
|
|
310
|
+
*
|
|
311
|
+
* // Add a new word
|
|
312
|
+
* trie.add('application');
|
|
313
|
+
* console.log(trie.size); // 4;
|
|
330
314
|
*/
|
|
331
315
|
|
|
332
316
|
add(word: string): boolean {
|
|
@@ -354,6 +338,22 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
354
338
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
355
339
|
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
356
340
|
* @returns Array of per-word 'added' flags.
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
* @example
|
|
351
|
+
* // Add multiple words
|
|
352
|
+
* const trie = new Trie();
|
|
353
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
354
|
+
* console.log(trie.has('cat')); // true;
|
|
355
|
+
* console.log(trie.has('car')); // true;
|
|
356
|
+
* console.log(trie.size); // 3;
|
|
357
357
|
*/
|
|
358
358
|
|
|
359
359
|
addMany(words: Iterable<string> | Iterable<R>): boolean[] {
|
|
@@ -373,6 +373,25 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
373
373
|
* @remarks Time O(L), Space O(1)
|
|
374
374
|
* @param word - Word to search for.
|
|
375
375
|
* @returns True if present.
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
* @example
|
|
389
|
+
* // Check if a word exists
|
|
390
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
391
|
+
*
|
|
392
|
+
* console.log(dict.has('app')); // true;
|
|
393
|
+
* console.log(dict.has('apple')); // true;
|
|
394
|
+
* console.log(dict.has('ap')); // false;
|
|
376
395
|
*/
|
|
377
396
|
|
|
378
397
|
override has(word: string): boolean {
|
|
@@ -390,6 +409,21 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
390
409
|
* Check whether the trie is empty.
|
|
391
410
|
* @remarks Time O(1), Space O(1)
|
|
392
411
|
* @returns True if size is 0.
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
* @example
|
|
422
|
+
* // Check if empty
|
|
423
|
+
* const trie = new Trie();
|
|
424
|
+
* console.log(trie.isEmpty()); // true;
|
|
425
|
+
* trie.add('word');
|
|
426
|
+
* console.log(trie.isEmpty()); // false;
|
|
393
427
|
*/
|
|
394
428
|
|
|
395
429
|
isEmpty(): boolean {
|
|
@@ -400,6 +434,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
400
434
|
* Remove all words and reset to a fresh root.
|
|
401
435
|
* @remarks Time O(1), Space O(1)
|
|
402
436
|
* @returns void
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
* @example
|
|
447
|
+
* // Remove all words
|
|
448
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
449
|
+
* trie.clear();
|
|
450
|
+
* console.log(trie.isEmpty()); // true;
|
|
403
451
|
*/
|
|
404
452
|
|
|
405
453
|
clear(): void {
|
|
@@ -412,6 +460,35 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
412
460
|
* @remarks Time O(L), Space O(1)
|
|
413
461
|
* @param word - Word to delete.
|
|
414
462
|
* @returns True if a word was removed.
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
* @example
|
|
476
|
+
* // Trie delete and iteration
|
|
477
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
478
|
+
*
|
|
479
|
+
* // Delete a word
|
|
480
|
+
* trie.delete('card');
|
|
481
|
+
* console.log(trie.has('card')); // false;
|
|
482
|
+
*
|
|
483
|
+
* // Word with same prefix still exists
|
|
484
|
+
* console.log(trie.has('care')); // true;
|
|
485
|
+
*
|
|
486
|
+
* // Size decreased
|
|
487
|
+
* console.log(trie.size); // 5;
|
|
488
|
+
*
|
|
489
|
+
* // Iterate through all words
|
|
490
|
+
* const allWords = [...trie];
|
|
491
|
+
* console.log(allWords.length); // 5;
|
|
415
492
|
*/
|
|
416
493
|
|
|
417
494
|
delete(word: string): boolean {
|
|
@@ -499,6 +576,25 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
499
576
|
* @remarks Time O(L), Space O(1)
|
|
500
577
|
* @param input - String to test as prefix.
|
|
501
578
|
* @returns True if input matches a path from root.
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
* @example
|
|
592
|
+
* // Check if a prefix exists
|
|
593
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
594
|
+
*
|
|
595
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
596
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
597
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
502
598
|
*/
|
|
503
599
|
|
|
504
600
|
hasPrefix(input: string): boolean {
|
|
@@ -537,6 +633,23 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
537
633
|
* Return the longest common prefix among all words.
|
|
538
634
|
* @remarks Time O(H), Space O(1)
|
|
539
635
|
* @returns The longest common prefix string.
|
|
636
|
+
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
* @example
|
|
649
|
+
* // Find shared prefix
|
|
650
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
651
|
+
*
|
|
652
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
540
653
|
*/
|
|
541
654
|
|
|
542
655
|
getLongestCommonPrefix(): string {
|
|
@@ -558,6 +671,29 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
558
671
|
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
559
672
|
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
560
673
|
* @returns Array of collected words (at most max).
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
* @example
|
|
687
|
+
* // Trie getWords and prefix search
|
|
688
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
689
|
+
*
|
|
690
|
+
* // Get all words with prefix 'app'
|
|
691
|
+
* const appWords = trie.getWords('app');
|
|
692
|
+
* console.log(appWords); // contains 'app';
|
|
693
|
+
* console.log(appWords); // contains 'apple';
|
|
694
|
+
* console.log(appWords); // contains 'apply';
|
|
695
|
+
* console.log(appWords); // contains 'application';
|
|
696
|
+
* expect(appWords).not.toContain('apricot');
|
|
561
697
|
*/
|
|
562
698
|
|
|
563
699
|
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
|
|
@@ -599,6 +735,21 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
599
735
|
* Deep clone this trie by iterating and inserting all words.
|
|
600
736
|
* @remarks Time O(ΣL), Space O(ΣL)
|
|
601
737
|
* @returns A new trie with the same words and options.
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
* @example
|
|
748
|
+
* // Create independent copy
|
|
749
|
+
* const trie = new Trie(['hello', 'world']);
|
|
750
|
+
* const copy = trie.clone();
|
|
751
|
+
* copy.delete('hello');
|
|
752
|
+
* console.log(trie.has('hello')); // true;
|
|
602
753
|
*/
|
|
603
754
|
|
|
604
755
|
clone(): this {
|
|
@@ -613,6 +764,20 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
613
764
|
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
614
765
|
* @param [thisArg] - Value for `this` inside the predicate.
|
|
615
766
|
* @returns A new trie containing words that satisfy the predicate.
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
775
|
+
|
|
776
|
+
* @example
|
|
777
|
+
* // Filter words
|
|
778
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
779
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
780
|
+
* console.log(result.size); // 3;
|
|
616
781
|
*/
|
|
617
782
|
|
|
618
783
|
filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): this {
|
|
@@ -627,6 +792,23 @@ export class Trie<R = any> extends IterableElementBase<string, R> {
|
|
|
627
792
|
return results;
|
|
628
793
|
}
|
|
629
794
|
|
|
795
|
+
/**
|
|
796
|
+
* Transform words
|
|
797
|
+
|
|
798
|
+
|
|
799
|
+
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
* @example
|
|
807
|
+
* // Transform words
|
|
808
|
+
* const trie = new Trie(['hello', 'world']);
|
|
809
|
+
* const upper = trie.map(w => w.toUpperCase());
|
|
810
|
+
* console.log(upper.has('HELLO')); // true;
|
|
811
|
+
*/
|
|
630
812
|
map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: any): Trie<RM>;
|
|
631
813
|
|
|
632
814
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {};
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
// SkipList types are co-located with the implementation in src/data-structures/linked-list/skip-linked-list.ts
|
|
2
|
+
export {};
|