heap-typed 2.1.1 → 2.1.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/dist/cjs/index.cjs +1020 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1012 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +11 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/heap-typed.js +1023 -0
- package/dist/umd/heap-typed.js.map +1 -0
- package/dist/umd/heap-typed.min.js +22 -0
- package/dist/umd/heap-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -12
- package/dist/index.js +0 -28
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,610 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Pablo Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.Trie = exports.TrieNode = void 0;
|
|
11
|
-
const base_1 = require("../base");
|
|
12
|
-
/**
|
|
13
|
-
* Node used by Trie to store one character and its children.
|
|
14
|
-
* @remarks Time O(1), Space O(1)
|
|
15
|
-
*/
|
|
16
|
-
class TrieNode {
|
|
17
|
-
/**
|
|
18
|
-
* Create a Trie node with a character key.
|
|
19
|
-
* @remarks Time O(1), Space O(1)
|
|
20
|
-
* @returns New TrieNode instance.
|
|
21
|
-
*/
|
|
22
|
-
constructor(key) {
|
|
23
|
-
this._key = key;
|
|
24
|
-
this._isEnd = false;
|
|
25
|
-
this._children = new Map();
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Get the character key of this node.
|
|
29
|
-
* @remarks Time O(1), Space O(1)
|
|
30
|
-
* @returns Character key string.
|
|
31
|
-
*/
|
|
32
|
-
get key() {
|
|
33
|
-
return this._key;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Set the character key of this node.
|
|
37
|
-
* @remarks Time O(1), Space O(1)
|
|
38
|
-
* @param value - New character key.
|
|
39
|
-
* @returns void
|
|
40
|
-
*/
|
|
41
|
-
set key(value) {
|
|
42
|
-
this._key = value;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Get the child map of this node.
|
|
46
|
-
* @remarks Time O(1), Space O(1)
|
|
47
|
-
* @returns Map from character to child node.
|
|
48
|
-
*/
|
|
49
|
-
get children() {
|
|
50
|
-
return this._children;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Replace the child map of this node.
|
|
54
|
-
* @remarks Time O(1), Space O(1)
|
|
55
|
-
* @param value - New map of character → node.
|
|
56
|
-
* @returns void
|
|
57
|
-
*/
|
|
58
|
-
set children(value) {
|
|
59
|
-
this._children = value;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Check whether this node marks the end of a word.
|
|
63
|
-
* @remarks Time O(1), Space O(1)
|
|
64
|
-
* @returns True if this node ends a word.
|
|
65
|
-
*/
|
|
66
|
-
get isEnd() {
|
|
67
|
-
return this._isEnd;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Mark this node as the end of a word or not.
|
|
71
|
-
* @remarks Time O(1), Space O(1)
|
|
72
|
-
* @param value - Whether this node ends a word.
|
|
73
|
-
* @returns void
|
|
74
|
-
*/
|
|
75
|
-
set isEnd(value) {
|
|
76
|
-
this._isEnd = value;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
exports.TrieNode = TrieNode;
|
|
80
|
-
/**
|
|
81
|
-
* Prefix tree (Trie) for fast prefix queries and word storage.
|
|
82
|
-
* @remarks Time O(1), Space O(1)
|
|
83
|
-
* @template R
|
|
84
|
-
* 1. Node Structure: Each node in a Trie represents a string (or a part of a string). The root node typically represents an empty string.
|
|
85
|
-
* 2. Child Node Relationship: Each node's children represent the strings that can be formed by adding one character to the string at the current node. For example, if a node represents the string 'ca', one of its children might represent 'cat'.
|
|
86
|
-
* 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
|
|
87
|
-
* 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
|
|
88
|
-
* 5. Autocomplete and Prediction: Trie can be used for implementing autocomplete and word prediction features, as it can quickly find all strings with a common prefix.
|
|
89
|
-
* 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
|
|
90
|
-
* 7. String Retrieval: For example, searching for a specific string in a large set of strings.
|
|
91
|
-
* 8. Autocomplete: Providing recommended words or phrases as a user types.
|
|
92
|
-
* 9. Spell Check: Checking the spelling of words.
|
|
93
|
-
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
94
|
-
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
|
|
95
|
-
* @example
|
|
96
|
-
* // Autocomplete: Prefix validation and checking
|
|
97
|
-
* const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
|
|
98
|
-
*
|
|
99
|
-
* // Get all completions for a prefix
|
|
100
|
-
* const gmailCompletions = autocomplete.getWords('gmail');
|
|
101
|
-
* console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
|
|
102
|
-
* @example
|
|
103
|
-
* // File System Path Operations
|
|
104
|
-
* const fileSystem = new Trie<string>([
|
|
105
|
-
* '/home/user/documents/file1.txt',
|
|
106
|
-
* '/home/user/documents/file2.txt',
|
|
107
|
-
* '/home/user/pictures/photo.jpg',
|
|
108
|
-
* '/home/user/pictures/vacation/',
|
|
109
|
-
* '/home/user/downloads'
|
|
110
|
-
* ]);
|
|
111
|
-
*
|
|
112
|
-
* // Find common directory prefix
|
|
113
|
-
* console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
|
|
114
|
-
*
|
|
115
|
-
* // List all files in a directory
|
|
116
|
-
* const documentsFiles = fileSystem.getWords('/home/user/documents/');
|
|
117
|
-
* console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
|
|
118
|
-
* @example
|
|
119
|
-
* // Autocomplete: Basic word suggestions
|
|
120
|
-
* // Create a trie for autocomplete
|
|
121
|
-
* const autocomplete = new Trie<string>([
|
|
122
|
-
* 'function',
|
|
123
|
-
* 'functional',
|
|
124
|
-
* 'functions',
|
|
125
|
-
* 'class',
|
|
126
|
-
* 'classes',
|
|
127
|
-
* 'classical',
|
|
128
|
-
* 'closure',
|
|
129
|
-
* 'const',
|
|
130
|
-
* 'constructor'
|
|
131
|
-
* ]);
|
|
132
|
-
*
|
|
133
|
-
* // Test autocomplete with different prefixes
|
|
134
|
-
* console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
|
|
135
|
-
* console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
|
|
136
|
-
* console.log(autocomplete.getWords('con')); // ['constructor', 'const']
|
|
137
|
-
*
|
|
138
|
-
* // Test with non-matching prefix
|
|
139
|
-
* console.log(autocomplete.getWords('xyz')); // []
|
|
140
|
-
* @example
|
|
141
|
-
* // Dictionary: Case-insensitive word lookup
|
|
142
|
-
* // Create a case-insensitive dictionary
|
|
143
|
-
* const dictionary = new Trie<string>([], { caseSensitive: false });
|
|
144
|
-
*
|
|
145
|
-
* // Add words with mixed casing
|
|
146
|
-
* dictionary.add('Hello');
|
|
147
|
-
* dictionary.add('WORLD');
|
|
148
|
-
* dictionary.add('JavaScript');
|
|
149
|
-
*
|
|
150
|
-
* // Test lookups with different casings
|
|
151
|
-
* console.log(dictionary.has('hello')); // true
|
|
152
|
-
* console.log(dictionary.has('HELLO')); // true
|
|
153
|
-
* console.log(dictionary.has('Hello')); // true
|
|
154
|
-
* console.log(dictionary.has('javascript')); // true
|
|
155
|
-
* console.log(dictionary.has('JAVASCRIPT')); // true
|
|
156
|
-
* @example
|
|
157
|
-
* // IP Address Routing Table
|
|
158
|
-
* // Add IP address prefixes and their corresponding routes
|
|
159
|
-
* const routes = {
|
|
160
|
-
* '192.168.1': 'LAN_SUBNET_1',
|
|
161
|
-
* '192.168.2': 'LAN_SUBNET_2',
|
|
162
|
-
* '10.0.0': 'PRIVATE_NETWORK_1',
|
|
163
|
-
* '10.0.1': 'PRIVATE_NETWORK_2'
|
|
164
|
-
* };
|
|
165
|
-
*
|
|
166
|
-
* const ipRoutingTable = new Trie<string>(Object.keys(routes));
|
|
167
|
-
*
|
|
168
|
-
* // Check IP address prefix matching
|
|
169
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
|
|
170
|
-
* console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
|
|
171
|
-
*
|
|
172
|
-
* // Validate IP address belongs to subnet
|
|
173
|
-
* const ip = '192.168.1.100';
|
|
174
|
-
* const subnet = ip.split('.').slice(0, 3).join('.');
|
|
175
|
-
* console.log(ipRoutingTable.hasPrefix(subnet)); // true
|
|
176
|
-
*/
|
|
177
|
-
class Trie extends base_1.IterableElementBase {
|
|
178
|
-
/**
|
|
179
|
-
* Create a Trie and optionally bulk-insert words.
|
|
180
|
-
* @remarks Time O(totalChars), Space O(totalChars)
|
|
181
|
-
* @param [words] - Iterable of strings (or raw records if toElementFn is provided).
|
|
182
|
-
* @param [options] - Options such as toElementFn and caseSensitive.
|
|
183
|
-
* @returns New Trie instance.
|
|
184
|
-
*/
|
|
185
|
-
constructor(words = [], options) {
|
|
186
|
-
super(options);
|
|
187
|
-
this._size = 0;
|
|
188
|
-
this._caseSensitive = true;
|
|
189
|
-
this._root = new TrieNode('');
|
|
190
|
-
if (options) {
|
|
191
|
-
const { caseSensitive } = options;
|
|
192
|
-
if (caseSensitive !== undefined)
|
|
193
|
-
this._caseSensitive = caseSensitive;
|
|
194
|
-
}
|
|
195
|
-
if (words) {
|
|
196
|
-
this.addMany(words);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Get the number of stored words.
|
|
201
|
-
* @remarks Time O(1), Space O(1)
|
|
202
|
-
* @returns Word count.
|
|
203
|
-
*/
|
|
204
|
-
get size() {
|
|
205
|
-
return this._size;
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Get whether comparisons are case-sensitive.
|
|
209
|
-
* @remarks Time O(1), Space O(1)
|
|
210
|
-
* @returns True if case-sensitive.
|
|
211
|
-
*/
|
|
212
|
-
get caseSensitive() {
|
|
213
|
-
return this._caseSensitive;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Get the root node.
|
|
217
|
-
* @remarks Time O(1), Space O(1)
|
|
218
|
-
* @returns Root TrieNode.
|
|
219
|
-
*/
|
|
220
|
-
get root() {
|
|
221
|
-
return this._root;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* (Protected) Get total count for base class iteration.
|
|
225
|
-
* @remarks Time O(1), Space O(1)
|
|
226
|
-
* @returns Total number of elements.
|
|
227
|
-
*/
|
|
228
|
-
get _total() {
|
|
229
|
-
return this._size;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Insert one word into the trie.
|
|
233
|
-
* @remarks Time O(L), Space O(L)
|
|
234
|
-
* @param word - Word to insert.
|
|
235
|
-
* @returns True if the word was newly added.
|
|
236
|
-
*/
|
|
237
|
-
add(word) {
|
|
238
|
-
word = this._caseProcess(word);
|
|
239
|
-
let cur = this.root;
|
|
240
|
-
let isNewWord = false;
|
|
241
|
-
for (const c of word) {
|
|
242
|
-
let nodeC = cur.children.get(c);
|
|
243
|
-
if (!nodeC) {
|
|
244
|
-
nodeC = new TrieNode(c);
|
|
245
|
-
cur.children.set(c, nodeC);
|
|
246
|
-
}
|
|
247
|
-
cur = nodeC;
|
|
248
|
-
}
|
|
249
|
-
if (!cur.isEnd) {
|
|
250
|
-
isNewWord = true;
|
|
251
|
-
cur.isEnd = true;
|
|
252
|
-
this._size++;
|
|
253
|
-
}
|
|
254
|
-
return isNewWord;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Insert many words from an iterable.
|
|
258
|
-
* @remarks Time O(ΣL), Space O(ΣL)
|
|
259
|
-
* @param words - Iterable of strings (or raw records if toElementFn is provided).
|
|
260
|
-
* @returns Array of per-word 'added' flags.
|
|
261
|
-
*/
|
|
262
|
-
addMany(words) {
|
|
263
|
-
const ans = [];
|
|
264
|
-
for (const word of words) {
|
|
265
|
-
if (this.toElementFn) {
|
|
266
|
-
ans.push(this.add(this.toElementFn(word)));
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
ans.push(this.add(word));
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return ans;
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Check whether a word exists.
|
|
276
|
-
* @remarks Time O(L), Space O(1)
|
|
277
|
-
* @param word - Word to search for.
|
|
278
|
-
* @returns True if present.
|
|
279
|
-
*/
|
|
280
|
-
has(word) {
|
|
281
|
-
word = this._caseProcess(word);
|
|
282
|
-
let cur = this.root;
|
|
283
|
-
for (const c of word) {
|
|
284
|
-
const nodeC = cur.children.get(c);
|
|
285
|
-
if (!nodeC)
|
|
286
|
-
return false;
|
|
287
|
-
cur = nodeC;
|
|
288
|
-
}
|
|
289
|
-
return cur.isEnd;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Check whether the trie is empty.
|
|
293
|
-
* @remarks Time O(1), Space O(1)
|
|
294
|
-
* @returns True if size is 0.
|
|
295
|
-
*/
|
|
296
|
-
isEmpty() {
|
|
297
|
-
return this._size === 0;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Remove all words and reset to a fresh root.
|
|
301
|
-
* @remarks Time O(1), Space O(1)
|
|
302
|
-
* @returns void
|
|
303
|
-
*/
|
|
304
|
-
clear() {
|
|
305
|
-
this._size = 0;
|
|
306
|
-
this._root = new TrieNode('');
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Delete one word if present.
|
|
310
|
-
* @remarks Time O(L), Space O(1)
|
|
311
|
-
* @param word - Word to delete.
|
|
312
|
-
* @returns True if a word was removed.
|
|
313
|
-
*/
|
|
314
|
-
delete(word) {
|
|
315
|
-
word = this._caseProcess(word);
|
|
316
|
-
let isDeleted = false;
|
|
317
|
-
const dfs = (cur, i) => {
|
|
318
|
-
const char = word[i];
|
|
319
|
-
const child = cur.children.get(char);
|
|
320
|
-
if (child) {
|
|
321
|
-
if (i === word.length - 1) {
|
|
322
|
-
if (child.isEnd) {
|
|
323
|
-
if (child.children.size > 0) {
|
|
324
|
-
child.isEnd = false;
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
cur.children.delete(char);
|
|
328
|
-
}
|
|
329
|
-
isDeleted = true;
|
|
330
|
-
return true;
|
|
331
|
-
}
|
|
332
|
-
return false;
|
|
333
|
-
}
|
|
334
|
-
const res = dfs(child, i + 1);
|
|
335
|
-
if (res && !cur.isEnd && child.children.size === 0) {
|
|
336
|
-
cur.children.delete(char);
|
|
337
|
-
return true;
|
|
338
|
-
}
|
|
339
|
-
return false;
|
|
340
|
-
}
|
|
341
|
-
return false;
|
|
342
|
-
};
|
|
343
|
-
dfs(this.root, 0);
|
|
344
|
-
if (isDeleted) {
|
|
345
|
-
this._size--;
|
|
346
|
-
}
|
|
347
|
-
return isDeleted;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Compute the height (max depth) of the trie.
|
|
351
|
-
* @remarks Time O(N), Space O(H)
|
|
352
|
-
* @returns Maximum depth from root to a leaf.
|
|
353
|
-
*/
|
|
354
|
-
getHeight() {
|
|
355
|
-
const startNode = this.root;
|
|
356
|
-
let maxDepth = 0;
|
|
357
|
-
if (startNode) {
|
|
358
|
-
const bfs = (node, level) => {
|
|
359
|
-
if (level > maxDepth) {
|
|
360
|
-
maxDepth = level;
|
|
361
|
-
}
|
|
362
|
-
const { children } = node;
|
|
363
|
-
if (children) {
|
|
364
|
-
for (const child of children.entries()) {
|
|
365
|
-
bfs(child[1], level + 1);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
};
|
|
369
|
-
bfs(startNode, 0);
|
|
370
|
-
}
|
|
371
|
-
return maxDepth;
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Check whether input is a proper prefix of at least one word.
|
|
375
|
-
* @remarks Time O(L), Space O(1)
|
|
376
|
-
* @param input - String to test as prefix.
|
|
377
|
-
* @returns True if input is a prefix but not a full word.
|
|
378
|
-
*/
|
|
379
|
-
hasPurePrefix(input) {
|
|
380
|
-
input = this._caseProcess(input);
|
|
381
|
-
let cur = this.root;
|
|
382
|
-
for (const c of input) {
|
|
383
|
-
const nodeC = cur.children.get(c);
|
|
384
|
-
if (!nodeC)
|
|
385
|
-
return false;
|
|
386
|
-
cur = nodeC;
|
|
387
|
-
}
|
|
388
|
-
return !cur.isEnd;
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Check whether any word starts with input.
|
|
392
|
-
* @remarks Time O(L), Space O(1)
|
|
393
|
-
* @param input - String to test as prefix.
|
|
394
|
-
* @returns True if input matches a path from root.
|
|
395
|
-
*/
|
|
396
|
-
hasPrefix(input) {
|
|
397
|
-
input = this._caseProcess(input);
|
|
398
|
-
let cur = this.root;
|
|
399
|
-
for (const c of input) {
|
|
400
|
-
const nodeC = cur.children.get(c);
|
|
401
|
-
if (!nodeC)
|
|
402
|
-
return false;
|
|
403
|
-
cur = nodeC;
|
|
404
|
-
}
|
|
405
|
-
return true;
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Check whether the trie’s longest common prefix equals input.
|
|
409
|
-
* @remarks Time O(min(H,L)), Space O(1)
|
|
410
|
-
* @param input - Candidate longest common prefix.
|
|
411
|
-
* @returns True if input equals the common prefix.
|
|
412
|
-
*/
|
|
413
|
-
hasCommonPrefix(input) {
|
|
414
|
-
input = this._caseProcess(input);
|
|
415
|
-
let commonPre = '';
|
|
416
|
-
const dfs = (cur) => {
|
|
417
|
-
commonPre += cur.key;
|
|
418
|
-
if (commonPre === input)
|
|
419
|
-
return;
|
|
420
|
-
if (cur.isEnd)
|
|
421
|
-
return;
|
|
422
|
-
if (cur && cur.children && cur.children.size === 1)
|
|
423
|
-
dfs(Array.from(cur.children.values())[0]);
|
|
424
|
-
else
|
|
425
|
-
return;
|
|
426
|
-
};
|
|
427
|
-
dfs(this.root);
|
|
428
|
-
return commonPre === input;
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Return the longest common prefix among all words.
|
|
432
|
-
* @remarks Time O(H), Space O(1)
|
|
433
|
-
* @returns The longest common prefix string.
|
|
434
|
-
*/
|
|
435
|
-
getLongestCommonPrefix() {
|
|
436
|
-
let commonPre = '';
|
|
437
|
-
const dfs = (cur) => {
|
|
438
|
-
commonPre += cur.key;
|
|
439
|
-
if (cur.isEnd)
|
|
440
|
-
return;
|
|
441
|
-
if (cur && cur.children && cur.children.size === 1)
|
|
442
|
-
dfs(Array.from(cur.children.values())[0]);
|
|
443
|
-
else
|
|
444
|
-
return;
|
|
445
|
-
};
|
|
446
|
-
dfs(this.root);
|
|
447
|
-
return commonPre;
|
|
448
|
-
}
|
|
449
|
-
/**
|
|
450
|
-
* Collect words under a prefix up to a maximum count.
|
|
451
|
-
* @remarks Time O(K·L), Space O(K·L)
|
|
452
|
-
* @param [prefix] - Prefix to match; default empty string for root.
|
|
453
|
-
* @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
|
|
454
|
-
* @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
|
|
455
|
-
* @returns Array of collected words (at most max).
|
|
456
|
-
*/
|
|
457
|
-
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
|
|
458
|
-
prefix = this._caseProcess(prefix);
|
|
459
|
-
const words = [];
|
|
460
|
-
let found = 0;
|
|
461
|
-
function dfs(node, word) {
|
|
462
|
-
for (const char of node.children.keys()) {
|
|
463
|
-
const charNode = node.children.get(char);
|
|
464
|
-
if (charNode !== undefined) {
|
|
465
|
-
dfs(charNode, word.concat(char));
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
if (node.isEnd) {
|
|
469
|
-
if (found > max - 1)
|
|
470
|
-
return;
|
|
471
|
-
words.push(word);
|
|
472
|
-
found++;
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
let startNode = this.root;
|
|
476
|
-
if (prefix) {
|
|
477
|
-
for (const c of prefix) {
|
|
478
|
-
const nodeC = startNode.children.get(c);
|
|
479
|
-
if (nodeC) {
|
|
480
|
-
startNode = nodeC;
|
|
481
|
-
}
|
|
482
|
-
else {
|
|
483
|
-
return [];
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
if (isAllWhenEmptyPrefix || startNode !== this.root)
|
|
488
|
-
dfs(startNode, prefix);
|
|
489
|
-
return words;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Deep clone this trie by iterating and inserting all words.
|
|
493
|
-
* @remarks Time O(ΣL), Space O(ΣL)
|
|
494
|
-
* @returns A new trie with the same words and options.
|
|
495
|
-
*/
|
|
496
|
-
clone() {
|
|
497
|
-
const next = this._createInstance();
|
|
498
|
-
for (const x of this)
|
|
499
|
-
next.add(x);
|
|
500
|
-
return next;
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Filter words into a new trie of the same class.
|
|
504
|
-
* @remarks Time O(ΣL), Space O(ΣL)
|
|
505
|
-
* @param predicate - Predicate (word, index, trie) → boolean to keep word.
|
|
506
|
-
* @param [thisArg] - Value for `this` inside the predicate.
|
|
507
|
-
* @returns A new trie containing words that satisfy the predicate.
|
|
508
|
-
*/
|
|
509
|
-
filter(predicate, thisArg) {
|
|
510
|
-
const results = this._createInstance();
|
|
511
|
-
let index = 0;
|
|
512
|
-
for (const word of this) {
|
|
513
|
-
if (predicate.call(thisArg, word, index, this)) {
|
|
514
|
-
results.add(word);
|
|
515
|
-
}
|
|
516
|
-
index++;
|
|
517
|
-
}
|
|
518
|
-
return results;
|
|
519
|
-
}
|
|
520
|
-
map(callback, options, thisArg) {
|
|
521
|
-
const newTrie = this._createLike([], options);
|
|
522
|
-
let i = 0;
|
|
523
|
-
for (const x of this) {
|
|
524
|
-
const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
|
|
525
|
-
if (typeof v !== 'string') {
|
|
526
|
-
throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
|
|
527
|
-
}
|
|
528
|
-
newTrie.add(v);
|
|
529
|
-
}
|
|
530
|
-
return newTrie;
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* Map words into a new trie of the same element type.
|
|
534
|
-
* @remarks Time O(ΣL), Space O(ΣL)
|
|
535
|
-
* @param callback - Mapping function (word, index, trie) → string.
|
|
536
|
-
* @param [thisArg] - Value for `this` inside the callback.
|
|
537
|
-
* @returns A new trie with mapped words.
|
|
538
|
-
*/
|
|
539
|
-
mapSame(callback, thisArg) {
|
|
540
|
-
const next = this._createInstance();
|
|
541
|
-
let i = 0;
|
|
542
|
-
for (const key of this) {
|
|
543
|
-
const mapped = thisArg === undefined ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
|
|
544
|
-
next.add(mapped);
|
|
545
|
-
}
|
|
546
|
-
return next;
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* (Protected) Create an empty instance of the same concrete class.
|
|
550
|
-
* @remarks Time O(1), Space O(1)
|
|
551
|
-
* @param [options] - Options forwarded to the constructor.
|
|
552
|
-
* @returns An empty like-kind trie instance.
|
|
553
|
-
*/
|
|
554
|
-
_createInstance(options) {
|
|
555
|
-
const Ctor = this.constructor;
|
|
556
|
-
const next = new Ctor([], Object.assign({ toElementFn: this.toElementFn, caseSensitive: this.caseSensitive }, (options !== null && options !== void 0 ? options : {})));
|
|
557
|
-
return next;
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* (Protected) Create a like-kind trie and seed it from an iterable.
|
|
561
|
-
* @remarks Time O(ΣL), Space O(ΣL)
|
|
562
|
-
* @template RM
|
|
563
|
-
* @param [elements] - Iterable used to seed the new trie.
|
|
564
|
-
* @param [options] - Options forwarded to the constructor.
|
|
565
|
-
* @returns A like-kind Trie instance.
|
|
566
|
-
*/
|
|
567
|
-
_createLike(elements = [], options) {
|
|
568
|
-
const Ctor = this.constructor;
|
|
569
|
-
return new Ctor(elements, options);
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* (Protected) Spawn an empty like-kind trie instance.
|
|
573
|
-
* @remarks Time O(1), Space O(1)
|
|
574
|
-
* @template RM
|
|
575
|
-
* @param [options] - Options forwarded to the constructor.
|
|
576
|
-
* @returns An empty like-kind Trie instance.
|
|
577
|
-
*/
|
|
578
|
-
_spawnLike(options) {
|
|
579
|
-
return this._createLike([], options);
|
|
580
|
-
}
|
|
581
|
-
/**
|
|
582
|
-
* (Protected) Iterate all words in lexicographic order of edges.
|
|
583
|
-
* @remarks Time O(ΣL), Space O(H)
|
|
584
|
-
* @returns Iterator of words.
|
|
585
|
-
*/
|
|
586
|
-
*_getIterator() {
|
|
587
|
-
function* _dfs(node, path) {
|
|
588
|
-
if (node.isEnd) {
|
|
589
|
-
yield path;
|
|
590
|
-
}
|
|
591
|
-
for (const [char, childNode] of node.children) {
|
|
592
|
-
yield* _dfs(childNode, path + char);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
yield* _dfs(this.root, '');
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* (Protected) Normalize a string according to case sensitivity.
|
|
599
|
-
* @remarks Time O(L), Space O(L)
|
|
600
|
-
* @param str - Input string to normalize.
|
|
601
|
-
* @returns Normalized string based on caseSensitive.
|
|
602
|
-
*/
|
|
603
|
-
_caseProcess(str) {
|
|
604
|
-
if (!this._caseSensitive) {
|
|
605
|
-
str = str.toLowerCase();
|
|
606
|
-
}
|
|
607
|
-
return str;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
exports.Trie = Trie;
|
package/dist/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Pablo Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export * from './data-structures/heap';
|
|
9
|
-
export * from './types/data-structures/heap';
|
|
10
|
-
export * from './types/common';
|
|
11
|
-
export * from './types/utils';
|
|
12
|
-
export * from './common';
|
package/dist/index.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
/**
|
|
18
|
-
* data-structure-typed
|
|
19
|
-
*
|
|
20
|
-
* @author Pablo Zeng
|
|
21
|
-
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
22
|
-
* @license MIT License
|
|
23
|
-
*/
|
|
24
|
-
__exportStar(require("./data-structures/heap"), exports);
|
|
25
|
-
__exportStar(require("./types/data-structures/heap"), exports);
|
|
26
|
-
__exportStar(require("./types/common"), exports);
|
|
27
|
-
__exportStar(require("./types/utils"), exports);
|
|
28
|
-
__exportStar(require("./common"), exports);
|
package/dist/interfaces/graph.js
DELETED
package/dist/interfaces/heap.js
DELETED
package/dist/interfaces/index.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./graph"), exports);
|
|
18
|
-
__exportStar(require("./binary-tree"), exports);
|
|
19
|
-
__exportStar(require("./doubly-linked-list"), exports);
|
|
20
|
-
__exportStar(require("./heap"), exports);
|
|
21
|
-
__exportStar(require("./navigator"), exports);
|
|
22
|
-
__exportStar(require("./priority-queue"), exports);
|
|
23
|
-
__exportStar(require("./segment-tree"), exports);
|
|
24
|
-
__exportStar(require("./singly-linked-list"), exports);
|