0x00 写在最前

嗯……这个 Trie 树完全是手撸的,没参考啥教程/博客,所以接口名称和算法实现上可能和常规方法有些许差异。不过功能都是完备且 ok 的(

Trie 树,又名字典树、前缀树,其名称来源于 retrieve 一词(v. 检索),是一种有序树,也属于确定有限自动状态机。其常被用于提供搜索提示。

0x01 实现

代码应该是 self-explained 的,所以就偷懒不写注释了(

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//@ts-check

"use strict";

class TrieNode {
/**
* Creates an instance of TrieNode.
* @param {String} ch currentChar
*/
constructor(ch) {
this.count = 0;
/**
* @type {TrieNode[]}
*/
this.children = [];
this.isEndOfWord = false;
this.char = ch;
}
}

class Trie {
constructor() {
this.root = new TrieNode("");
}
/**
*
* @param {String} word
*/
insert(word) {
if(word === undefined || word.length === 0) {
return this;
}
let node = this.root;
for(let i = 0; i < word.length; i++) {
let pos = word[i].charCodeAt(0) - "a".charCodeAt(0);
if(node.children[pos] === undefined) {
node.children[pos] = new TrieNode(word[i]);
node.children[pos].count = 1;
} else {
node.children[pos].count += 1;
}
node = node.children[pos];
}
// after `for` finished, we need to mark node as end-of-the-word.
// note that an end-of-the-word node may also have child nodes.
node.isEndOfWord = true;
return this;
}
/**
* @param {String[]} strs
*/
insertAll(strs = []) {
for(let str of strs) {
this.insert(str);
}
return this;
}
countPrefix(pfx = "") {
if(pfx.length === 0) {
return this.root.count;
}
let node = this.root;
for(let i = 0; i < pfx.length; i++) {
let pos = pfx[i].charCodeAt(0) - "a".charCodeAt(0);
if(node.children[pos] === undefined) {
return 0;
}
node = node.children[pos];
}
return node.count;
}
/**
* @param {String} word
*/
contains(word) {
if(word === undefined)
return false;
return this.countPrefix(word) >= 1;
}
/**
* @param {String[]} words
* @returns {Boolean} if every word in `words` exists in Trie
*/
containsAll(words = []) {
for(let word of words) {
if(this.contains(word) === false) {
return false;
}
}
return true;
}
/**
* @param {String} word
*/
remove(word) {
if(word === undefined || word.length === 0) {
return this;
}
let node = this.root;
for(let i = 0; i < word.length; i++) {
let pos = word[i].charCodeAt(0) - "a".charCodeAt(0);
const childNode = node.children[pos];
if(childNode === undefined) {
break;
}
node.count -= 1;
if(childNode.isEndOfWord && !childNode.count) {
delete node.children[pos];
break;
}
node = childNode;
}
return this;
}
/**
* @param {String[]} words
*/
removeAll(words = []) {
for(let word of words) {
this.remove(word);
}
return this;
}
/**
* @returns {String[]} words in an array
*/
getWordsByPrefix(pfx = "") {
let node = this.root;
for(let i = 0; i < pfx.length; i++) {
let pos = pfx[i].charCodeAt(0) - "a".charCodeAt(0);
if(node.children[pos] === undefined) {
return [];
}
node = node.children[pos];
}
return this.enumAllCombinations(node);
}
getWords() {
return this.getWordsByPrefix("");
}
/**
* @param {TrieNode} node
* @param {String} pfx used for recursively enumerate
* @returns {String[]}
*/
enumAllCombinations(node, pfx = "") {
/**
* @type {String[]}
*/
let result = [];
if(node.children.length === 0) {
return result;
}
for(let childNode of node.children) {
if(!childNode) {
continue;
}
if(childNode.isEndOfWord) {
result.push(node.char + childNode.char);
}
result = result.concat(this.enumAllCombinations(childNode, node.char));
}
return result.map(str => pfx + str);
}
}

来源:https://blog.jiejiss.com/