-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.js
More file actions
58 lines (58 loc) · 1.57 KB
/
Trie.js
File metadata and controls
58 lines (58 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 定义节点
class Node {
constructor(isWord=false) {
this.isWord = isWord;
this.next = new Map();
}
}
// 字典树
module.exports = class Trie{
constructor() {
this.root = new Node();
this.size = 0;
}
// 获取Trie中存储的单词数量
getSize() {
return this.size;
}
// 向Trie中添加一个新的单词word
add(word) {
// 非递归写法,字符串拆成字符
let cur = this.root;
for(let i = 0;i < word.length; i++) {
let c = word.charAt(i);
if(cur.next.get(c) === undefined) {
cur.next.set(c, new Node());
}
cur = cur.next.get(c); // 走到下一个节点
}
// 这个节点以前不表示单词结尾
if(!cur.isWord) {
cur.isWord = true;
this.size++;
}
}
// 查询单词word是否在Trie中
contains(word) {
let cur = root;
for(let i = 0;i < word.length; i++) {
let c = wrod.charAt(i);
if(cur.next.get(c) === null)
return false;
cur = cur.next.get(c);
}
// 比如trie中有panda,查pan。查到节点,但是并没有这个单词
return cur.isWord;
}
// 前缀查询
isPrefix(prefix) {
let cur = this.root;
for(let i = 0; i < prefix.length; i++) {
let c = prefix.charAt(i);
if(cur.next.get(c) === undefined)
return false;
cur = cur.next.get(c);
}
return true;
}
}