You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Initialize your data structure here. */classTreeNode{constructor(){this.END=falsethis.children=newArray(26)}containKey(letter){returnthis.children[letter.charCodeAt()-97]!==undefined}getCh(letter){returnthis.children[letter.charCodeAt()-97]}putCh(letter,TrieNode){this.children[letter.charCodeAt()-97]=TrieNode}setEnd(){this.END=true}isEnd(){returnthis.END}}letroot=nullvarTrie=function(){root=newTreeNode()};/** * Inserts a word into the trie. * @param {string} word * @return {void} */Trie.prototype.insert=function(word){letcur=rootfor(leti=0;i<word.length;i++){if(!cur.containKey(word[i])){cur.putCh(word[i],newTreeNode())}cur=cur.getCh(word[i])}cur.setEnd()};letSearchPrefix=function(word){letcur=rootfor(leti=0;i<word.length;i++){if(cur.containKey(word[i]))cur=cur.getCh(word[i])elsereturnnull}returncur}/** * Returns if the word is in the trie. * @param {string} word * @return {boolean} */Trie.prototype.search=function(word){letcur=SearchPrefix(word)returncur!==null&&cur.isEnd()};/** * Returns if there is any word in the trie that starts with the given prefix. * @param {string} prefix * @return {boolean} */Trie.prototype.startsWith=function(prefix){returnSearchPrefix(prefix)!==null};/** * Your Trie object will be instantiated and called as such: * var obj = new Trie() * obj.insert(word) * var param_2 = obj.search(word) * var param_3 = obj.startsWith(prefix) */
前缀树
定义
前缀树又称单词查找树,字典树,Trie 树,是一种树形结构,是哈希树的一个变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

性质
操作
前缀树的主要操作为插入,查找,删除(删除操作很少见)
源码实现
leetcode题目
No.208 实现 Trie
No.677 键值映射
The text was updated successfully, but these errors were encountered: