{A}
AlgoViz
首页
路线图
题单
教程
题目
可视化
错题本
进度
登录
加载中…
前缀树 Trie
逐步插入单词,观察 Trie 节点的创建与共享前缀过程。
速度:
0.5x
1x
2x
4x
单词:
黄色=当前节点,蓝色=路径,绿色=单词结尾,●=根节点
●
初始化空 Trie,只有根节点
步骤 1 / 29
初始化空 Trie
前缀树 Trie
复制代码
当前高亮行:
2
(初始化空 Trie)
1
class Trie {
2
constructor() {
this
.root = {}; }
3
insert(word) {
4
let
node =
this
.root;
5
for
(
const
ch of word) {
6
if
(!node[ch]) node[ch] = {};
7
node = node[ch];
8
}
9
node.isEnd =
true
;
10
}
11
}