data-structure-typed 2.6.1 → 2.6.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.
- package/CHANGELOG.md +9 -1
- package/dist/cjs/binary-tree.cjs +2927 -23688
- package/dist/cjs/graph.cjs +845 -2634
- package/dist/cjs/hash.cjs +185 -616
- package/dist/cjs/heap.cjs +266 -818
- package/dist/cjs/index.cjs +5116 -31358
- package/dist/cjs/linked-list.cjs +644 -2615
- package/dist/cjs/matrix.cjs +220 -535
- package/dist/cjs/priority-queue.cjs +266 -818
- package/dist/cjs/queue.cjs +637 -2343
- package/dist/cjs/stack.cjs +124 -530
- package/dist/cjs/trie.cjs +145 -592
- package/dist/cjs-legacy/binary-tree.cjs +4352 -25113
- package/dist/cjs-legacy/graph.cjs +847 -2636
- package/dist/cjs-legacy/hash.cjs +181 -612
- package/dist/cjs-legacy/heap.cjs +266 -818
- package/dist/cjs-legacy/index.cjs +6657 -32899
- package/dist/cjs-legacy/linked-list.cjs +640 -2611
- package/dist/cjs-legacy/matrix.cjs +220 -535
- package/dist/cjs-legacy/priority-queue.cjs +266 -818
- package/dist/cjs-legacy/queue.cjs +634 -2340
- package/dist/cjs-legacy/stack.cjs +124 -530
- package/dist/cjs-legacy/trie.cjs +145 -592
- package/dist/esm/binary-tree.mjs +2927 -23688
- package/dist/esm/graph.mjs +845 -2634
- package/dist/esm/hash.mjs +185 -616
- package/dist/esm/heap.mjs +266 -818
- package/dist/esm/index.mjs +5116 -31358
- package/dist/esm/linked-list.mjs +644 -2615
- package/dist/esm/matrix.mjs +220 -535
- package/dist/esm/priority-queue.mjs +266 -818
- package/dist/esm/queue.mjs +637 -2343
- package/dist/esm/stack.mjs +124 -530
- package/dist/esm/trie.mjs +145 -592
- package/dist/esm-legacy/binary-tree.mjs +4352 -25113
- package/dist/esm-legacy/graph.mjs +847 -2636
- package/dist/esm-legacy/hash.mjs +181 -612
- package/dist/esm-legacy/heap.mjs +266 -818
- package/dist/esm-legacy/index.mjs +6657 -32899
- package/dist/esm-legacy/linked-list.mjs +640 -2611
- package/dist/esm-legacy/matrix.mjs +220 -535
- package/dist/esm-legacy/priority-queue.mjs +266 -818
- package/dist/esm-legacy/queue.mjs +634 -2340
- package/dist/esm-legacy/stack.mjs +124 -530
- package/dist/esm-legacy/trie.mjs +145 -592
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +29 -500
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +38 -563
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +230 -1212
- package/dist/types/data-structures/binary-tree/bst.d.ts +124 -1063
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +99 -854
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +62 -314
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +366 -5057
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +277 -4885
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +253 -3929
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +261 -4782
- package/dist/types/data-structures/graph/abstract-graph.d.ts +44 -44
- package/dist/types/data-structures/graph/directed-graph.d.ts +130 -527
- package/dist/types/data-structures/graph/undirected-graph.d.ts +125 -482
- package/dist/types/data-structures/hash/hash-map.d.ts +132 -562
- package/dist/types/data-structures/heap/heap.d.ts +194 -746
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +120 -809
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -733
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +193 -863
- package/dist/types/data-structures/matrix/matrix.d.ts +159 -474
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -6
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +6 -11
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +196 -804
- package/dist/types/data-structures/queue/queue.d.ts +99 -585
- package/dist/types/data-structures/stack/stack.d.ts +75 -481
- package/dist/types/data-structures/trie/trie.d.ts +98 -584
- package/dist/umd/data-structure-typed.js +6580 -32822
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/docs-site-docusaurus/docs/api/classes/AVLTree.md +162 -215
- package/docs-site-docusaurus/docs/api/classes/AVLTreeNode.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/AbstractGraph.md +47 -47
- package/docs-site-docusaurus/docs/api/classes/BST.md +155 -208
- package/docs-site-docusaurus/docs/api/classes/BSTNode.md +7 -7
- package/docs-site-docusaurus/docs/api/classes/BinaryIndexedTree.md +13 -29
- package/docs-site-docusaurus/docs/api/classes/BinaryTree.md +104 -158
- package/docs-site-docusaurus/docs/api/classes/BinaryTreeNode.md +13 -13
- package/docs-site-docusaurus/docs/api/classes/Deque.md +72 -104
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +67 -101
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedListNode.md +6 -6
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeap.md +9 -9
- package/docs-site-docusaurus/docs/api/classes/FibonacciHeapNode.md +1 -1
- package/docs-site-docusaurus/docs/api/classes/HashMap.md +52 -48
- package/docs-site-docusaurus/docs/api/classes/Heap.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +17 -17
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +38 -38
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +45 -45
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +68 -96
- package/docs-site-docusaurus/docs/api/classes/MapGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/Matrix.md +57 -52
- package/docs-site-docusaurus/docs/api/classes/MaxHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +41 -74
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +38 -66
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +46 -79
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +40 -68
- package/docs-site-docusaurus/docs/api/classes/Queue.md +54 -78
- package/docs-site-docusaurus/docs/api/classes/RedBlackTree.md +171 -225
- package/docs-site-docusaurus/docs/api/classes/SegmentTree.md +10 -22
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedList.md +68 -98
- package/docs-site-docusaurus/docs/api/classes/SinglyLinkedListNode.md +4 -4
- package/docs-site-docusaurus/docs/api/classes/SkipList.md +98 -75
- package/docs-site-docusaurus/docs/api/classes/Stack.md +33 -53
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +57 -124
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +45 -116
- package/docs-site-docusaurus/docs/api/classes/Trie.md +37 -61
- package/docs-site-docusaurus/docs/api/classes/TrieNode.md +8 -8
- package/docs-site-docusaurus/docs/api/classes/UndirectedGraph.md +59 -77
- package/package.json +1 -1
- package/src/data-structures/binary-tree/tree-multi-set.ts +15 -11
package/dist/cjs/trie.cjs
CHANGED
|
@@ -413,68 +413,26 @@ var Trie = class extends IterableElementBase {
|
|
|
413
413
|
return this._size;
|
|
414
414
|
}
|
|
415
415
|
/**
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
* @example
|
|
463
|
-
* // basic Trie creation and add words
|
|
464
|
-
* // Create a simple Trie with initial words
|
|
465
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
466
|
-
*
|
|
467
|
-
* // Verify size
|
|
468
|
-
* console.log(trie.size); // 3;
|
|
469
|
-
*
|
|
470
|
-
* // Check if words exist
|
|
471
|
-
* console.log(trie.has('apple')); // true;
|
|
472
|
-
* console.log(trie.has('app')); // true;
|
|
473
|
-
*
|
|
474
|
-
* // Add a new word
|
|
475
|
-
* trie.add('application');
|
|
476
|
-
* console.log(trie.size); // 4;
|
|
477
|
-
*/
|
|
416
|
+
* Insert one word into the trie.
|
|
417
|
+
* @remarks Time O(L), Space O(L)
|
|
418
|
+
* @param word - Word to insert.
|
|
419
|
+
* @returns True if the word was newly added.
|
|
420
|
+
* @example
|
|
421
|
+
* // basic Trie creation and add words
|
|
422
|
+
* // Create a simple Trie with initial words
|
|
423
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
424
|
+
*
|
|
425
|
+
* // Verify size
|
|
426
|
+
* console.log(trie.size); // 3;
|
|
427
|
+
*
|
|
428
|
+
* // Check if words exist
|
|
429
|
+
* console.log(trie.has('apple')); // true;
|
|
430
|
+
* console.log(trie.has('app')); // true;
|
|
431
|
+
*
|
|
432
|
+
* // Add a new word
|
|
433
|
+
* trie.add('application');
|
|
434
|
+
* console.log(trie.size); // 4;
|
|
435
|
+
*/
|
|
478
436
|
add(word) {
|
|
479
437
|
word = this._caseProcess(word);
|
|
480
438
|
let cur = this.root;
|
|
@@ -495,57 +453,18 @@ var Trie = class extends IterableElementBase {
|
|
|
495
453
|
return isNewWord;
|
|
496
454
|
}
|
|
497
455
|
/**
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
* @example
|
|
542
|
-
* // Add multiple words
|
|
543
|
-
* const trie = new Trie();
|
|
544
|
-
* trie.addMany(['cat', 'car', 'card']);
|
|
545
|
-
* console.log(trie.has('cat')); // true;
|
|
546
|
-
* console.log(trie.has('car')); // true;
|
|
547
|
-
* console.log(trie.size); // 3;
|
|
548
|
-
*/
|
|
456
|
+
* Insert many words from an iterable.
|
|
457
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
458
|
+
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
459
|
+
* @returns Array of per-word 'added' flags.
|
|
460
|
+
* @example
|
|
461
|
+
* // Add multiple words
|
|
462
|
+
* const trie = new Trie();
|
|
463
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
464
|
+
* console.log(trie.has('cat')); // true;
|
|
465
|
+
* console.log(trie.has('car')); // true;
|
|
466
|
+
* console.log(trie.size); // 3;
|
|
467
|
+
*/
|
|
549
468
|
addMany(words) {
|
|
550
469
|
const ans = [];
|
|
551
470
|
for (const word of words) {
|
|
@@ -558,60 +477,18 @@ var Trie = class extends IterableElementBase {
|
|
|
558
477
|
return ans;
|
|
559
478
|
}
|
|
560
479
|
/**
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
* @example
|
|
608
|
-
* // Check if a word exists
|
|
609
|
-
* const dict = new Trie(['apple', 'app', 'application']);
|
|
610
|
-
*
|
|
611
|
-
* console.log(dict.has('app')); // true;
|
|
612
|
-
* console.log(dict.has('apple')); // true;
|
|
613
|
-
* console.log(dict.has('ap')); // false;
|
|
614
|
-
*/
|
|
480
|
+
* Check whether a word exists.
|
|
481
|
+
* @remarks Time O(L), Space O(1)
|
|
482
|
+
* @param word - Word to search for.
|
|
483
|
+
* @returns True if present.
|
|
484
|
+
* @example
|
|
485
|
+
* // Check if a word exists
|
|
486
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
487
|
+
*
|
|
488
|
+
* console.log(dict.has('app')); // true;
|
|
489
|
+
* console.log(dict.has('apple')); // true;
|
|
490
|
+
* console.log(dict.has('ap')); // false;
|
|
491
|
+
*/
|
|
615
492
|
has(word) {
|
|
616
493
|
word = this._caseProcess(word);
|
|
617
494
|
let cur = this.root;
|
|
@@ -623,176 +500,56 @@ var Trie = class extends IterableElementBase {
|
|
|
623
500
|
return cur.isEnd;
|
|
624
501
|
}
|
|
625
502
|
/**
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
* @example
|
|
669
|
-
* // Check if empty
|
|
670
|
-
* const trie = new Trie();
|
|
671
|
-
* console.log(trie.isEmpty()); // true;
|
|
672
|
-
* trie.add('word');
|
|
673
|
-
* console.log(trie.isEmpty()); // false;
|
|
674
|
-
*/
|
|
503
|
+
* Check whether the trie is empty.
|
|
504
|
+
* @remarks Time O(1), Space O(1)
|
|
505
|
+
* @returns True if size is 0.
|
|
506
|
+
* @example
|
|
507
|
+
* // Check if empty
|
|
508
|
+
* const trie = new Trie();
|
|
509
|
+
* console.log(trie.isEmpty()); // true;
|
|
510
|
+
* trie.add('word');
|
|
511
|
+
* console.log(trie.isEmpty()); // false;
|
|
512
|
+
*/
|
|
675
513
|
isEmpty() {
|
|
676
514
|
return this._size === 0;
|
|
677
515
|
}
|
|
678
516
|
/**
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
* @example
|
|
722
|
-
* // Remove all words
|
|
723
|
-
* const trie = new Trie(['a', 'b', 'c']);
|
|
724
|
-
* trie.clear();
|
|
725
|
-
* console.log(trie.isEmpty()); // true;
|
|
726
|
-
*/
|
|
517
|
+
* Remove all words and reset to a fresh root.
|
|
518
|
+
* @remarks Time O(1), Space O(1)
|
|
519
|
+
* @returns void
|
|
520
|
+
* @example
|
|
521
|
+
* // Remove all words
|
|
522
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
523
|
+
* trie.clear();
|
|
524
|
+
* console.log(trie.isEmpty()); // true;
|
|
525
|
+
*/
|
|
727
526
|
clear() {
|
|
728
527
|
this._size = 0;
|
|
729
528
|
this._root = new TrieNode("");
|
|
730
529
|
}
|
|
731
530
|
/**
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
* @example
|
|
779
|
-
* // Trie delete and iteration
|
|
780
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
781
|
-
*
|
|
782
|
-
* // Delete a word
|
|
783
|
-
* trie.delete('card');
|
|
784
|
-
* console.log(trie.has('card')); // false;
|
|
785
|
-
*
|
|
786
|
-
* // Word with same prefix still exists
|
|
787
|
-
* console.log(trie.has('care')); // true;
|
|
788
|
-
*
|
|
789
|
-
* // Size decreased
|
|
790
|
-
* console.log(trie.size); // 5;
|
|
791
|
-
*
|
|
792
|
-
* // Iterate through all words
|
|
793
|
-
* const allWords = [...trie];
|
|
794
|
-
* console.log(allWords.length); // 5;
|
|
795
|
-
*/
|
|
531
|
+
* Delete one word if present.
|
|
532
|
+
* @remarks Time O(L), Space O(1)
|
|
533
|
+
* @param word - Word to delete.
|
|
534
|
+
* @returns True if a word was removed.
|
|
535
|
+
* @example
|
|
536
|
+
* // Trie delete and iteration
|
|
537
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
538
|
+
*
|
|
539
|
+
* // Delete a word
|
|
540
|
+
* trie.delete('card');
|
|
541
|
+
* console.log(trie.has('card')); // false;
|
|
542
|
+
*
|
|
543
|
+
* // Word with same prefix still exists
|
|
544
|
+
* console.log(trie.has('care')); // true;
|
|
545
|
+
*
|
|
546
|
+
* // Size decreased
|
|
547
|
+
* console.log(trie.size); // 5;
|
|
548
|
+
*
|
|
549
|
+
* // Iterate through all words
|
|
550
|
+
* const allWords = [...trie];
|
|
551
|
+
* console.log(allWords.length); // 5;
|
|
552
|
+
*/
|
|
796
553
|
delete(word) {
|
|
797
554
|
word = this._caseProcess(word);
|
|
798
555
|
let isDeleted = false;
|
|
@@ -868,60 +625,18 @@ var Trie = class extends IterableElementBase {
|
|
|
868
625
|
return !cur.isEnd;
|
|
869
626
|
}
|
|
870
627
|
/**
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
* @example
|
|
918
|
-
* // Check if a prefix exists
|
|
919
|
-
* const trie = new Trie(['hello', 'help', 'world']);
|
|
920
|
-
*
|
|
921
|
-
* console.log(trie.hasPrefix('hel')); // true;
|
|
922
|
-
* console.log(trie.hasPrefix('wor')); // true;
|
|
923
|
-
* console.log(trie.hasPrefix('xyz')); // false;
|
|
924
|
-
*/
|
|
628
|
+
* Check whether any word starts with input.
|
|
629
|
+
* @remarks Time O(L), Space O(1)
|
|
630
|
+
* @param input - String to test as prefix.
|
|
631
|
+
* @returns True if input matches a path from root.
|
|
632
|
+
* @example
|
|
633
|
+
* // Check if a prefix exists
|
|
634
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
635
|
+
*
|
|
636
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
637
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
638
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
639
|
+
*/
|
|
925
640
|
hasPrefix(input) {
|
|
926
641
|
input = this._caseProcess(input);
|
|
927
642
|
let cur = this.root;
|
|
@@ -952,57 +667,15 @@ var Trie = class extends IterableElementBase {
|
|
|
952
667
|
return commonPre === input;
|
|
953
668
|
}
|
|
954
669
|
/**
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
* @example
|
|
1001
|
-
* // Find shared prefix
|
|
1002
|
-
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
1003
|
-
*
|
|
1004
|
-
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
1005
|
-
*/
|
|
670
|
+
* Return the longest common prefix among all words.
|
|
671
|
+
* @remarks Time O(H), Space O(1)
|
|
672
|
+
* @returns The longest common prefix string.
|
|
673
|
+
* @example
|
|
674
|
+
* // Find shared prefix
|
|
675
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
676
|
+
*
|
|
677
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
678
|
+
*/
|
|
1006
679
|
getLongestCommonPrefix() {
|
|
1007
680
|
let commonPre = "";
|
|
1008
681
|
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
@@ -1015,66 +688,24 @@ var Trie = class extends IterableElementBase {
|
|
|
1015
688
|
return commonPre;
|
|
1016
689
|
}
|
|
1017
690
|
/**
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
* @example
|
|
1067
|
-
* // Trie getWords and prefix search
|
|
1068
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
1069
|
-
*
|
|
1070
|
-
* // Get all words with prefix 'app'
|
|
1071
|
-
* const appWords = trie.getWords('app');
|
|
1072
|
-
* console.log(appWords); // contains 'app';
|
|
1073
|
-
* console.log(appWords); // contains 'apple';
|
|
1074
|
-
* console.log(appWords); // contains 'apply';
|
|
1075
|
-
* console.log(appWords); // contains 'application';
|
|
1076
|
-
* expect(appWords).not.toContain('apricot');
|
|
1077
|
-
*/
|
|
691
|
+
* Collect words under a prefix up to a maximum count.
|
|
692
|
+
* @remarks Time O(K·L), Space O(K·L)
|
|
693
|
+
* @param [prefix] - Prefix to match; default empty string for root.
|
|
694
|
+
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
695
|
+
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
696
|
+
* @returns Array of collected words (at most max).
|
|
697
|
+
* @example
|
|
698
|
+
* // Trie getWords and prefix search
|
|
699
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
700
|
+
*
|
|
701
|
+
* // Get all words with prefix 'app'
|
|
702
|
+
* const appWords = trie.getWords('app');
|
|
703
|
+
* console.log(appWords); // contains 'app';
|
|
704
|
+
* console.log(appWords); // contains 'apple';
|
|
705
|
+
* console.log(appWords); // contains 'apply';
|
|
706
|
+
* console.log(appWords); // contains 'application';
|
|
707
|
+
* expect(appWords).not.toContain('apricot');
|
|
708
|
+
*/
|
|
1078
709
|
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
1079
710
|
prefix = this._caseProcess(prefix);
|
|
1080
711
|
const words = [];
|
|
@@ -1105,111 +736,33 @@ var Trie = class extends IterableElementBase {
|
|
|
1105
736
|
return words;
|
|
1106
737
|
}
|
|
1107
738
|
/**
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
* @example
|
|
1151
|
-
* // Create independent copy
|
|
1152
|
-
* const trie = new Trie(['hello', 'world']);
|
|
1153
|
-
* const copy = trie.clone();
|
|
1154
|
-
* copy.delete('hello');
|
|
1155
|
-
* console.log(trie.has('hello')); // true;
|
|
1156
|
-
*/
|
|
739
|
+
* Deep clone this trie by iterating and inserting all words.
|
|
740
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
741
|
+
* @returns A new trie with the same words and options.
|
|
742
|
+
* @example
|
|
743
|
+
* // Create independent copy
|
|
744
|
+
* const trie = new Trie(['hello', 'world']);
|
|
745
|
+
* const copy = trie.clone();
|
|
746
|
+
* copy.delete('hello');
|
|
747
|
+
* console.log(trie.has('hello')); // true;
|
|
748
|
+
*/
|
|
1157
749
|
clone() {
|
|
1158
750
|
const next = this._createInstance();
|
|
1159
751
|
for (const x of this) next.add(x);
|
|
1160
752
|
return next;
|
|
1161
753
|
}
|
|
1162
754
|
/**
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
* @example
|
|
1208
|
-
* // Filter words
|
|
1209
|
-
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
1210
|
-
* const result = trie.filter(w => w.startsWith('ca'));
|
|
1211
|
-
* console.log(result.size); // 3;
|
|
1212
|
-
*/
|
|
755
|
+
* Filter words into a new trie of the same class.
|
|
756
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
757
|
+
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
758
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
759
|
+
* @returns A new trie containing words that satisfy the predicate.
|
|
760
|
+
* @example
|
|
761
|
+
* // Filter words
|
|
762
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
763
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
764
|
+
* console.log(result.size); // 3;
|
|
765
|
+
*/
|
|
1213
766
|
filter(predicate, thisArg) {
|
|
1214
767
|
const results = this._createInstance();
|
|
1215
768
|
let index = 0;
|