data-structure-typed 1.36.2 → 1.36.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -1
- package/README.md +1 -1
- package/dist/data-structures/trie/trie.d.ts +10 -6
- package/dist/data-structures/trie/trie.js +57 -18
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/lib/data-structures/trie/trie.d.ts +10 -6
- package/lib/data-structures/trie/trie.js +57 -18
- package/package.json +5 -5
- package/src/data-structures/trie/trie.ts +57 -21
- package/test/unit/data-structures/trie/trie.test.ts +663 -3
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,10 +8,11 @@ All notable changes to this project will be documented in this file.
|
|
|
8
8
|
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
|
9
9
|
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
|
10
10
|
|
|
11
|
-
## [v1.36.
|
|
11
|
+
## [v1.36.4](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
|
12
12
|
|
|
13
13
|
### Changes
|
|
14
14
|
|
|
15
|
+
- [trie] support casesensitivity. getWords bug fixed [`#8`](https://github.com/zrwusa/data-structure-typed/pull/8)
|
|
15
16
|
- [binary-tree, graph] In order to optimize the design of Binary Trees,… [`#7`](https://github.com/zrwusa/data-structure-typed/pull/7)
|
|
16
17
|
- [BinaryTree, Heap] In abstract classes, only retain abstract methods.… [`#6`](https://github.com/zrwusa/data-structure-typed/pull/6)
|
|
17
18
|
- [heap] test [`#5`](https://github.com/zrwusa/data-structure-typed/pull/5)
|
package/README.md
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export declare class TrieNode {
|
|
9
9
|
constructor(v: string);
|
|
10
|
-
private
|
|
11
|
-
get
|
|
12
|
-
set
|
|
10
|
+
private _key;
|
|
11
|
+
get key(): string;
|
|
12
|
+
set key(v: string);
|
|
13
13
|
protected _children: Map<string, TrieNode>;
|
|
14
14
|
get children(): Map<string, TrieNode>;
|
|
15
15
|
set children(v: Map<string, TrieNode>);
|
|
@@ -18,13 +18,16 @@ export declare class TrieNode {
|
|
|
18
18
|
set isEnd(v: boolean);
|
|
19
19
|
}
|
|
20
20
|
export declare class Trie {
|
|
21
|
-
constructor(words?: string[]);
|
|
21
|
+
constructor(words?: string[], caseSensitive?: boolean);
|
|
22
22
|
protected _root: TrieNode;
|
|
23
23
|
get root(): TrieNode;
|
|
24
24
|
set root(v: TrieNode);
|
|
25
|
+
private _caseSensitive;
|
|
25
26
|
add(word: string): boolean;
|
|
26
27
|
has(input: string): boolean;
|
|
28
|
+
private _caseProcess;
|
|
27
29
|
remove(word: string): boolean;
|
|
30
|
+
getHeight(): number;
|
|
28
31
|
/**
|
|
29
32
|
* 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
33
|
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
@@ -53,9 +56,10 @@ export declare class Trie {
|
|
|
53
56
|
getLongestCommonPrefix(): string;
|
|
54
57
|
/**
|
|
55
58
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
56
|
-
* @param
|
|
59
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
57
60
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
61
|
+
* @param {number} max - The max count of words will be found
|
|
58
62
|
* @returns an array of strings.
|
|
59
63
|
*/
|
|
60
|
-
|
|
64
|
+
getWords(prefix?: string, max?: number): string[];
|
|
61
65
|
}
|
|
@@ -10,15 +10,15 @@ exports.Trie = exports.TrieNode = void 0;
|
|
|
10
10
|
*/
|
|
11
11
|
class TrieNode {
|
|
12
12
|
constructor(v) {
|
|
13
|
-
this.
|
|
13
|
+
this._key = v;
|
|
14
14
|
this._isEnd = false;
|
|
15
15
|
this._children = new Map();
|
|
16
16
|
}
|
|
17
|
-
get
|
|
18
|
-
return this.
|
|
17
|
+
get key() {
|
|
18
|
+
return this._key;
|
|
19
19
|
}
|
|
20
|
-
set
|
|
21
|
-
this.
|
|
20
|
+
set key(v) {
|
|
21
|
+
this._key = v;
|
|
22
22
|
}
|
|
23
23
|
get children() {
|
|
24
24
|
return this._children;
|
|
@@ -35,8 +35,9 @@ class TrieNode {
|
|
|
35
35
|
}
|
|
36
36
|
exports.TrieNode = TrieNode;
|
|
37
37
|
class Trie {
|
|
38
|
-
constructor(words) {
|
|
38
|
+
constructor(words, caseSensitive = true) {
|
|
39
39
|
this._root = new TrieNode('');
|
|
40
|
+
this._caseSensitive = caseSensitive;
|
|
40
41
|
if (words) {
|
|
41
42
|
for (const i of words) {
|
|
42
43
|
this.add(i);
|
|
@@ -50,7 +51,8 @@ class Trie {
|
|
|
50
51
|
this._root = v;
|
|
51
52
|
}
|
|
52
53
|
add(word) {
|
|
53
|
-
|
|
54
|
+
word = this._caseProcess(word);
|
|
55
|
+
let cur = this.root;
|
|
54
56
|
for (const c of word) {
|
|
55
57
|
let nodeC = cur.children.get(c);
|
|
56
58
|
if (!nodeC) {
|
|
@@ -63,7 +65,8 @@ class Trie {
|
|
|
63
65
|
return true;
|
|
64
66
|
}
|
|
65
67
|
has(input) {
|
|
66
|
-
|
|
68
|
+
input = this._caseProcess(input);
|
|
69
|
+
let cur = this.root;
|
|
67
70
|
for (const c of input) {
|
|
68
71
|
const nodeC = cur.children.get(c);
|
|
69
72
|
if (!nodeC)
|
|
@@ -72,7 +75,14 @@ class Trie {
|
|
|
72
75
|
}
|
|
73
76
|
return cur.isEnd;
|
|
74
77
|
}
|
|
78
|
+
_caseProcess(input) {
|
|
79
|
+
if (!this._caseSensitive) {
|
|
80
|
+
input = input.toLowerCase(); // Convert input to lowercase if case insensitive
|
|
81
|
+
}
|
|
82
|
+
return input;
|
|
83
|
+
}
|
|
75
84
|
remove(word) {
|
|
85
|
+
word = this._caseProcess(word);
|
|
76
86
|
let isDeleted = false;
|
|
77
87
|
const dfs = (cur, i) => {
|
|
78
88
|
const char = word[i];
|
|
@@ -103,6 +113,25 @@ class Trie {
|
|
|
103
113
|
dfs(this.root, 0);
|
|
104
114
|
return isDeleted;
|
|
105
115
|
}
|
|
116
|
+
getHeight() {
|
|
117
|
+
const beginRoot = this.root;
|
|
118
|
+
let maxDepth = 1;
|
|
119
|
+
if (beginRoot) {
|
|
120
|
+
const bfs = (node, level) => {
|
|
121
|
+
if (level > maxDepth) {
|
|
122
|
+
maxDepth = level;
|
|
123
|
+
}
|
|
124
|
+
const { children } = node;
|
|
125
|
+
if (children) {
|
|
126
|
+
for (const child of children.entries()) {
|
|
127
|
+
bfs(child[1], level + 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
bfs(beginRoot, 1);
|
|
132
|
+
}
|
|
133
|
+
return maxDepth;
|
|
134
|
+
}
|
|
106
135
|
// --- start additional methods ---
|
|
107
136
|
/**
|
|
108
137
|
* 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
|
|
@@ -110,7 +139,8 @@ class Trie {
|
|
|
110
139
|
* @returns a boolean value.
|
|
111
140
|
*/
|
|
112
141
|
isAbsPrefix(input) {
|
|
113
|
-
|
|
142
|
+
input = this._caseProcess(input);
|
|
143
|
+
let cur = this.root;
|
|
114
144
|
for (const c of input) {
|
|
115
145
|
const nodeC = cur.children.get(c);
|
|
116
146
|
if (!nodeC)
|
|
@@ -125,7 +155,8 @@ class Trie {
|
|
|
125
155
|
* @returns a boolean value.
|
|
126
156
|
*/
|
|
127
157
|
isPrefix(input) {
|
|
128
|
-
|
|
158
|
+
input = this._caseProcess(input);
|
|
159
|
+
let cur = this.root;
|
|
129
160
|
for (const c of input) {
|
|
130
161
|
const nodeC = cur.children.get(c);
|
|
131
162
|
if (!nodeC)
|
|
@@ -141,9 +172,10 @@ class Trie {
|
|
|
141
172
|
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
142
173
|
*/
|
|
143
174
|
isCommonPrefix(input) {
|
|
175
|
+
input = this._caseProcess(input);
|
|
144
176
|
let commonPre = '';
|
|
145
177
|
const dfs = (cur) => {
|
|
146
|
-
commonPre += cur.
|
|
178
|
+
commonPre += cur.key;
|
|
147
179
|
if (commonPre === input)
|
|
148
180
|
return;
|
|
149
181
|
if (cur.isEnd)
|
|
@@ -153,7 +185,7 @@ class Trie {
|
|
|
153
185
|
else
|
|
154
186
|
return;
|
|
155
187
|
};
|
|
156
|
-
dfs(this.
|
|
188
|
+
dfs(this.root);
|
|
157
189
|
return commonPre === input;
|
|
158
190
|
}
|
|
159
191
|
/**
|
|
@@ -165,7 +197,7 @@ class Trie {
|
|
|
165
197
|
getLongestCommonPrefix() {
|
|
166
198
|
let commonPre = '';
|
|
167
199
|
const dfs = (cur) => {
|
|
168
|
-
commonPre += cur.
|
|
200
|
+
commonPre += cur.key;
|
|
169
201
|
if (cur.isEnd)
|
|
170
202
|
return;
|
|
171
203
|
if (cur && cur.children && cur.children.size === 1)
|
|
@@ -173,17 +205,20 @@ class Trie {
|
|
|
173
205
|
else
|
|
174
206
|
return;
|
|
175
207
|
};
|
|
176
|
-
dfs(this.
|
|
208
|
+
dfs(this.root);
|
|
177
209
|
return commonPre;
|
|
178
210
|
}
|
|
179
211
|
/**
|
|
180
212
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
181
|
-
* @param
|
|
213
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
182
214
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
215
|
+
* @param {number} max - The max count of words will be found
|
|
183
216
|
* @returns an array of strings.
|
|
184
217
|
*/
|
|
185
|
-
|
|
218
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
|
|
219
|
+
prefix = this._caseProcess(prefix);
|
|
186
220
|
const words = [];
|
|
221
|
+
let found = 0;
|
|
187
222
|
function dfs(node, word) {
|
|
188
223
|
for (const char of node.children.keys()) {
|
|
189
224
|
const charNode = node.children.get(char);
|
|
@@ -192,10 +227,13 @@ class Trie {
|
|
|
192
227
|
}
|
|
193
228
|
}
|
|
194
229
|
if (node.isEnd) {
|
|
230
|
+
if (found > max - 1)
|
|
231
|
+
return;
|
|
195
232
|
words.push(word);
|
|
233
|
+
found++;
|
|
196
234
|
}
|
|
197
235
|
}
|
|
198
|
-
let startNode = this.
|
|
236
|
+
let startNode = this.root;
|
|
199
237
|
if (prefix) {
|
|
200
238
|
for (const c of prefix) {
|
|
201
239
|
const nodeC = startNode.children.get(c);
|
|
@@ -203,7 +241,8 @@ class Trie {
|
|
|
203
241
|
startNode = nodeC;
|
|
204
242
|
}
|
|
205
243
|
}
|
|
206
|
-
|
|
244
|
+
if (startNode !== this.root)
|
|
245
|
+
dfs(startNode, prefix);
|
|
207
246
|
return words;
|
|
208
247
|
}
|
|
209
248
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,QAAQ;IACnB,YAAY,CAAS;QACnB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC/C,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,CAAS;QACf,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAID,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ,CAAC,CAAwB;QACnC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACrB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,CAAU;QAClB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AApCD,4BAoCC;AAED,MAAa,IAAI;IACf,YAAY,KAAgB;
|
|
1
|
+
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACH,MAAa,QAAQ;IACnB,YAAY,CAAS;QACnB,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAC/C,CAAC;IAID,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,IAAI,GAAG,CAAC,CAAS;QACf,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAID,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAI,QAAQ,CAAC,CAAwB;QACnC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;IACrB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED,IAAI,KAAK,CAAC,CAAU;QAClB,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AApCD,4BAoCC;AAED,MAAa,IAAI;IACf,YAAY,KAAgB,EAAE,aAAa,GAAG,IAAI;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,QAAQ,CAAC,EAAE,CAAC,CAAC;QAC9B,IAAI,CAAC,cAAc,GAAG,aAAa,CAAC;QACpC,IAAI,KAAK,EAAE;YACT,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;gBACrB,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;aACb;SACF;IACH,CAAC;IAID,IAAI,IAAI;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9
|
constructor(v: string);
|
|
10
|
-
private
|
|
11
|
-
get
|
|
12
|
-
set
|
|
10
|
+
private _key;
|
|
11
|
+
get key(): string;
|
|
12
|
+
set key(v: string);
|
|
13
13
|
protected _children: Map<string, TrieNode>;
|
|
14
14
|
get children(): Map<string, TrieNode>;
|
|
15
15
|
set children(v: Map<string, TrieNode>);
|
|
@@ -18,13 +18,16 @@ export declare class TrieNode {
|
|
|
18
18
|
set isEnd(v: boolean);
|
|
19
19
|
}
|
|
20
20
|
export declare class Trie {
|
|
21
|
-
constructor(words?: string[]);
|
|
21
|
+
constructor(words?: string[], caseSensitive?: boolean);
|
|
22
22
|
protected _root: TrieNode;
|
|
23
23
|
get root(): TrieNode;
|
|
24
24
|
set root(v: TrieNode);
|
|
25
|
+
private _caseSensitive;
|
|
25
26
|
add(word: string): boolean;
|
|
26
27
|
has(input: string): boolean;
|
|
28
|
+
private _caseProcess;
|
|
27
29
|
remove(word: string): boolean;
|
|
30
|
+
getHeight(): number;
|
|
28
31
|
/**
|
|
29
32
|
* 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
33
|
* @param {string} input - The input parameter is a string that represents the input value for the function.
|
|
@@ -53,9 +56,10 @@ export declare class Trie {
|
|
|
53
56
|
getLongestCommonPrefix(): string;
|
|
54
57
|
/**
|
|
55
58
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
56
|
-
* @param
|
|
59
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
57
60
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
61
|
+
* @param {number} max - The max count of words will be found
|
|
58
62
|
* @returns an array of strings.
|
|
59
63
|
*/
|
|
60
|
-
|
|
64
|
+
getWords(prefix?: string, max?: number): string[];
|
|
61
65
|
}
|
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export class TrieNode {
|
|
9
9
|
constructor(v) {
|
|
10
|
-
this.
|
|
10
|
+
this._key = v;
|
|
11
11
|
this._isEnd = false;
|
|
12
12
|
this._children = new Map();
|
|
13
13
|
}
|
|
14
|
-
get
|
|
15
|
-
return this.
|
|
14
|
+
get key() {
|
|
15
|
+
return this._key;
|
|
16
16
|
}
|
|
17
|
-
set
|
|
18
|
-
this.
|
|
17
|
+
set key(v) {
|
|
18
|
+
this._key = v;
|
|
19
19
|
}
|
|
20
20
|
get children() {
|
|
21
21
|
return this._children;
|
|
@@ -31,8 +31,9 @@ export class TrieNode {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
export class Trie {
|
|
34
|
-
constructor(words) {
|
|
34
|
+
constructor(words, caseSensitive = true) {
|
|
35
35
|
this._root = new TrieNode('');
|
|
36
|
+
this._caseSensitive = caseSensitive;
|
|
36
37
|
if (words) {
|
|
37
38
|
for (const i of words) {
|
|
38
39
|
this.add(i);
|
|
@@ -46,7 +47,8 @@ export class Trie {
|
|
|
46
47
|
this._root = v;
|
|
47
48
|
}
|
|
48
49
|
add(word) {
|
|
49
|
-
|
|
50
|
+
word = this._caseProcess(word);
|
|
51
|
+
let cur = this.root;
|
|
50
52
|
for (const c of word) {
|
|
51
53
|
let nodeC = cur.children.get(c);
|
|
52
54
|
if (!nodeC) {
|
|
@@ -59,7 +61,8 @@ export class Trie {
|
|
|
59
61
|
return true;
|
|
60
62
|
}
|
|
61
63
|
has(input) {
|
|
62
|
-
|
|
64
|
+
input = this._caseProcess(input);
|
|
65
|
+
let cur = this.root;
|
|
63
66
|
for (const c of input) {
|
|
64
67
|
const nodeC = cur.children.get(c);
|
|
65
68
|
if (!nodeC)
|
|
@@ -68,7 +71,14 @@ export class Trie {
|
|
|
68
71
|
}
|
|
69
72
|
return cur.isEnd;
|
|
70
73
|
}
|
|
74
|
+
_caseProcess(input) {
|
|
75
|
+
if (!this._caseSensitive) {
|
|
76
|
+
input = input.toLowerCase(); // Convert input to lowercase if case insensitive
|
|
77
|
+
}
|
|
78
|
+
return input;
|
|
79
|
+
}
|
|
71
80
|
remove(word) {
|
|
81
|
+
word = this._caseProcess(word);
|
|
72
82
|
let isDeleted = false;
|
|
73
83
|
const dfs = (cur, i) => {
|
|
74
84
|
const char = word[i];
|
|
@@ -99,6 +109,25 @@ export class Trie {
|
|
|
99
109
|
dfs(this.root, 0);
|
|
100
110
|
return isDeleted;
|
|
101
111
|
}
|
|
112
|
+
getHeight() {
|
|
113
|
+
const beginRoot = this.root;
|
|
114
|
+
let maxDepth = 1;
|
|
115
|
+
if (beginRoot) {
|
|
116
|
+
const bfs = (node, level) => {
|
|
117
|
+
if (level > maxDepth) {
|
|
118
|
+
maxDepth = level;
|
|
119
|
+
}
|
|
120
|
+
const { children } = node;
|
|
121
|
+
if (children) {
|
|
122
|
+
for (const child of children.entries()) {
|
|
123
|
+
bfs(child[1], level + 1);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
bfs(beginRoot, 1);
|
|
128
|
+
}
|
|
129
|
+
return maxDepth;
|
|
130
|
+
}
|
|
102
131
|
// --- start additional methods ---
|
|
103
132
|
/**
|
|
104
133
|
* 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
|
|
@@ -106,7 +135,8 @@ export class Trie {
|
|
|
106
135
|
* @returns a boolean value.
|
|
107
136
|
*/
|
|
108
137
|
isAbsPrefix(input) {
|
|
109
|
-
|
|
138
|
+
input = this._caseProcess(input);
|
|
139
|
+
let cur = this.root;
|
|
110
140
|
for (const c of input) {
|
|
111
141
|
const nodeC = cur.children.get(c);
|
|
112
142
|
if (!nodeC)
|
|
@@ -121,7 +151,8 @@ export class Trie {
|
|
|
121
151
|
* @returns a boolean value.
|
|
122
152
|
*/
|
|
123
153
|
isPrefix(input) {
|
|
124
|
-
|
|
154
|
+
input = this._caseProcess(input);
|
|
155
|
+
let cur = this.root;
|
|
125
156
|
for (const c of input) {
|
|
126
157
|
const nodeC = cur.children.get(c);
|
|
127
158
|
if (!nodeC)
|
|
@@ -137,9 +168,10 @@ export class Trie {
|
|
|
137
168
|
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
138
169
|
*/
|
|
139
170
|
isCommonPrefix(input) {
|
|
171
|
+
input = this._caseProcess(input);
|
|
140
172
|
let commonPre = '';
|
|
141
173
|
const dfs = (cur) => {
|
|
142
|
-
commonPre += cur.
|
|
174
|
+
commonPre += cur.key;
|
|
143
175
|
if (commonPre === input)
|
|
144
176
|
return;
|
|
145
177
|
if (cur.isEnd)
|
|
@@ -149,7 +181,7 @@ export class Trie {
|
|
|
149
181
|
else
|
|
150
182
|
return;
|
|
151
183
|
};
|
|
152
|
-
dfs(this.
|
|
184
|
+
dfs(this.root);
|
|
153
185
|
return commonPre === input;
|
|
154
186
|
}
|
|
155
187
|
/**
|
|
@@ -161,7 +193,7 @@ export class Trie {
|
|
|
161
193
|
getLongestCommonPrefix() {
|
|
162
194
|
let commonPre = '';
|
|
163
195
|
const dfs = (cur) => {
|
|
164
|
-
commonPre += cur.
|
|
196
|
+
commonPre += cur.key;
|
|
165
197
|
if (cur.isEnd)
|
|
166
198
|
return;
|
|
167
199
|
if (cur && cur.children && cur.children.size === 1)
|
|
@@ -169,17 +201,20 @@ export class Trie {
|
|
|
169
201
|
else
|
|
170
202
|
return;
|
|
171
203
|
};
|
|
172
|
-
dfs(this.
|
|
204
|
+
dfs(this.root);
|
|
173
205
|
return commonPre;
|
|
174
206
|
}
|
|
175
207
|
/**
|
|
176
208
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
177
|
-
* @param
|
|
209
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
178
210
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
211
|
+
* @param {number} max - The max count of words will be found
|
|
179
212
|
* @returns an array of strings.
|
|
180
213
|
*/
|
|
181
|
-
|
|
214
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
|
|
215
|
+
prefix = this._caseProcess(prefix);
|
|
182
216
|
const words = [];
|
|
217
|
+
let found = 0;
|
|
183
218
|
function dfs(node, word) {
|
|
184
219
|
for (const char of node.children.keys()) {
|
|
185
220
|
const charNode = node.children.get(char);
|
|
@@ -188,10 +223,13 @@ export class Trie {
|
|
|
188
223
|
}
|
|
189
224
|
}
|
|
190
225
|
if (node.isEnd) {
|
|
226
|
+
if (found > max - 1)
|
|
227
|
+
return;
|
|
191
228
|
words.push(word);
|
|
229
|
+
found++;
|
|
192
230
|
}
|
|
193
231
|
}
|
|
194
|
-
let startNode = this.
|
|
232
|
+
let startNode = this.root;
|
|
195
233
|
if (prefix) {
|
|
196
234
|
for (const c of prefix) {
|
|
197
235
|
const nodeC = startNode.children.get(c);
|
|
@@ -199,7 +237,8 @@ export class Trie {
|
|
|
199
237
|
startNode = nodeC;
|
|
200
238
|
}
|
|
201
239
|
}
|
|
202
|
-
|
|
240
|
+
if (startNode !== this.root)
|
|
241
|
+
dfs(startNode, prefix);
|
|
203
242
|
return words;
|
|
204
243
|
}
|
|
205
244
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.36.
|
|
3
|
+
"version": "1.36.4",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -54,17 +54,17 @@
|
|
|
54
54
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
55
55
|
"@typescript-eslint/parser": "^6.7.4",
|
|
56
56
|
"auto-changelog": "^2.4.0",
|
|
57
|
-
"avl-tree-typed": "^1.36.
|
|
57
|
+
"avl-tree-typed": "^1.36.3",
|
|
58
58
|
"benchmark": "^2.1.4",
|
|
59
|
-
"binary-tree-typed": "^1.36.
|
|
60
|
-
"bst-typed": "^1.36.
|
|
59
|
+
"binary-tree-typed": "^1.36.3",
|
|
60
|
+
"bst-typed": "^1.36.3",
|
|
61
61
|
"dependency-cruiser": "^14.1.0",
|
|
62
62
|
"eslint": "^8.50.0",
|
|
63
63
|
"eslint-config-prettier": "^9.0.0",
|
|
64
64
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
65
65
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
66
66
|
"eslint-plugin-import": "^2.28.1",
|
|
67
|
-
"heap-typed": "^1.
|
|
67
|
+
"heap-typed": "^1.36.3",
|
|
68
68
|
"istanbul-badges-readme": "^1.8.5",
|
|
69
69
|
"jest": "^29.7.0",
|
|
70
70
|
"prettier": "^3.0.3",
|