data-structure-typed 2.6.0 → 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/.github/workflows/ci.yml +7 -2
- package/.github/workflows/release-package.yml +9 -2
- 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 +253 -101
- package/docs-site-docusaurus/docs/api/classes/DirectedGraph.md +60 -80
- package/docs-site-docusaurus/docs/api/classes/DoublyLinkedList.md +244 -114
- 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 +119 -64
- package/docs-site-docusaurus/docs/api/classes/IterableElementBase.md +84 -14
- package/docs-site-docusaurus/docs/api/classes/LinearBase.md +138 -34
- package/docs-site-docusaurus/docs/api/classes/LinearLinkedBase.md +149 -41
- package/docs-site-docusaurus/docs/api/classes/LinkedHashMap.md +23 -23
- package/docs-site-docusaurus/docs/api/classes/LinkedListQueue.md +172 -92
- 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 +117 -62
- package/docs-site-docusaurus/docs/api/classes/MaxPriorityQueue.md +120 -70
- package/docs-site-docusaurus/docs/api/classes/MinHeap.md +117 -62
- package/docs-site-docusaurus/docs/api/classes/MinPriorityQueue.md +125 -75
- package/docs-site-docusaurus/docs/api/classes/PriorityQueue.md +119 -64
- package/docs-site-docusaurus/docs/api/classes/Queue.md +158 -74
- 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 +172 -94
- 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 +112 -50
- package/docs-site-docusaurus/docs/api/classes/TreeMap.md +104 -129
- package/docs-site-docusaurus/docs/api/classes/TreeMultiMap.md +90 -121
- package/docs-site-docusaurus/docs/api/classes/TreeSet.md +298 -107
- package/docs-site-docusaurus/docs/api/classes/Trie.md +116 -58
- 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 +45 -46
- package/src/common/error.ts +15 -32
- package/src/common/index.ts +0 -3
- package/src/data-structures/base/iterable-element-base.ts +0 -3
- package/src/data-structures/base/linear-base.ts +2 -36
- package/src/data-structures/binary-tree/avl-tree.ts +31 -529
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +47 -572
- package/src/data-structures/binary-tree/binary-tree.ts +326 -1311
- package/src/data-structures/binary-tree/bst.ts +158 -1082
- package/src/data-structures/binary-tree/red-black-tree.ts +451 -1290
- package/src/data-structures/binary-tree/segment-tree.ts +73 -351
- package/src/data-structures/binary-tree/tree-map.ts +462 -5124
- package/src/data-structures/binary-tree/tree-multi-map.ts +302 -4914
- package/src/data-structures/binary-tree/tree-multi-set.ts +299 -3983
- package/src/data-structures/binary-tree/tree-set.ts +338 -4836
- package/src/data-structures/graph/abstract-graph.ts +98 -167
- package/src/data-structures/graph/directed-graph.ts +137 -562
- package/src/data-structures/graph/map-graph.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +132 -511
- package/src/data-structures/hash/hash-map.ts +154 -582
- package/src/data-structures/heap/heap.ts +200 -795
- package/src/data-structures/linked-list/doubly-linked-list.ts +121 -865
- package/src/data-structures/linked-list/singly-linked-list.ts +122 -794
- package/src/data-structures/linked-list/skip-linked-list.ts +211 -918
- package/src/data-structures/matrix/matrix.ts +179 -518
- package/src/data-structures/matrix/navigator.ts +0 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -6
- package/src/data-structures/priority-queue/min-priority-queue.ts +6 -11
- package/src/data-structures/priority-queue/priority-queue.ts +1 -2
- package/src/data-structures/queue/deque.ts +214 -882
- package/src/data-structures/queue/queue.ts +102 -625
- package/src/data-structures/stack/stack.ts +76 -505
- package/src/data-structures/trie/trie.ts +98 -628
- package/src/types/common.ts +0 -10
- package/src/types/data-structures/binary-tree/bst.ts +0 -7
- package/src/types/data-structures/binary-tree/red-black-tree.ts +0 -1
- package/src/types/data-structures/graph/abstract-graph.ts +0 -2
- package/src/types/data-structures/hash/hash-map.ts +0 -3
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/matrix/navigator.ts +0 -2
- package/src/types/utils/utils.ts +0 -7
- package/src/types/utils/validate-type.ts +0 -7
- package/src/utils/number.ts +0 -2
- package/src/utils/utils.ts +0 -5
package/dist/esm/trie.mjs
CHANGED
|
@@ -411,68 +411,26 @@ var Trie = class extends IterableElementBase {
|
|
|
411
411
|
return this._size;
|
|
412
412
|
}
|
|
413
413
|
/**
|
|
414
|
-
|
|
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
|
-
* @example
|
|
461
|
-
* // basic Trie creation and add words
|
|
462
|
-
* // Create a simple Trie with initial words
|
|
463
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
464
|
-
*
|
|
465
|
-
* // Verify size
|
|
466
|
-
* console.log(trie.size); // 3;
|
|
467
|
-
*
|
|
468
|
-
* // Check if words exist
|
|
469
|
-
* console.log(trie.has('apple')); // true;
|
|
470
|
-
* console.log(trie.has('app')); // true;
|
|
471
|
-
*
|
|
472
|
-
* // Add a new word
|
|
473
|
-
* trie.add('application');
|
|
474
|
-
* console.log(trie.size); // 4;
|
|
475
|
-
*/
|
|
414
|
+
* Insert one word into the trie.
|
|
415
|
+
* @remarks Time O(L), Space O(L)
|
|
416
|
+
* @param word - Word to insert.
|
|
417
|
+
* @returns True if the word was newly added.
|
|
418
|
+
* @example
|
|
419
|
+
* // basic Trie creation and add words
|
|
420
|
+
* // Create a simple Trie with initial words
|
|
421
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
422
|
+
*
|
|
423
|
+
* // Verify size
|
|
424
|
+
* console.log(trie.size); // 3;
|
|
425
|
+
*
|
|
426
|
+
* // Check if words exist
|
|
427
|
+
* console.log(trie.has('apple')); // true;
|
|
428
|
+
* console.log(trie.has('app')); // true;
|
|
429
|
+
*
|
|
430
|
+
* // Add a new word
|
|
431
|
+
* trie.add('application');
|
|
432
|
+
* console.log(trie.size); // 4;
|
|
433
|
+
*/
|
|
476
434
|
add(word) {
|
|
477
435
|
word = this._caseProcess(word);
|
|
478
436
|
let cur = this.root;
|
|
@@ -493,57 +451,18 @@ var Trie = class extends IterableElementBase {
|
|
|
493
451
|
return isNewWord;
|
|
494
452
|
}
|
|
495
453
|
/**
|
|
496
|
-
|
|
497
|
-
|
|
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
|
-
* @example
|
|
540
|
-
* // Add multiple words
|
|
541
|
-
* const trie = new Trie();
|
|
542
|
-
* trie.addMany(['cat', 'car', 'card']);
|
|
543
|
-
* console.log(trie.has('cat')); // true;
|
|
544
|
-
* console.log(trie.has('car')); // true;
|
|
545
|
-
* console.log(trie.size); // 3;
|
|
546
|
-
*/
|
|
454
|
+
* Insert many words from an iterable.
|
|
455
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
456
|
+
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
457
|
+
* @returns Array of per-word 'added' flags.
|
|
458
|
+
* @example
|
|
459
|
+
* // Add multiple words
|
|
460
|
+
* const trie = new Trie();
|
|
461
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
462
|
+
* console.log(trie.has('cat')); // true;
|
|
463
|
+
* console.log(trie.has('car')); // true;
|
|
464
|
+
* console.log(trie.size); // 3;
|
|
465
|
+
*/
|
|
547
466
|
addMany(words) {
|
|
548
467
|
const ans = [];
|
|
549
468
|
for (const word of words) {
|
|
@@ -556,60 +475,18 @@ var Trie = class extends IterableElementBase {
|
|
|
556
475
|
return ans;
|
|
557
476
|
}
|
|
558
477
|
/**
|
|
559
|
-
|
|
560
|
-
|
|
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
|
-
* @example
|
|
606
|
-
* // Check if a word exists
|
|
607
|
-
* const dict = new Trie(['apple', 'app', 'application']);
|
|
608
|
-
*
|
|
609
|
-
* console.log(dict.has('app')); // true;
|
|
610
|
-
* console.log(dict.has('apple')); // true;
|
|
611
|
-
* console.log(dict.has('ap')); // false;
|
|
612
|
-
*/
|
|
478
|
+
* Check whether a word exists.
|
|
479
|
+
* @remarks Time O(L), Space O(1)
|
|
480
|
+
* @param word - Word to search for.
|
|
481
|
+
* @returns True if present.
|
|
482
|
+
* @example
|
|
483
|
+
* // Check if a word exists
|
|
484
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
485
|
+
*
|
|
486
|
+
* console.log(dict.has('app')); // true;
|
|
487
|
+
* console.log(dict.has('apple')); // true;
|
|
488
|
+
* console.log(dict.has('ap')); // false;
|
|
489
|
+
*/
|
|
613
490
|
has(word) {
|
|
614
491
|
word = this._caseProcess(word);
|
|
615
492
|
let cur = this.root;
|
|
@@ -621,176 +498,56 @@ var Trie = class extends IterableElementBase {
|
|
|
621
498
|
return cur.isEnd;
|
|
622
499
|
}
|
|
623
500
|
/**
|
|
624
|
-
|
|
625
|
-
|
|
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
|
-
* @example
|
|
667
|
-
* // Check if empty
|
|
668
|
-
* const trie = new Trie();
|
|
669
|
-
* console.log(trie.isEmpty()); // true;
|
|
670
|
-
* trie.add('word');
|
|
671
|
-
* console.log(trie.isEmpty()); // false;
|
|
672
|
-
*/
|
|
501
|
+
* Check whether the trie is empty.
|
|
502
|
+
* @remarks Time O(1), Space O(1)
|
|
503
|
+
* @returns True if size is 0.
|
|
504
|
+
* @example
|
|
505
|
+
* // Check if empty
|
|
506
|
+
* const trie = new Trie();
|
|
507
|
+
* console.log(trie.isEmpty()); // true;
|
|
508
|
+
* trie.add('word');
|
|
509
|
+
* console.log(trie.isEmpty()); // false;
|
|
510
|
+
*/
|
|
673
511
|
isEmpty() {
|
|
674
512
|
return this._size === 0;
|
|
675
513
|
}
|
|
676
514
|
/**
|
|
677
|
-
|
|
678
|
-
|
|
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
|
-
* @example
|
|
720
|
-
* // Remove all words
|
|
721
|
-
* const trie = new Trie(['a', 'b', 'c']);
|
|
722
|
-
* trie.clear();
|
|
723
|
-
* console.log(trie.isEmpty()); // true;
|
|
724
|
-
*/
|
|
515
|
+
* Remove all words and reset to a fresh root.
|
|
516
|
+
* @remarks Time O(1), Space O(1)
|
|
517
|
+
* @returns void
|
|
518
|
+
* @example
|
|
519
|
+
* // Remove all words
|
|
520
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
521
|
+
* trie.clear();
|
|
522
|
+
* console.log(trie.isEmpty()); // true;
|
|
523
|
+
*/
|
|
725
524
|
clear() {
|
|
726
525
|
this._size = 0;
|
|
727
526
|
this._root = new TrieNode("");
|
|
728
527
|
}
|
|
729
528
|
/**
|
|
730
|
-
|
|
731
|
-
|
|
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
|
-
* @example
|
|
777
|
-
* // Trie delete and iteration
|
|
778
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
779
|
-
*
|
|
780
|
-
* // Delete a word
|
|
781
|
-
* trie.delete('card');
|
|
782
|
-
* console.log(trie.has('card')); // false;
|
|
783
|
-
*
|
|
784
|
-
* // Word with same prefix still exists
|
|
785
|
-
* console.log(trie.has('care')); // true;
|
|
786
|
-
*
|
|
787
|
-
* // Size decreased
|
|
788
|
-
* console.log(trie.size); // 5;
|
|
789
|
-
*
|
|
790
|
-
* // Iterate through all words
|
|
791
|
-
* const allWords = [...trie];
|
|
792
|
-
* console.log(allWords.length); // 5;
|
|
793
|
-
*/
|
|
529
|
+
* Delete one word if present.
|
|
530
|
+
* @remarks Time O(L), Space O(1)
|
|
531
|
+
* @param word - Word to delete.
|
|
532
|
+
* @returns True if a word was removed.
|
|
533
|
+
* @example
|
|
534
|
+
* // Trie delete and iteration
|
|
535
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
536
|
+
*
|
|
537
|
+
* // Delete a word
|
|
538
|
+
* trie.delete('card');
|
|
539
|
+
* console.log(trie.has('card')); // false;
|
|
540
|
+
*
|
|
541
|
+
* // Word with same prefix still exists
|
|
542
|
+
* console.log(trie.has('care')); // true;
|
|
543
|
+
*
|
|
544
|
+
* // Size decreased
|
|
545
|
+
* console.log(trie.size); // 5;
|
|
546
|
+
*
|
|
547
|
+
* // Iterate through all words
|
|
548
|
+
* const allWords = [...trie];
|
|
549
|
+
* console.log(allWords.length); // 5;
|
|
550
|
+
*/
|
|
794
551
|
delete(word) {
|
|
795
552
|
word = this._caseProcess(word);
|
|
796
553
|
let isDeleted = false;
|
|
@@ -866,60 +623,18 @@ var Trie = class extends IterableElementBase {
|
|
|
866
623
|
return !cur.isEnd;
|
|
867
624
|
}
|
|
868
625
|
/**
|
|
869
|
-
|
|
870
|
-
|
|
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
|
-
* @example
|
|
916
|
-
* // Check if a prefix exists
|
|
917
|
-
* const trie = new Trie(['hello', 'help', 'world']);
|
|
918
|
-
*
|
|
919
|
-
* console.log(trie.hasPrefix('hel')); // true;
|
|
920
|
-
* console.log(trie.hasPrefix('wor')); // true;
|
|
921
|
-
* console.log(trie.hasPrefix('xyz')); // false;
|
|
922
|
-
*/
|
|
626
|
+
* Check whether any word starts with input.
|
|
627
|
+
* @remarks Time O(L), Space O(1)
|
|
628
|
+
* @param input - String to test as prefix.
|
|
629
|
+
* @returns True if input matches a path from root.
|
|
630
|
+
* @example
|
|
631
|
+
* // Check if a prefix exists
|
|
632
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
633
|
+
*
|
|
634
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
635
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
636
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
637
|
+
*/
|
|
923
638
|
hasPrefix(input) {
|
|
924
639
|
input = this._caseProcess(input);
|
|
925
640
|
let cur = this.root;
|
|
@@ -950,57 +665,15 @@ var Trie = class extends IterableElementBase {
|
|
|
950
665
|
return commonPre === input;
|
|
951
666
|
}
|
|
952
667
|
/**
|
|
953
|
-
|
|
954
|
-
|
|
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
|
-
* @example
|
|
999
|
-
* // Find shared prefix
|
|
1000
|
-
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
1001
|
-
*
|
|
1002
|
-
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
1003
|
-
*/
|
|
668
|
+
* Return the longest common prefix among all words.
|
|
669
|
+
* @remarks Time O(H), Space O(1)
|
|
670
|
+
* @returns The longest common prefix string.
|
|
671
|
+
* @example
|
|
672
|
+
* // Find shared prefix
|
|
673
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
674
|
+
*
|
|
675
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
676
|
+
*/
|
|
1004
677
|
getLongestCommonPrefix() {
|
|
1005
678
|
let commonPre = "";
|
|
1006
679
|
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
@@ -1013,66 +686,24 @@ var Trie = class extends IterableElementBase {
|
|
|
1013
686
|
return commonPre;
|
|
1014
687
|
}
|
|
1015
688
|
/**
|
|
1016
|
-
|
|
1017
|
-
|
|
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
|
-
* @example
|
|
1065
|
-
* // Trie getWords and prefix search
|
|
1066
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
1067
|
-
*
|
|
1068
|
-
* // Get all words with prefix 'app'
|
|
1069
|
-
* const appWords = trie.getWords('app');
|
|
1070
|
-
* console.log(appWords); // contains 'app';
|
|
1071
|
-
* console.log(appWords); // contains 'apple';
|
|
1072
|
-
* console.log(appWords); // contains 'apply';
|
|
1073
|
-
* console.log(appWords); // contains 'application';
|
|
1074
|
-
* expect(appWords).not.toContain('apricot');
|
|
1075
|
-
*/
|
|
689
|
+
* Collect words under a prefix up to a maximum count.
|
|
690
|
+
* @remarks Time O(K·L), Space O(K·L)
|
|
691
|
+
* @param [prefix] - Prefix to match; default empty string for root.
|
|
692
|
+
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
693
|
+
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
694
|
+
* @returns Array of collected words (at most max).
|
|
695
|
+
* @example
|
|
696
|
+
* // Trie getWords and prefix search
|
|
697
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
698
|
+
*
|
|
699
|
+
* // Get all words with prefix 'app'
|
|
700
|
+
* const appWords = trie.getWords('app');
|
|
701
|
+
* console.log(appWords); // contains 'app';
|
|
702
|
+
* console.log(appWords); // contains 'apple';
|
|
703
|
+
* console.log(appWords); // contains 'apply';
|
|
704
|
+
* console.log(appWords); // contains 'application';
|
|
705
|
+
* expect(appWords).not.toContain('apricot');
|
|
706
|
+
*/
|
|
1076
707
|
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
1077
708
|
prefix = this._caseProcess(prefix);
|
|
1078
709
|
const words = [];
|
|
@@ -1103,111 +734,33 @@ var Trie = class extends IterableElementBase {
|
|
|
1103
734
|
return words;
|
|
1104
735
|
}
|
|
1105
736
|
/**
|
|
1106
|
-
|
|
1107
|
-
|
|
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
|
-
* @example
|
|
1149
|
-
* // Create independent copy
|
|
1150
|
-
* const trie = new Trie(['hello', 'world']);
|
|
1151
|
-
* const copy = trie.clone();
|
|
1152
|
-
* copy.delete('hello');
|
|
1153
|
-
* console.log(trie.has('hello')); // true;
|
|
1154
|
-
*/
|
|
737
|
+
* Deep clone this trie by iterating and inserting all words.
|
|
738
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
739
|
+
* @returns A new trie with the same words and options.
|
|
740
|
+
* @example
|
|
741
|
+
* // Create independent copy
|
|
742
|
+
* const trie = new Trie(['hello', 'world']);
|
|
743
|
+
* const copy = trie.clone();
|
|
744
|
+
* copy.delete('hello');
|
|
745
|
+
* console.log(trie.has('hello')); // true;
|
|
746
|
+
*/
|
|
1155
747
|
clone() {
|
|
1156
748
|
const next = this._createInstance();
|
|
1157
749
|
for (const x of this) next.add(x);
|
|
1158
750
|
return next;
|
|
1159
751
|
}
|
|
1160
752
|
/**
|
|
1161
|
-
|
|
1162
|
-
|
|
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
|
-
* @example
|
|
1206
|
-
* // Filter words
|
|
1207
|
-
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
1208
|
-
* const result = trie.filter(w => w.startsWith('ca'));
|
|
1209
|
-
* console.log(result.size); // 3;
|
|
1210
|
-
*/
|
|
753
|
+
* Filter words into a new trie of the same class.
|
|
754
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
755
|
+
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
756
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
757
|
+
* @returns A new trie containing words that satisfy the predicate.
|
|
758
|
+
* @example
|
|
759
|
+
* // Filter words
|
|
760
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
761
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
762
|
+
* console.log(result.size); // 3;
|
|
763
|
+
*/
|
|
1211
764
|
filter(predicate, thisArg) {
|
|
1212
765
|
const results = this._createInstance();
|
|
1213
766
|
let index = 0;
|