DSA - 前缀树 Trie
概念
操作
插入
insert(word) {
let crawl = this.root
for (let char of word) {
// 将字母转换成对应的 0~25 下标
const index = this._char2Index(char)
// 如果当前字符没有对应的节点,那就新建一个节点
if (!crawl.pointers[index]) {
crawl.pointers[index] = this._getTrieNode('')
}
// 继续深入
crawl = crawl.pointers[index]
}
// 标识一下最后的节点,把 word 存在这里只是方式之一
// 也可以存储别的信息,看实际需求
crawl.value = word
}查找
删除
复杂度
哈希表 vs 前缀树
优点
缺点
应用
Last updated