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-legacy/trie.mjs
CHANGED
|
@@ -408,68 +408,26 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
408
408
|
return this._size;
|
|
409
409
|
}
|
|
410
410
|
/**
|
|
411
|
-
|
|
412
|
-
|
|
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
|
-
* @example
|
|
458
|
-
* // basic Trie creation and add words
|
|
459
|
-
* // Create a simple Trie with initial words
|
|
460
|
-
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
461
|
-
*
|
|
462
|
-
* // Verify size
|
|
463
|
-
* console.log(trie.size); // 3;
|
|
464
|
-
*
|
|
465
|
-
* // Check if words exist
|
|
466
|
-
* console.log(trie.has('apple')); // true;
|
|
467
|
-
* console.log(trie.has('app')); // true;
|
|
468
|
-
*
|
|
469
|
-
* // Add a new word
|
|
470
|
-
* trie.add('application');
|
|
471
|
-
* console.log(trie.size); // 4;
|
|
472
|
-
*/
|
|
411
|
+
* Insert one word into the trie.
|
|
412
|
+
* @remarks Time O(L), Space O(L)
|
|
413
|
+
* @param word - Word to insert.
|
|
414
|
+
* @returns True if the word was newly added.
|
|
415
|
+
* @example
|
|
416
|
+
* // basic Trie creation and add words
|
|
417
|
+
* // Create a simple Trie with initial words
|
|
418
|
+
* const trie = new Trie(['apple', 'app', 'apply']);
|
|
419
|
+
*
|
|
420
|
+
* // Verify size
|
|
421
|
+
* console.log(trie.size); // 3;
|
|
422
|
+
*
|
|
423
|
+
* // Check if words exist
|
|
424
|
+
* console.log(trie.has('apple')); // true;
|
|
425
|
+
* console.log(trie.has('app')); // true;
|
|
426
|
+
*
|
|
427
|
+
* // Add a new word
|
|
428
|
+
* trie.add('application');
|
|
429
|
+
* console.log(trie.size); // 4;
|
|
430
|
+
*/
|
|
473
431
|
add(word) {
|
|
474
432
|
word = this._caseProcess(word);
|
|
475
433
|
let cur = this.root;
|
|
@@ -490,57 +448,18 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
490
448
|
return isNewWord;
|
|
491
449
|
}
|
|
492
450
|
/**
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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
|
-
* @example
|
|
537
|
-
* // Add multiple words
|
|
538
|
-
* const trie = new Trie();
|
|
539
|
-
* trie.addMany(['cat', 'car', 'card']);
|
|
540
|
-
* console.log(trie.has('cat')); // true;
|
|
541
|
-
* console.log(trie.has('car')); // true;
|
|
542
|
-
* console.log(trie.size); // 3;
|
|
543
|
-
*/
|
|
451
|
+
* Insert many words from an iterable.
|
|
452
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
453
|
+
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
454
|
+
* @returns Array of per-word 'added' flags.
|
|
455
|
+
* @example
|
|
456
|
+
* // Add multiple words
|
|
457
|
+
* const trie = new Trie();
|
|
458
|
+
* trie.addMany(['cat', 'car', 'card']);
|
|
459
|
+
* console.log(trie.has('cat')); // true;
|
|
460
|
+
* console.log(trie.has('car')); // true;
|
|
461
|
+
* console.log(trie.size); // 3;
|
|
462
|
+
*/
|
|
544
463
|
addMany(words) {
|
|
545
464
|
const ans = [];
|
|
546
465
|
for (const word of words) {
|
|
@@ -553,60 +472,18 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
553
472
|
return ans;
|
|
554
473
|
}
|
|
555
474
|
/**
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
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
|
-
* @example
|
|
603
|
-
* // Check if a word exists
|
|
604
|
-
* const dict = new Trie(['apple', 'app', 'application']);
|
|
605
|
-
*
|
|
606
|
-
* console.log(dict.has('app')); // true;
|
|
607
|
-
* console.log(dict.has('apple')); // true;
|
|
608
|
-
* console.log(dict.has('ap')); // false;
|
|
609
|
-
*/
|
|
475
|
+
* Check whether a word exists.
|
|
476
|
+
* @remarks Time O(L), Space O(1)
|
|
477
|
+
* @param word - Word to search for.
|
|
478
|
+
* @returns True if present.
|
|
479
|
+
* @example
|
|
480
|
+
* // Check if a word exists
|
|
481
|
+
* const dict = new Trie(['apple', 'app', 'application']);
|
|
482
|
+
*
|
|
483
|
+
* console.log(dict.has('app')); // true;
|
|
484
|
+
* console.log(dict.has('apple')); // true;
|
|
485
|
+
* console.log(dict.has('ap')); // false;
|
|
486
|
+
*/
|
|
610
487
|
has(word) {
|
|
611
488
|
word = this._caseProcess(word);
|
|
612
489
|
let cur = this.root;
|
|
@@ -618,176 +495,56 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
618
495
|
return cur.isEnd;
|
|
619
496
|
}
|
|
620
497
|
/**
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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
|
-
* @example
|
|
664
|
-
* // Check if empty
|
|
665
|
-
* const trie = new Trie();
|
|
666
|
-
* console.log(trie.isEmpty()); // true;
|
|
667
|
-
* trie.add('word');
|
|
668
|
-
* console.log(trie.isEmpty()); // false;
|
|
669
|
-
*/
|
|
498
|
+
* Check whether the trie is empty.
|
|
499
|
+
* @remarks Time O(1), Space O(1)
|
|
500
|
+
* @returns True if size is 0.
|
|
501
|
+
* @example
|
|
502
|
+
* // Check if empty
|
|
503
|
+
* const trie = new Trie();
|
|
504
|
+
* console.log(trie.isEmpty()); // true;
|
|
505
|
+
* trie.add('word');
|
|
506
|
+
* console.log(trie.isEmpty()); // false;
|
|
507
|
+
*/
|
|
670
508
|
isEmpty() {
|
|
671
509
|
return this._size === 0;
|
|
672
510
|
}
|
|
673
511
|
/**
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
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
|
-
* @example
|
|
717
|
-
* // Remove all words
|
|
718
|
-
* const trie = new Trie(['a', 'b', 'c']);
|
|
719
|
-
* trie.clear();
|
|
720
|
-
* console.log(trie.isEmpty()); // true;
|
|
721
|
-
*/
|
|
512
|
+
* Remove all words and reset to a fresh root.
|
|
513
|
+
* @remarks Time O(1), Space O(1)
|
|
514
|
+
* @returns void
|
|
515
|
+
* @example
|
|
516
|
+
* // Remove all words
|
|
517
|
+
* const trie = new Trie(['a', 'b', 'c']);
|
|
518
|
+
* trie.clear();
|
|
519
|
+
* console.log(trie.isEmpty()); // true;
|
|
520
|
+
*/
|
|
722
521
|
clear() {
|
|
723
522
|
this._size = 0;
|
|
724
523
|
this._root = new TrieNode("");
|
|
725
524
|
}
|
|
726
525
|
/**
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
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
|
-
* @example
|
|
774
|
-
* // Trie delete and iteration
|
|
775
|
-
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
776
|
-
*
|
|
777
|
-
* // Delete a word
|
|
778
|
-
* trie.delete('card');
|
|
779
|
-
* console.log(trie.has('card')); // false;
|
|
780
|
-
*
|
|
781
|
-
* // Word with same prefix still exists
|
|
782
|
-
* console.log(trie.has('care')); // true;
|
|
783
|
-
*
|
|
784
|
-
* // Size decreased
|
|
785
|
-
* console.log(trie.size); // 5;
|
|
786
|
-
*
|
|
787
|
-
* // Iterate through all words
|
|
788
|
-
* const allWords = [...trie];
|
|
789
|
-
* console.log(allWords.length); // 5;
|
|
790
|
-
*/
|
|
526
|
+
* Delete one word if present.
|
|
527
|
+
* @remarks Time O(L), Space O(1)
|
|
528
|
+
* @param word - Word to delete.
|
|
529
|
+
* @returns True if a word was removed.
|
|
530
|
+
* @example
|
|
531
|
+
* // Trie delete and iteration
|
|
532
|
+
* const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
|
|
533
|
+
*
|
|
534
|
+
* // Delete a word
|
|
535
|
+
* trie.delete('card');
|
|
536
|
+
* console.log(trie.has('card')); // false;
|
|
537
|
+
*
|
|
538
|
+
* // Word with same prefix still exists
|
|
539
|
+
* console.log(trie.has('care')); // true;
|
|
540
|
+
*
|
|
541
|
+
* // Size decreased
|
|
542
|
+
* console.log(trie.size); // 5;
|
|
543
|
+
*
|
|
544
|
+
* // Iterate through all words
|
|
545
|
+
* const allWords = [...trie];
|
|
546
|
+
* console.log(allWords.length); // 5;
|
|
547
|
+
*/
|
|
791
548
|
delete(word) {
|
|
792
549
|
word = this._caseProcess(word);
|
|
793
550
|
let isDeleted = false;
|
|
@@ -863,60 +620,18 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
863
620
|
return !cur.isEnd;
|
|
864
621
|
}
|
|
865
622
|
/**
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
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
|
-
* @example
|
|
913
|
-
* // Check if a prefix exists
|
|
914
|
-
* const trie = new Trie(['hello', 'help', 'world']);
|
|
915
|
-
*
|
|
916
|
-
* console.log(trie.hasPrefix('hel')); // true;
|
|
917
|
-
* console.log(trie.hasPrefix('wor')); // true;
|
|
918
|
-
* console.log(trie.hasPrefix('xyz')); // false;
|
|
919
|
-
*/
|
|
623
|
+
* Check whether any word starts with input.
|
|
624
|
+
* @remarks Time O(L), Space O(1)
|
|
625
|
+
* @param input - String to test as prefix.
|
|
626
|
+
* @returns True if input matches a path from root.
|
|
627
|
+
* @example
|
|
628
|
+
* // Check if a prefix exists
|
|
629
|
+
* const trie = new Trie(['hello', 'help', 'world']);
|
|
630
|
+
*
|
|
631
|
+
* console.log(trie.hasPrefix('hel')); // true;
|
|
632
|
+
* console.log(trie.hasPrefix('wor')); // true;
|
|
633
|
+
* console.log(trie.hasPrefix('xyz')); // false;
|
|
634
|
+
*/
|
|
920
635
|
hasPrefix(input) {
|
|
921
636
|
input = this._caseProcess(input);
|
|
922
637
|
let cur = this.root;
|
|
@@ -947,57 +662,15 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
947
662
|
return commonPre === input;
|
|
948
663
|
}
|
|
949
664
|
/**
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
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
|
-
* @example
|
|
996
|
-
* // Find shared prefix
|
|
997
|
-
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
998
|
-
*
|
|
999
|
-
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
1000
|
-
*/
|
|
665
|
+
* Return the longest common prefix among all words.
|
|
666
|
+
* @remarks Time O(H), Space O(1)
|
|
667
|
+
* @returns The longest common prefix string.
|
|
668
|
+
* @example
|
|
669
|
+
* // Find shared prefix
|
|
670
|
+
* const trie = new Trie(['flower', 'flow', 'flight']);
|
|
671
|
+
*
|
|
672
|
+
* console.log(trie.getLongestCommonPrefix()); // 'fl';
|
|
673
|
+
*/
|
|
1001
674
|
getLongestCommonPrefix() {
|
|
1002
675
|
let commonPre = "";
|
|
1003
676
|
const dfs = /* @__PURE__ */ __name((cur) => {
|
|
@@ -1010,66 +683,24 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
1010
683
|
return commonPre;
|
|
1011
684
|
}
|
|
1012
685
|
/**
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
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
|
-
* @example
|
|
1062
|
-
* // Trie getWords and prefix search
|
|
1063
|
-
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
1064
|
-
*
|
|
1065
|
-
* // Get all words with prefix 'app'
|
|
1066
|
-
* const appWords = trie.getWords('app');
|
|
1067
|
-
* console.log(appWords); // contains 'app';
|
|
1068
|
-
* console.log(appWords); // contains 'apple';
|
|
1069
|
-
* console.log(appWords); // contains 'apply';
|
|
1070
|
-
* console.log(appWords); // contains 'application';
|
|
1071
|
-
* expect(appWords).not.toContain('apricot');
|
|
1072
|
-
*/
|
|
686
|
+
* Collect words under a prefix up to a maximum count.
|
|
687
|
+
* @remarks Time O(K·L), Space O(K·L)
|
|
688
|
+
* @param [prefix] - Prefix to match; default empty string for root.
|
|
689
|
+
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
690
|
+
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
691
|
+
* @returns Array of collected words (at most max).
|
|
692
|
+
* @example
|
|
693
|
+
* // Trie getWords and prefix search
|
|
694
|
+
* const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
|
|
695
|
+
*
|
|
696
|
+
* // Get all words with prefix 'app'
|
|
697
|
+
* const appWords = trie.getWords('app');
|
|
698
|
+
* console.log(appWords); // contains 'app';
|
|
699
|
+
* console.log(appWords); // contains 'apple';
|
|
700
|
+
* console.log(appWords); // contains 'apply';
|
|
701
|
+
* console.log(appWords); // contains 'application';
|
|
702
|
+
* expect(appWords).not.toContain('apricot');
|
|
703
|
+
*/
|
|
1073
704
|
getWords(prefix = "", max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
1074
705
|
prefix = this._caseProcess(prefix);
|
|
1075
706
|
const words = [];
|
|
@@ -1100,111 +731,33 @@ var _Trie = class _Trie extends IterableElementBase {
|
|
|
1100
731
|
return words;
|
|
1101
732
|
}
|
|
1102
733
|
/**
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
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
|
-
* @example
|
|
1146
|
-
* // Create independent copy
|
|
1147
|
-
* const trie = new Trie(['hello', 'world']);
|
|
1148
|
-
* const copy = trie.clone();
|
|
1149
|
-
* copy.delete('hello');
|
|
1150
|
-
* console.log(trie.has('hello')); // true;
|
|
1151
|
-
*/
|
|
734
|
+
* Deep clone this trie by iterating and inserting all words.
|
|
735
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
736
|
+
* @returns A new trie with the same words and options.
|
|
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;
|
|
743
|
+
*/
|
|
1152
744
|
clone() {
|
|
1153
745
|
const next = this._createInstance();
|
|
1154
746
|
for (const x of this) next.add(x);
|
|
1155
747
|
return next;
|
|
1156
748
|
}
|
|
1157
749
|
/**
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
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
|
-
* @example
|
|
1203
|
-
* // Filter words
|
|
1204
|
-
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
1205
|
-
* const result = trie.filter(w => w.startsWith('ca'));
|
|
1206
|
-
* console.log(result.size); // 3;
|
|
1207
|
-
*/
|
|
750
|
+
* Filter words into a new trie of the same class.
|
|
751
|
+
* @remarks Time O(ΣL), Space O(ΣL)
|
|
752
|
+
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
753
|
+
* @param [thisArg] - Value for `this` inside the predicate.
|
|
754
|
+
* @returns A new trie containing words that satisfy the predicate.
|
|
755
|
+
* @example
|
|
756
|
+
* // Filter words
|
|
757
|
+
* const trie = new Trie(['cat', 'car', 'dog', 'card']);
|
|
758
|
+
* const result = trie.filter(w => w.startsWith('ca'));
|
|
759
|
+
* console.log(result.size); // 3;
|
|
760
|
+
*/
|
|
1208
761
|
filter(predicate, thisArg) {
|
|
1209
762
|
const results = this._createInstance();
|
|
1210
763
|
let index = 0;
|