trie-typed 1.19.3 → 1.19.5
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/README.md +10 -204
- package/dist/index.d.ts +8 -1
- package/dist/index.js +11 -15
- package/package.json +2 -2
- package/tsconfig.json +6 -4
- package/dist/trie.d.ts +0 -61
- package/dist/trie.js +0 -317
package/README.md
CHANGED
|
@@ -1,180 +1,30 @@
|
|
|
1
1
|
# What
|
|
2
|
-
|
|
3
2
|
## Brief
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Binary Tree, Binary Search Tree (BST), AVL Tree, Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Linked List, Singly Linked List, Doubly Linked List, Queue, Object Deque, Array Deque, Stack, Hash, Coordinate Set, Coordinate Map, Heap, Priority Queue, Max Priority Queue, Min Priority Queue, Trie
|
|
7
|
-
|
|
8
|
-
## Algorithms list only a few out, you can discover more in API docs
|
|
3
|
+
This is a standalone Trie data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete [data-structure-typed](https://www.npmjs.com/package/data-structure-typed) package
|
|
9
4
|
|
|
10
|
-
DFS, DFSIterative, BFS, morris, Bellman-Ford Algorithm, Dijkstra's Algorithm, Floyd-Warshall Algorithm, Tarjan's Algorithm
|
|
11
|
-
|
|
12
|
-
## Code design
|
|
13
|
-
By strictly adhering to object-oriented design (BinaryTree -> BST -> AVLTree -> TreeMultiset), you can seamlessly inherit the existing data structures to implement the customized ones you need. Object-oriented design stands as the optimal approach to data structure design.
|
|
14
5
|
|
|
15
6
|
# How
|
|
16
7
|
|
|
17
8
|
## install
|
|
18
|
-
###
|
|
19
|
-
|
|
9
|
+
### npm
|
|
20
10
|
```bash
|
|
21
|
-
|
|
11
|
+
npm i trie-typed
|
|
22
12
|
```
|
|
23
|
-
|
|
24
|
-
### npm
|
|
25
|
-
|
|
13
|
+
### yarn
|
|
26
14
|
```bash
|
|
27
|
-
|
|
15
|
+
yarn add trie-typed
|
|
28
16
|
```
|
|
29
17
|
|
|
30
|
-
###
|
|
31
|
-
|
|
18
|
+
### snippet
|
|
32
19
|
#### TS
|
|
33
20
|
```typescript
|
|
34
|
-
import {BST, BSTNode} from 'data-structure-typed';
|
|
35
|
-
|
|
36
|
-
const bst = new BST();
|
|
37
|
-
bst.add(11);
|
|
38
|
-
bst.add(3);
|
|
39
|
-
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
40
|
-
bst.size === 16; // true
|
|
41
|
-
bst.has(6); // true
|
|
42
|
-
const node6 = bst.get(6);
|
|
43
|
-
bst.getHeight(6) === 2; // true
|
|
44
|
-
bst.getHeight() === 5; // true
|
|
45
|
-
bst.getDepth(6) === 3; // true
|
|
46
|
-
const leftMost = bst.getLeftMost();
|
|
47
|
-
leftMost?.id === 1; // true
|
|
48
|
-
expect(leftMost?.id).toBe(1);
|
|
49
|
-
bst.remove(6);
|
|
50
|
-
bst.get(6); // null
|
|
51
|
-
bst.isAVLBalanced(); // true or false
|
|
52
|
-
const bfsIDs = bst.BFS();
|
|
53
|
-
bfsIDs[0] === 11; // true
|
|
54
|
-
expect(bfsIDs[0]).toBe(11);
|
|
55
|
-
|
|
56
|
-
const objBST = new BST<BSTNode<{ id: number, keyA: number }>>();
|
|
57
|
-
objBST.add(11, {id: 11, keyA: 11});
|
|
58
|
-
objBST.add(3, {id: 3, keyA: 3});
|
|
59
|
-
|
|
60
|
-
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
61
|
-
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
62
|
-
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
63
|
-
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
64
|
-
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
65
|
-
|
|
66
|
-
objBST.remove(11);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const avlTree = new AVLTree();
|
|
70
|
-
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
71
|
-
avlTree.isAVLBalanced(); // true
|
|
72
|
-
avlTree.remove(10);
|
|
73
|
-
avlTree.isAVLBalanced(); // true
|
|
74
21
|
|
|
75
22
|
```
|
|
76
23
|
#### JS
|
|
77
24
|
```javascript
|
|
78
|
-
const {BST, BSTNode} = require('data-structure-typed');
|
|
79
|
-
|
|
80
|
-
const bst = new BST();
|
|
81
|
-
bst.add(11);
|
|
82
|
-
bst.add(3);
|
|
83
|
-
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
84
|
-
bst.size === 16; // true
|
|
85
|
-
bst.has(6); // true
|
|
86
|
-
const node6 = bst.get(6);
|
|
87
|
-
bst.getHeight(6) === 2; // true
|
|
88
|
-
bst.getHeight() === 5; // true
|
|
89
|
-
bst.getDepth(6) === 3; // true
|
|
90
|
-
const leftMost = bst.getLeftMost();
|
|
91
|
-
leftMost?.id === 1; // true
|
|
92
|
-
expect(leftMost?.id).toBe(1);
|
|
93
|
-
bst.remove(6);
|
|
94
|
-
bst.get(6); // null
|
|
95
|
-
bst.isAVLBalanced(); // true or false
|
|
96
|
-
const bfsIDs = bst.BFS();
|
|
97
|
-
bfsIDs[0] === 11; // true
|
|
98
|
-
expect(bfsIDs[0]).toBe(11);
|
|
99
|
-
|
|
100
|
-
const objBST = new BST();
|
|
101
|
-
objBST.add(11, {id: 11, keyA: 11});
|
|
102
|
-
objBST.add(3, {id: 3, keyA: 3});
|
|
103
|
-
|
|
104
|
-
objBST.addMany([{id: 15, keyA: 15}, {id: 1, keyA: 1}, {id: 8, keyA: 8},
|
|
105
|
-
{id: 13, keyA: 13}, {id: 16, keyA: 16}, {id: 2, keyA: 2},
|
|
106
|
-
{id: 6, keyA: 6}, {id: 9, keyA: 9}, {id: 12, keyA: 12},
|
|
107
|
-
{id: 14, keyA: 14}, {id: 4, keyA: 4}, {id: 7, keyA: 7},
|
|
108
|
-
{id: 10, keyA: 10}, {id: 5, keyA: 5}]);
|
|
109
|
-
|
|
110
|
-
objBST.remove(11);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const avlTree = new AVLTree();
|
|
114
|
-
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5])
|
|
115
|
-
avlTree.isAVLBalanced(); // true
|
|
116
|
-
avlTree.remove(10);
|
|
117
|
-
avlTree.isAVLBalanced(); // true
|
|
118
|
-
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Directed Graph simple snippet
|
|
122
25
|
|
|
123
|
-
#### TS or JS
|
|
124
|
-
```typescript
|
|
125
|
-
import {DirectedGraph} from 'data-structure-typed';
|
|
126
|
-
|
|
127
|
-
const graph = new DirectedGraph();
|
|
128
|
-
|
|
129
|
-
graph.addVertex('A');
|
|
130
|
-
graph.addVertex('B');
|
|
131
|
-
|
|
132
|
-
graph.hasVertex('A'); // true
|
|
133
|
-
graph.hasVertex('B'); // true
|
|
134
|
-
graph.hasVertex('C'); // false
|
|
135
|
-
|
|
136
|
-
graph.addEdge('A', 'B');
|
|
137
|
-
graph.hasEdge('A', 'B'); // true
|
|
138
|
-
graph.hasEdge('B', 'A'); // false
|
|
139
|
-
|
|
140
|
-
graph.removeEdgeSrcToDest('A', 'B');
|
|
141
|
-
graph.hasEdge('A', 'B'); // false
|
|
142
|
-
|
|
143
|
-
graph.addVertex('C');
|
|
144
|
-
|
|
145
|
-
graph.addEdge('A', 'B');
|
|
146
|
-
graph.addEdge('B', 'C');
|
|
147
|
-
|
|
148
|
-
const topologicalOrderIds = graph.topologicalSort(); // ['A', 'B', 'C']
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Undirected Graph snippet
|
|
152
|
-
|
|
153
|
-
#### TS or JS
|
|
154
|
-
```typescript
|
|
155
|
-
import {UndirectedGraph} from 'data-structure-typed';
|
|
156
|
-
|
|
157
|
-
const graph = new UndirectedGraph();
|
|
158
|
-
graph.addVertex('A');
|
|
159
|
-
graph.addVertex('B');
|
|
160
|
-
graph.addVertex('C');
|
|
161
|
-
graph.addVertex('D');
|
|
162
|
-
graph.removeVertex('C');
|
|
163
|
-
graph.addEdge('A', 'B');
|
|
164
|
-
graph.addEdge('B', 'D');
|
|
165
|
-
|
|
166
|
-
const dijkstraResult = graph.dijkstra('A');
|
|
167
|
-
Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.id) // ['A', 'B', 'D']
|
|
168
26
|
```
|
|
169
27
|
|
|
170
|
-

|
|
171
|
-
|
|
172
|
-

|
|
173
|
-
|
|
174
|
-

|
|
175
|
-
|
|
176
|
-

|
|
177
|
-
|
|
178
28
|
|
|
179
29
|
## API docs & Examples
|
|
180
30
|
|
|
@@ -182,10 +32,6 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
182
32
|
|
|
183
33
|
[Live Examples](https://data-structure-typed-examples.vercel.app)
|
|
184
34
|
|
|
185
|
-
<a href="https://data-structure-typed-examples.vercel.app" target="_blank">Live Examples</a>
|
|
186
|
-
|
|
187
|
-
[//]: # ([Examples Repository](https://github.com/zrwusa/data-structure-typed-examples))
|
|
188
|
-
|
|
189
35
|
<a href="https://github.com/zrwusa/data-structure-typed-examples" target="_blank">Examples Repository</a>
|
|
190
36
|
|
|
191
37
|
## Data Structures
|
|
@@ -203,10 +49,10 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
203
49
|
<tbody>
|
|
204
50
|
<tr>
|
|
205
51
|
<td>Binary Tree</td>
|
|
206
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
207
|
-
</
|
|
208
|
-
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""
|
|
209
|
-
</
|
|
52
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
|
|
53
|
+
</td>
|
|
54
|
+
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""/>
|
|
55
|
+
</td>
|
|
210
56
|
<td><a href="https://data-structure-typed-docs.vercel.app/classes/BinaryTree.html"><span>Binary Tree</span></a></td>
|
|
211
57
|
<td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td>
|
|
212
58
|
</tr>
|
|
@@ -653,46 +499,6 @@ import {UndirectedGraph} from 'data-structure-typed';
|
|
|
653
499
|
|
|
654
500
|

|
|
655
501
|
|
|
656
|
-
[//]: # ()
|
|
657
|
-
|
|
658
|
-
[//]: # ()
|
|
659
|
-
[//]: # ()
|
|
660
|
-
|
|
661
|
-
[//]: # ()
|
|
662
|
-
[//]: # ()
|
|
663
|
-
|
|
664
|
-
[//]: # ()
|
|
665
|
-
[//]: # ()
|
|
666
|
-
|
|
667
|
-
[//]: # ()
|
|
668
|
-
[//]: # ()
|
|
669
|
-
|
|
670
|
-
[//]: # ()
|
|
671
|
-
[//]: # ()
|
|
672
|
-
|
|
673
|
-
[//]: # ()
|
|
674
|
-
[//]: # ()
|
|
675
|
-
|
|
676
|
-
[//]: # ()
|
|
677
|
-
[//]: # ()
|
|
678
|
-
|
|
679
|
-
[//]: # ()
|
|
680
|
-
[//]: # ()
|
|
681
|
-
|
|
682
|
-
[//]: # ()
|
|
683
|
-
[//]: # ()
|
|
684
|
-
|
|
685
|
-
[//]: # ()
|
|
686
|
-
[//]: # ()
|
|
687
|
-
|
|
688
|
-
[//]: # ()
|
|
689
|
-
[//]: # ()
|
|
690
|
-
|
|
691
|
-
[//]: # ()
|
|
692
|
-
|
|
693
|
-
[//]: # ()
|
|
694
|
-
|
|
695
|
-
[//]: # ()
|
|
696
502
|
|
|
697
503
|
|
|
698
504
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
3
|
+
exports.Trie = exports.TrieNode = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
var data_structure_typed_1 = require("data-structure-typed");
|
|
12
|
+
Object.defineProperty(exports, "TrieNode", { enumerable: true, get: function () { return data_structure_typed_1.TrieNode; } });
|
|
13
|
+
Object.defineProperty(exports, "Trie", { enumerable: true, get: function () { return data_structure_typed_1.Trie; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trie-typed",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.5",
|
|
4
4
|
"description": "Trie, Prefix tree. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"typescript": "^4.9.5"
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
|
-
"data-structure-typed": "^1.19.
|
|
59
|
+
"data-structure-typed": "^1.19.5",
|
|
60
60
|
"zod": "^3.22.2"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/tsconfig.json
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
"declaration": true,
|
|
4
4
|
"outDir": "./dist",
|
|
5
5
|
"module": "commonjs",
|
|
6
|
-
"target": "
|
|
6
|
+
"target": "es6",
|
|
7
7
|
"lib": [
|
|
8
|
-
"
|
|
8
|
+
// "es2015",
|
|
9
|
+
"esnext"
|
|
9
10
|
],
|
|
10
11
|
"strict": true,
|
|
11
12
|
"esModuleInterop": true,
|
|
@@ -30,8 +31,9 @@
|
|
|
30
31
|
"src",
|
|
31
32
|
],
|
|
32
33
|
"exclude": [
|
|
33
|
-
// "node_modules/data-structure-typed",
|
|
34
|
+
// "node_modules/data-structure-typed",
|
|
34
35
|
"node_modules",
|
|
35
36
|
"dist"
|
|
36
37
|
]
|
|
37
|
-
}
|
|
38
|
+
}
|
|
39
|
+
|
package/dist/trie.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export declare class TrieNode {
|
|
9
|
-
constructor(v: string);
|
|
10
|
-
private _val;
|
|
11
|
-
get val(): string;
|
|
12
|
-
set val(v: string);
|
|
13
|
-
protected _children: Map<string, TrieNode>;
|
|
14
|
-
get children(): Map<string, TrieNode>;
|
|
15
|
-
set children(v: Map<string, TrieNode>);
|
|
16
|
-
protected _isEnd: boolean;
|
|
17
|
-
get isEnd(): boolean;
|
|
18
|
-
set isEnd(v: boolean);
|
|
19
|
-
}
|
|
20
|
-
export declare class Trie {
|
|
21
|
-
constructor(words?: string[]);
|
|
22
|
-
protected _root: TrieNode;
|
|
23
|
-
get root(): TrieNode;
|
|
24
|
-
set root(v: TrieNode);
|
|
25
|
-
add(word: string): boolean;
|
|
26
|
-
has(input: string): boolean;
|
|
27
|
-
remove(word: string): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
|
|
30
|
-
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
31
|
-
* @returns a boolean value.
|
|
32
|
-
*/
|
|
33
|
-
isAbsPrefix(input: string): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
|
|
36
|
-
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
37
|
-
* @returns a boolean value.
|
|
38
|
-
*/
|
|
39
|
-
isPrefix(input: string): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
|
|
42
|
-
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
43
|
-
* in the Trie data structure.
|
|
44
|
-
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
45
|
-
*/
|
|
46
|
-
isCommonPrefix(input: string): boolean;
|
|
47
|
-
/**
|
|
48
|
-
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
49
|
-
* structure.
|
|
50
|
-
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
51
|
-
* Trie.
|
|
52
|
-
*/
|
|
53
|
-
getLongestCommonPrefix(): string;
|
|
54
|
-
/**
|
|
55
|
-
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
56
|
-
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
57
|
-
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
58
|
-
* @returns an array of strings.
|
|
59
|
-
*/
|
|
60
|
-
getAll(prefix?: string): string[];
|
|
61
|
-
}
|
package/dist/trie.js
DELETED
|
@@ -1,317 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __values = (this && this.__values) || function(o) {
|
|
3
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
4
|
-
if (m) return m.call(o);
|
|
5
|
-
if (o && typeof o.length === "number") return {
|
|
6
|
-
next: function () {
|
|
7
|
-
if (o && i >= o.length) o = void 0;
|
|
8
|
-
return { value: o && o[i++], done: !o };
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.Trie = exports.TrieNode = void 0;
|
|
15
|
-
/**
|
|
16
|
-
* data-structure-typed
|
|
17
|
-
*
|
|
18
|
-
* @author Tyler Zeng
|
|
19
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
20
|
-
* @license MIT License
|
|
21
|
-
*/
|
|
22
|
-
var TrieNode = /** @class */ (function () {
|
|
23
|
-
function TrieNode(v) {
|
|
24
|
-
this._val = v;
|
|
25
|
-
this._isEnd = false;
|
|
26
|
-
this._children = new Map();
|
|
27
|
-
}
|
|
28
|
-
Object.defineProperty(TrieNode.prototype, "val", {
|
|
29
|
-
get: function () {
|
|
30
|
-
return this._val;
|
|
31
|
-
},
|
|
32
|
-
set: function (v) {
|
|
33
|
-
this._val = v;
|
|
34
|
-
},
|
|
35
|
-
enumerable: false,
|
|
36
|
-
configurable: true
|
|
37
|
-
});
|
|
38
|
-
Object.defineProperty(TrieNode.prototype, "children", {
|
|
39
|
-
get: function () {
|
|
40
|
-
return this._children;
|
|
41
|
-
},
|
|
42
|
-
set: function (v) {
|
|
43
|
-
this._children = v;
|
|
44
|
-
},
|
|
45
|
-
enumerable: false,
|
|
46
|
-
configurable: true
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(TrieNode.prototype, "isEnd", {
|
|
49
|
-
get: function () {
|
|
50
|
-
return this._isEnd;
|
|
51
|
-
},
|
|
52
|
-
set: function (v) {
|
|
53
|
-
this._isEnd = v;
|
|
54
|
-
},
|
|
55
|
-
enumerable: false,
|
|
56
|
-
configurable: true
|
|
57
|
-
});
|
|
58
|
-
return TrieNode;
|
|
59
|
-
}());
|
|
60
|
-
exports.TrieNode = TrieNode;
|
|
61
|
-
var Trie = /** @class */ (function () {
|
|
62
|
-
function Trie(words) {
|
|
63
|
-
var e_1, _a;
|
|
64
|
-
this._root = new TrieNode('');
|
|
65
|
-
if (words) {
|
|
66
|
-
try {
|
|
67
|
-
for (var words_1 = __values(words), words_1_1 = words_1.next(); !words_1_1.done; words_1_1 = words_1.next()) {
|
|
68
|
-
var i = words_1_1.value;
|
|
69
|
-
this.add(i);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
73
|
-
finally {
|
|
74
|
-
try {
|
|
75
|
-
if (words_1_1 && !words_1_1.done && (_a = words_1.return)) _a.call(words_1);
|
|
76
|
-
}
|
|
77
|
-
finally { if (e_1) throw e_1.error; }
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
Object.defineProperty(Trie.prototype, "root", {
|
|
82
|
-
get: function () {
|
|
83
|
-
return this._root;
|
|
84
|
-
},
|
|
85
|
-
set: function (v) {
|
|
86
|
-
this._root = v;
|
|
87
|
-
},
|
|
88
|
-
enumerable: false,
|
|
89
|
-
configurable: true
|
|
90
|
-
});
|
|
91
|
-
Trie.prototype.add = function (word) {
|
|
92
|
-
var e_2, _a;
|
|
93
|
-
var cur = this._root;
|
|
94
|
-
try {
|
|
95
|
-
for (var word_1 = __values(word), word_1_1 = word_1.next(); !word_1_1.done; word_1_1 = word_1.next()) {
|
|
96
|
-
var c = word_1_1.value;
|
|
97
|
-
var nodeC = cur.children.get(c);
|
|
98
|
-
if (!nodeC) {
|
|
99
|
-
nodeC = new TrieNode(c);
|
|
100
|
-
cur.children.set(c, nodeC);
|
|
101
|
-
}
|
|
102
|
-
cur = nodeC;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
106
|
-
finally {
|
|
107
|
-
try {
|
|
108
|
-
if (word_1_1 && !word_1_1.done && (_a = word_1.return)) _a.call(word_1);
|
|
109
|
-
}
|
|
110
|
-
finally { if (e_2) throw e_2.error; }
|
|
111
|
-
}
|
|
112
|
-
cur.isEnd = true;
|
|
113
|
-
return true;
|
|
114
|
-
};
|
|
115
|
-
Trie.prototype.has = function (input) {
|
|
116
|
-
var e_3, _a;
|
|
117
|
-
var cur = this._root;
|
|
118
|
-
try {
|
|
119
|
-
for (var input_1 = __values(input), input_1_1 = input_1.next(); !input_1_1.done; input_1_1 = input_1.next()) {
|
|
120
|
-
var c = input_1_1.value;
|
|
121
|
-
var nodeC = cur.children.get(c);
|
|
122
|
-
if (!nodeC)
|
|
123
|
-
return false;
|
|
124
|
-
cur = nodeC;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
128
|
-
finally {
|
|
129
|
-
try {
|
|
130
|
-
if (input_1_1 && !input_1_1.done && (_a = input_1.return)) _a.call(input_1);
|
|
131
|
-
}
|
|
132
|
-
finally { if (e_3) throw e_3.error; }
|
|
133
|
-
}
|
|
134
|
-
return cur.isEnd;
|
|
135
|
-
};
|
|
136
|
-
Trie.prototype.remove = function (word) {
|
|
137
|
-
var isDeleted = false;
|
|
138
|
-
var dfs = function (cur, i) {
|
|
139
|
-
var char = word[i];
|
|
140
|
-
var child = cur.children.get(char);
|
|
141
|
-
if (child) {
|
|
142
|
-
if (i === word.length - 1) {
|
|
143
|
-
if (child.isEnd) {
|
|
144
|
-
if (child.children.size > 0) {
|
|
145
|
-
child.isEnd = false;
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
cur.children.delete(char);
|
|
149
|
-
}
|
|
150
|
-
isDeleted = true;
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
var res = dfs(child, i + 1);
|
|
156
|
-
if (res && !cur.isEnd && child.children.size === 0) {
|
|
157
|
-
cur.children.delete(char);
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
return false;
|
|
163
|
-
};
|
|
164
|
-
dfs(this.root, 0);
|
|
165
|
-
return isDeleted;
|
|
166
|
-
};
|
|
167
|
-
// --- start additional methods ---
|
|
168
|
-
/**
|
|
169
|
-
* The function checks if a given input string has an absolute prefix in a tree data structure.Only can present as a prefix, not a word
|
|
170
|
-
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
171
|
-
* @returns a boolean value.
|
|
172
|
-
*/
|
|
173
|
-
Trie.prototype.isAbsPrefix = function (input) {
|
|
174
|
-
var e_4, _a;
|
|
175
|
-
var cur = this._root;
|
|
176
|
-
try {
|
|
177
|
-
for (var input_2 = __values(input), input_2_1 = input_2.next(); !input_2_1.done; input_2_1 = input_2.next()) {
|
|
178
|
-
var c = input_2_1.value;
|
|
179
|
-
var nodeC = cur.children.get(c);
|
|
180
|
-
if (!nodeC)
|
|
181
|
-
return false;
|
|
182
|
-
cur = nodeC;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
186
|
-
finally {
|
|
187
|
-
try {
|
|
188
|
-
if (input_2_1 && !input_2_1.done && (_a = input_2.return)) _a.call(input_2);
|
|
189
|
-
}
|
|
190
|
-
finally { if (e_4) throw e_4.error; }
|
|
191
|
-
}
|
|
192
|
-
return !cur.isEnd;
|
|
193
|
-
};
|
|
194
|
-
/**
|
|
195
|
-
* The function checks if a given input string is a prefix of any existing string in a tree structure.Can present as a abs prefix or word
|
|
196
|
-
* @param {string} input - The input parameter is a string that represents the prefix we want to check.
|
|
197
|
-
* @returns a boolean value.
|
|
198
|
-
*/
|
|
199
|
-
Trie.prototype.isPrefix = function (input) {
|
|
200
|
-
var e_5, _a;
|
|
201
|
-
var cur = this._root;
|
|
202
|
-
try {
|
|
203
|
-
for (var input_3 = __values(input), input_3_1 = input_3.next(); !input_3_1.done; input_3_1 = input_3.next()) {
|
|
204
|
-
var c = input_3_1.value;
|
|
205
|
-
var nodeC = cur.children.get(c);
|
|
206
|
-
if (!nodeC)
|
|
207
|
-
return false;
|
|
208
|
-
cur = nodeC;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
212
|
-
finally {
|
|
213
|
-
try {
|
|
214
|
-
if (input_3_1 && !input_3_1.done && (_a = input_3.return)) _a.call(input_3);
|
|
215
|
-
}
|
|
216
|
-
finally { if (e_5) throw e_5.error; }
|
|
217
|
-
}
|
|
218
|
-
return true;
|
|
219
|
-
};
|
|
220
|
-
/**
|
|
221
|
-
* The function checks if the input string is a common prefix in a Trie data structure.Check if the input string is the common prefix of all the words
|
|
222
|
-
* @param {string} input - The input parameter is a string that represents the common prefix that we want to check for
|
|
223
|
-
* in the Trie data structure.
|
|
224
|
-
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
225
|
-
*/
|
|
226
|
-
Trie.prototype.isCommonPrefix = function (input) {
|
|
227
|
-
var commonPre = '';
|
|
228
|
-
var dfs = function (cur) {
|
|
229
|
-
commonPre += cur.val;
|
|
230
|
-
if (commonPre === input)
|
|
231
|
-
return;
|
|
232
|
-
if (cur.isEnd)
|
|
233
|
-
return;
|
|
234
|
-
if (cur && cur.children && cur.children.size === 1)
|
|
235
|
-
dfs(Array.from(cur.children.values())[0]);
|
|
236
|
-
else
|
|
237
|
-
return;
|
|
238
|
-
};
|
|
239
|
-
dfs(this._root);
|
|
240
|
-
return commonPre === input;
|
|
241
|
-
};
|
|
242
|
-
/**
|
|
243
|
-
* The function `getLongestCommonPrefix` returns the longest common prefix among all the words stored in a Trie data
|
|
244
|
-
* structure.
|
|
245
|
-
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
246
|
-
* Trie.
|
|
247
|
-
*/
|
|
248
|
-
Trie.prototype.getLongestCommonPrefix = function () {
|
|
249
|
-
var commonPre = '';
|
|
250
|
-
var dfs = function (cur) {
|
|
251
|
-
commonPre += cur.val;
|
|
252
|
-
if (cur.isEnd)
|
|
253
|
-
return;
|
|
254
|
-
if (cur && cur.children && cur.children.size === 1)
|
|
255
|
-
dfs(Array.from(cur.children.values())[0]);
|
|
256
|
-
else
|
|
257
|
-
return;
|
|
258
|
-
};
|
|
259
|
-
dfs(this._root);
|
|
260
|
-
return commonPre;
|
|
261
|
-
};
|
|
262
|
-
/**
|
|
263
|
-
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
264
|
-
* @param [prefix] - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
265
|
-
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
266
|
-
* @returns an array of strings.
|
|
267
|
-
*/
|
|
268
|
-
Trie.prototype.getAll = function (prefix) {
|
|
269
|
-
var e_6, _a;
|
|
270
|
-
if (prefix === void 0) { prefix = ''; }
|
|
271
|
-
var words = [];
|
|
272
|
-
function dfs(node, word) {
|
|
273
|
-
var e_7, _a;
|
|
274
|
-
try {
|
|
275
|
-
for (var _b = __values(node.children.keys()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
276
|
-
var char = _c.value;
|
|
277
|
-
var charNode = node.children.get(char);
|
|
278
|
-
if (charNode !== undefined) {
|
|
279
|
-
dfs(charNode, word.concat(char));
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
284
|
-
finally {
|
|
285
|
-
try {
|
|
286
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
287
|
-
}
|
|
288
|
-
finally { if (e_7) throw e_7.error; }
|
|
289
|
-
}
|
|
290
|
-
if (node.isEnd) {
|
|
291
|
-
words.push(word);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
var startNode = this._root;
|
|
295
|
-
if (prefix) {
|
|
296
|
-
try {
|
|
297
|
-
for (var prefix_1 = __values(prefix), prefix_1_1 = prefix_1.next(); !prefix_1_1.done; prefix_1_1 = prefix_1.next()) {
|
|
298
|
-
var c = prefix_1_1.value;
|
|
299
|
-
var nodeC = startNode.children.get(c);
|
|
300
|
-
if (nodeC)
|
|
301
|
-
startNode = nodeC;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
305
|
-
finally {
|
|
306
|
-
try {
|
|
307
|
-
if (prefix_1_1 && !prefix_1_1.done && (_a = prefix_1.return)) _a.call(prefix_1);
|
|
308
|
-
}
|
|
309
|
-
finally { if (e_6) throw e_6.error; }
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
dfs(startNode, prefix);
|
|
313
|
-
return words;
|
|
314
|
-
};
|
|
315
|
-
return Trie;
|
|
316
|
-
}());
|
|
317
|
-
exports.Trie = Trie;
|