data-structure-typed 1.36.3 → 1.36.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/.github/workflows/ci.yml +2 -20
- package/.travis.yml +3 -4
- package/CHANGELOG.md +6 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/dist/data-structures/binary-tree/binary-tree.js +10 -4
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/tree/tree.js +3 -5
- package/dist/data-structures/tree/tree.js.map +1 -1
- package/dist/data-structures/trie/trie.d.ts +49 -26
- package/dist/data-structures/trie/trie.js +99 -41
- package/dist/data-structures/trie/trie.js.map +1 -1
- package/lib/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/lib/data-structures/binary-tree/binary-tree.js +10 -4
- package/lib/data-structures/tree/tree.js +3 -5
- package/lib/data-structures/trie/trie.d.ts +49 -26
- package/lib/data-structures/trie/trie.js +97 -39
- package/package.json +5 -5
- package/src/data-structures/binary-tree/binary-tree.ts +9 -5
- package/src/data-structures/tree/tree.ts +3 -5
- package/src/data-structures/trie/trie.ts +101 -40
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +13 -0
- package/test/unit/data-structures/heap/heap.test.ts +0 -1
- package/test/unit/data-structures/tree/tree.test.ts +2 -2
- package/test/unit/data-structures/trie/trie.test.ts +740 -10
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"trie.js","sourceRoot":"","sources":["../../../src/data-structures/trie/trie.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;AAEH;;;GAGG;AACH,MAAa,QAAQ;IACnB,YAAY,GAAW;QACrB,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC;QAChB,IAAI,CAAC,MAA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,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE;gBACvC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;gBACzC,IAAI,QAAQ,KAAK,SAAS,EAAE;oBAC1B,GAAG,CAAC,QAAQ,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;iBAClC;aACF;YACD,IAAI,IAAI,CAAC,KAAK,EAAE;gBACd,IAAI,KAAK,GAAG,GAAG,GAAG,CAAC;oBAAE,OAAO;gBAC5B,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACjB,KAAK,EAAE,CAAC;aACT;QACH,CAAC;QAED,IAAI,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC;QAE1B,IAAI,MAAM,EAAE;YACV,KAAK,MAAM,CAAC,IAAI,MAAM,EAAE;gBACtB,MAAM,KAAK,GAAG,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBACxC,IAAI,KAAK;oBAAE,SAAS,GAAG,KAAK,CAAC;aAC9B;SACF;QACD,IAAI,SAAS,KAAK,IAAI,CAAC,IAAI;YAAE,GAAG,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QAEpD,OAAO,KAAK,CAAC;IACf,CAAC;CAGF;AAxOD,oBAwOC"}
|
|
@@ -120,10 +120,11 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
|
|
|
120
120
|
remove(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
121
121
|
/**
|
|
122
122
|
* The function calculates the depth of a node in a binary tree.
|
|
123
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
123
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
|
|
124
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be the predecessor node of distNode
|
|
124
125
|
* @returns the depth of the given node or binary tree.
|
|
125
126
|
*/
|
|
126
|
-
getDepth(beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
127
|
+
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot?: N | BinaryTreeNodeKey | null): number;
|
|
127
128
|
/**
|
|
128
129
|
* The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
|
|
129
130
|
* @param {N | BinaryTreeNodeKey | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
|
|
@@ -309,16 +309,22 @@ export class BinaryTree {
|
|
|
309
309
|
}
|
|
310
310
|
/**
|
|
311
311
|
* The function calculates the depth of a node in a binary tree.
|
|
312
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
312
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
|
|
313
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be the predecessor node of distNode
|
|
313
314
|
* @returns the depth of the given node or binary tree.
|
|
314
315
|
*/
|
|
315
|
-
getDepth(beginRoot = this.root) {
|
|
316
|
+
getDepth(distNode, beginRoot = this.root) {
|
|
317
|
+
if (typeof distNode === 'number')
|
|
318
|
+
distNode = this.get(distNode, 'key');
|
|
316
319
|
if (typeof beginRoot === 'number')
|
|
317
320
|
beginRoot = this.get(beginRoot, 'key');
|
|
318
321
|
let depth = 0;
|
|
319
|
-
while (
|
|
322
|
+
while (distNode === null || distNode === void 0 ? void 0 : distNode.parent) {
|
|
323
|
+
if (distNode === beginRoot) {
|
|
324
|
+
return depth;
|
|
325
|
+
}
|
|
320
326
|
depth++;
|
|
321
|
-
|
|
327
|
+
distNode = distNode.parent;
|
|
322
328
|
}
|
|
323
329
|
return depth;
|
|
324
330
|
}
|
|
@@ -34,10 +34,8 @@ export class TreeNode {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
getHeight() {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let maxDepth = 1;
|
|
40
|
-
if (beginRoot) {
|
|
37
|
+
let maxDepth = 0;
|
|
38
|
+
if (this) {
|
|
41
39
|
const bfs = (node, level) => {
|
|
42
40
|
if (level > maxDepth) {
|
|
43
41
|
maxDepth = level;
|
|
@@ -49,7 +47,7 @@ export class TreeNode {
|
|
|
49
47
|
}
|
|
50
48
|
}
|
|
51
49
|
};
|
|
52
|
-
bfs(
|
|
50
|
+
bfs(this, 0);
|
|
53
51
|
}
|
|
54
52
|
return maxDepth;
|
|
55
53
|
}
|
|
@@ -5,11 +5,15 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
10
|
+
* and a flag indicating whether it's the end of a word.
|
|
11
|
+
*/
|
|
8
12
|
export declare class TrieNode {
|
|
9
|
-
constructor(
|
|
10
|
-
private
|
|
11
|
-
get
|
|
12
|
-
set
|
|
13
|
+
constructor(key: string);
|
|
14
|
+
private _key;
|
|
15
|
+
get key(): string;
|
|
16
|
+
set key(v: string);
|
|
13
17
|
protected _children: Map<string, TrieNode>;
|
|
14
18
|
get children(): Map<string, TrieNode>;
|
|
15
19
|
set children(v: Map<string, TrieNode>);
|
|
@@ -17,45 +21,64 @@ export declare class TrieNode {
|
|
|
17
21
|
get isEnd(): boolean;
|
|
18
22
|
set isEnd(v: boolean);
|
|
19
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
26
|
+
*/
|
|
20
27
|
export declare class Trie {
|
|
21
|
-
constructor(words?: string[]);
|
|
28
|
+
constructor(words?: string[], caseSensitive?: boolean);
|
|
22
29
|
protected _root: TrieNode;
|
|
23
30
|
get root(): TrieNode;
|
|
24
31
|
set root(v: TrieNode);
|
|
32
|
+
private readonly _caseSensitive;
|
|
33
|
+
/**
|
|
34
|
+
* Add a word to the Trie structure.
|
|
35
|
+
* @param {string} word - The word to add.
|
|
36
|
+
* @returns {boolean} True if the word was successfully added.
|
|
37
|
+
*/
|
|
25
38
|
add(word: string): boolean;
|
|
26
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Check if the Trie contains a given word.
|
|
41
|
+
* @param {string} word - The word to check for.
|
|
42
|
+
* @returns {boolean} True if the word is present in the Trie.
|
|
43
|
+
*/
|
|
44
|
+
has(word: string): boolean;
|
|
45
|
+
private _caseProcess;
|
|
46
|
+
/**
|
|
47
|
+
* Remove a word from the Trie structure.
|
|
48
|
+
* @param{string} word - The word to remove.
|
|
49
|
+
* @returns {boolean} True if the word was successfully removed.
|
|
50
|
+
*/
|
|
27
51
|
remove(word: string): boolean;
|
|
52
|
+
getHeight(): number;
|
|
28
53
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @param {string} input - The input
|
|
31
|
-
* @returns
|
|
54
|
+
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
|
|
55
|
+
* @param {string} input - The input string to check.
|
|
56
|
+
* @returns {boolean} True if it's an absolute prefix in the Trie.
|
|
32
57
|
*/
|
|
33
|
-
|
|
58
|
+
hasPurePrefix(input: string): boolean;
|
|
34
59
|
/**
|
|
35
|
-
*
|
|
36
|
-
* @param {string} input - The input
|
|
37
|
-
* @returns a
|
|
60
|
+
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
|
|
61
|
+
* @param {string} input - The input string representing the prefix to check.
|
|
62
|
+
* @returns {boolean} True if it's a prefix in the Trie.
|
|
38
63
|
*/
|
|
39
|
-
|
|
64
|
+
hasPrefix(input: string): boolean;
|
|
40
65
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @param {string} input - The input
|
|
43
|
-
* in the Trie
|
|
44
|
-
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
66
|
+
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
|
|
67
|
+
* @param {string} input - The input string representing the common prefix to check for.
|
|
68
|
+
* @returns {boolean} True if it's a common prefix in the Trie.
|
|
45
69
|
*/
|
|
46
|
-
|
|
70
|
+
hasCommonPrefix(input: string): boolean;
|
|
47
71
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
51
|
-
* Trie.
|
|
72
|
+
* Get the longest common prefix among all the words stored in the Trie.
|
|
73
|
+
* @returns {string} The longest common prefix found in the Trie.
|
|
52
74
|
*/
|
|
53
75
|
getLongestCommonPrefix(): string;
|
|
54
76
|
/**
|
|
55
77
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
56
|
-
* @param
|
|
78
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
57
79
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
58
|
-
* @
|
|
80
|
+
* @param {number} max - The max count of words will be found
|
|
81
|
+
* @returns {string[]} an array of strings.
|
|
59
82
|
*/
|
|
60
|
-
|
|
83
|
+
getWords(prefix?: string, max?: number): string[];
|
|
61
84
|
}
|
|
@@ -5,17 +5,21 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
10
|
+
* and a flag indicating whether it's the end of a word.
|
|
11
|
+
*/
|
|
8
12
|
export class TrieNode {
|
|
9
|
-
constructor(
|
|
10
|
-
this.
|
|
13
|
+
constructor(key) {
|
|
14
|
+
this._key = key;
|
|
11
15
|
this._isEnd = false;
|
|
12
16
|
this._children = new Map();
|
|
13
17
|
}
|
|
14
|
-
get
|
|
15
|
-
return this.
|
|
18
|
+
get key() {
|
|
19
|
+
return this._key;
|
|
16
20
|
}
|
|
17
|
-
set
|
|
18
|
-
this.
|
|
21
|
+
set key(v) {
|
|
22
|
+
this._key = v;
|
|
19
23
|
}
|
|
20
24
|
get children() {
|
|
21
25
|
return this._children;
|
|
@@ -30,9 +34,13 @@ export class TrieNode {
|
|
|
30
34
|
this._isEnd = v;
|
|
31
35
|
}
|
|
32
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
|
39
|
+
*/
|
|
33
40
|
export class Trie {
|
|
34
|
-
constructor(words) {
|
|
41
|
+
constructor(words, caseSensitive = true) {
|
|
35
42
|
this._root = new TrieNode('');
|
|
43
|
+
this._caseSensitive = caseSensitive;
|
|
36
44
|
if (words) {
|
|
37
45
|
for (const i of words) {
|
|
38
46
|
this.add(i);
|
|
@@ -45,8 +53,14 @@ export class Trie {
|
|
|
45
53
|
set root(v) {
|
|
46
54
|
this._root = v;
|
|
47
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Add a word to the Trie structure.
|
|
58
|
+
* @param {string} word - The word to add.
|
|
59
|
+
* @returns {boolean} True if the word was successfully added.
|
|
60
|
+
*/
|
|
48
61
|
add(word) {
|
|
49
|
-
|
|
62
|
+
word = this._caseProcess(word);
|
|
63
|
+
let cur = this.root;
|
|
50
64
|
for (const c of word) {
|
|
51
65
|
let nodeC = cur.children.get(c);
|
|
52
66
|
if (!nodeC) {
|
|
@@ -58,9 +72,15 @@ export class Trie {
|
|
|
58
72
|
cur.isEnd = true;
|
|
59
73
|
return true;
|
|
60
74
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Check if the Trie contains a given word.
|
|
77
|
+
* @param {string} word - The word to check for.
|
|
78
|
+
* @returns {boolean} True if the word is present in the Trie.
|
|
79
|
+
*/
|
|
80
|
+
has(word) {
|
|
81
|
+
word = this._caseProcess(word);
|
|
82
|
+
let cur = this.root;
|
|
83
|
+
for (const c of word) {
|
|
64
84
|
const nodeC = cur.children.get(c);
|
|
65
85
|
if (!nodeC)
|
|
66
86
|
return false;
|
|
@@ -68,7 +88,19 @@ export class Trie {
|
|
|
68
88
|
}
|
|
69
89
|
return cur.isEnd;
|
|
70
90
|
}
|
|
91
|
+
_caseProcess(str) {
|
|
92
|
+
if (!this._caseSensitive) {
|
|
93
|
+
str = str.toLowerCase(); // Convert str to lowercase if case-insensitive
|
|
94
|
+
}
|
|
95
|
+
return str;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Remove a word from the Trie structure.
|
|
99
|
+
* @param{string} word - The word to remove.
|
|
100
|
+
* @returns {boolean} True if the word was successfully removed.
|
|
101
|
+
*/
|
|
71
102
|
remove(word) {
|
|
103
|
+
word = this._caseProcess(word);
|
|
72
104
|
let isDeleted = false;
|
|
73
105
|
const dfs = (cur, i) => {
|
|
74
106
|
const char = word[i];
|
|
@@ -99,14 +131,34 @@ export class Trie {
|
|
|
99
131
|
dfs(this.root, 0);
|
|
100
132
|
return isDeleted;
|
|
101
133
|
}
|
|
134
|
+
getHeight() {
|
|
135
|
+
const beginRoot = this.root;
|
|
136
|
+
let maxDepth = 0;
|
|
137
|
+
if (beginRoot) {
|
|
138
|
+
const bfs = (node, level) => {
|
|
139
|
+
if (level > maxDepth) {
|
|
140
|
+
maxDepth = level;
|
|
141
|
+
}
|
|
142
|
+
const { children } = node;
|
|
143
|
+
if (children) {
|
|
144
|
+
for (const child of children.entries()) {
|
|
145
|
+
bfs(child[1], level + 1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
bfs(beginRoot, 0);
|
|
150
|
+
}
|
|
151
|
+
return maxDepth;
|
|
152
|
+
}
|
|
102
153
|
// --- start additional methods ---
|
|
103
154
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @param {string} input - The input
|
|
106
|
-
* @returns
|
|
155
|
+
* Check if a given input string has an absolute prefix in the Trie, meaning it's not a complete word.
|
|
156
|
+
* @param {string} input - The input string to check.
|
|
157
|
+
* @returns {boolean} True if it's an absolute prefix in the Trie.
|
|
107
158
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
159
|
+
hasPurePrefix(input) {
|
|
160
|
+
input = this._caseProcess(input);
|
|
161
|
+
let cur = this.root;
|
|
110
162
|
for (const c of input) {
|
|
111
163
|
const nodeC = cur.children.get(c);
|
|
112
164
|
if (!nodeC)
|
|
@@ -116,12 +168,13 @@ export class Trie {
|
|
|
116
168
|
return !cur.isEnd;
|
|
117
169
|
}
|
|
118
170
|
/**
|
|
119
|
-
*
|
|
120
|
-
* @param {string} input - The input
|
|
121
|
-
* @returns a
|
|
171
|
+
* Check if a given input string is a prefix of any existing word in the Trie, whether as an absolute prefix or a complete word.
|
|
172
|
+
* @param {string} input - The input string representing the prefix to check.
|
|
173
|
+
* @returns {boolean} True if it's a prefix in the Trie.
|
|
122
174
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
175
|
+
hasPrefix(input) {
|
|
176
|
+
input = this._caseProcess(input);
|
|
177
|
+
let cur = this.root;
|
|
125
178
|
for (const c of input) {
|
|
126
179
|
const nodeC = cur.children.get(c);
|
|
127
180
|
if (!nodeC)
|
|
@@ -131,15 +184,15 @@ export class Trie {
|
|
|
131
184
|
return true;
|
|
132
185
|
}
|
|
133
186
|
/**
|
|
134
|
-
*
|
|
135
|
-
* @param {string} input - The input
|
|
136
|
-
* in the Trie
|
|
137
|
-
* @returns a boolean value indicating whether the input string is a common prefix in the Trie data structure.
|
|
187
|
+
* Check if the input string is a common prefix in the Trie, meaning it's a prefix shared by all words in the Trie.
|
|
188
|
+
* @param {string} input - The input string representing the common prefix to check for.
|
|
189
|
+
* @returns {boolean} True if it's a common prefix in the Trie.
|
|
138
190
|
*/
|
|
139
|
-
|
|
191
|
+
hasCommonPrefix(input) {
|
|
192
|
+
input = this._caseProcess(input);
|
|
140
193
|
let commonPre = '';
|
|
141
194
|
const dfs = (cur) => {
|
|
142
|
-
commonPre += cur.
|
|
195
|
+
commonPre += cur.key;
|
|
143
196
|
if (commonPre === input)
|
|
144
197
|
return;
|
|
145
198
|
if (cur.isEnd)
|
|
@@ -149,19 +202,17 @@ export class Trie {
|
|
|
149
202
|
else
|
|
150
203
|
return;
|
|
151
204
|
};
|
|
152
|
-
dfs(this.
|
|
205
|
+
dfs(this.root);
|
|
153
206
|
return commonPre === input;
|
|
154
207
|
}
|
|
155
208
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* @returns The function `getLongestCommonPrefix` returns a string, which is the longest common prefix found in the
|
|
159
|
-
* Trie.
|
|
209
|
+
* Get the longest common prefix among all the words stored in the Trie.
|
|
210
|
+
* @returns {string} The longest common prefix found in the Trie.
|
|
160
211
|
*/
|
|
161
212
|
getLongestCommonPrefix() {
|
|
162
213
|
let commonPre = '';
|
|
163
214
|
const dfs = (cur) => {
|
|
164
|
-
commonPre += cur.
|
|
215
|
+
commonPre += cur.key;
|
|
165
216
|
if (cur.isEnd)
|
|
166
217
|
return;
|
|
167
218
|
if (cur && cur.children && cur.children.size === 1)
|
|
@@ -169,17 +220,20 @@ export class Trie {
|
|
|
169
220
|
else
|
|
170
221
|
return;
|
|
171
222
|
};
|
|
172
|
-
dfs(this.
|
|
223
|
+
dfs(this.root);
|
|
173
224
|
return commonPre;
|
|
174
225
|
}
|
|
175
226
|
/**
|
|
176
227
|
* The `getAll` function returns an array of all words in a Trie data structure that start with a given prefix.
|
|
177
|
-
* @param
|
|
228
|
+
* @param {string} prefix - The `prefix` parameter is a string that represents the prefix that we want to search for in the
|
|
178
229
|
* trie. It is an optional parameter, so if no prefix is provided, it will default to an empty string.
|
|
179
|
-
* @
|
|
230
|
+
* @param {number} max - The max count of words will be found
|
|
231
|
+
* @returns {string[]} an array of strings.
|
|
180
232
|
*/
|
|
181
|
-
|
|
233
|
+
getWords(prefix = '', max = Number.MAX_SAFE_INTEGER) {
|
|
234
|
+
prefix = this._caseProcess(prefix);
|
|
182
235
|
const words = [];
|
|
236
|
+
let found = 0;
|
|
183
237
|
function dfs(node, word) {
|
|
184
238
|
for (const char of node.children.keys()) {
|
|
185
239
|
const charNode = node.children.get(char);
|
|
@@ -188,10 +242,13 @@ export class Trie {
|
|
|
188
242
|
}
|
|
189
243
|
}
|
|
190
244
|
if (node.isEnd) {
|
|
245
|
+
if (found > max - 1)
|
|
246
|
+
return;
|
|
191
247
|
words.push(word);
|
|
248
|
+
found++;
|
|
192
249
|
}
|
|
193
250
|
}
|
|
194
|
-
let startNode = this.
|
|
251
|
+
let startNode = this.root;
|
|
195
252
|
if (prefix) {
|
|
196
253
|
for (const c of prefix) {
|
|
197
254
|
const nodeC = startNode.children.get(c);
|
|
@@ -199,7 +256,8 @@ export class Trie {
|
|
|
199
256
|
startNode = nodeC;
|
|
200
257
|
}
|
|
201
258
|
}
|
|
202
|
-
|
|
259
|
+
if (startNode !== this.root)
|
|
260
|
+
dfs(startNode, prefix);
|
|
203
261
|
return words;
|
|
204
262
|
}
|
|
205
263
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.36.
|
|
3
|
+
"version": "1.36.5",
|
|
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.4",
|
|
58
58
|
"benchmark": "^2.1.4",
|
|
59
|
-
"binary-tree-typed": "^1.36.
|
|
60
|
-
"bst-typed": "^1.36.
|
|
59
|
+
"binary-tree-typed": "^1.36.4",
|
|
60
|
+
"bst-typed": "^1.36.4",
|
|
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.36.
|
|
67
|
+
"heap-typed": "^1.36.4",
|
|
68
68
|
"istanbul-badges-readme": "^1.8.5",
|
|
69
69
|
"jest": "^29.7.0",
|
|
70
70
|
"prettier": "^3.0.3",
|
|
@@ -350,16 +350,20 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
350
350
|
|
|
351
351
|
/**
|
|
352
352
|
* The function calculates the depth of a node in a binary tree.
|
|
353
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
353
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter can be any node of the tree
|
|
354
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter can be the predecessor node of distNode
|
|
354
355
|
* @returns the depth of the given node or binary tree.
|
|
355
356
|
*/
|
|
356
|
-
getDepth(beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
357
|
+
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
358
|
+
if (typeof distNode === 'number') distNode = this.get(distNode, 'key');
|
|
357
359
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'key');
|
|
358
|
-
|
|
359
360
|
let depth = 0;
|
|
360
|
-
while (
|
|
361
|
+
while (distNode?.parent) {
|
|
362
|
+
if (distNode === beginRoot) {
|
|
363
|
+
return depth;
|
|
364
|
+
}
|
|
361
365
|
depth++;
|
|
362
|
-
|
|
366
|
+
distNode = distNode.parent;
|
|
363
367
|
}
|
|
364
368
|
return depth;
|
|
365
369
|
}
|
|
@@ -47,10 +47,8 @@ export class TreeNode<V = any> {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
getHeight() {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
let maxDepth = 1;
|
|
53
|
-
if (beginRoot) {
|
|
50
|
+
let maxDepth = 0;
|
|
51
|
+
if (this) {
|
|
54
52
|
const bfs = (node: TreeNode<V>, level: number) => {
|
|
55
53
|
if (level > maxDepth) {
|
|
56
54
|
maxDepth = level;
|
|
@@ -62,7 +60,7 @@ export class TreeNode<V = any> {
|
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
62
|
};
|
|
65
|
-
bfs(
|
|
63
|
+
bfs(this, 0);
|
|
66
64
|
}
|
|
67
65
|
return maxDepth;
|
|
68
66
|
}
|