Trie 是一种树状数据结构,用于高效存储和搜索字符串,特别是在自动完成、拼写检查和 IP 路由等应用程序中。
插入单词需要遍历特里树并为不存在的字符创建新节点。
搜索通过遍历单词的节点来检查单词是否存在于 trie 中。
这会检查 trie 中是否有任何单词以给定的前缀开头。
class TrieNode { constructor() { this.children = {}; // Stores child nodes this.isEndOfWord = false; // Marks the end of a word } } class Trie { constructor() { this.root = new TrieNode(); } // Insert a word insert(word) { let node = this.root; for (let char of word) { if (!node.children[char]) { node.children[char] = new TrieNode(); } node = node.children[char]; } node.isEndOfWord = true; // Mark the end of the word } // Search for a word search(word) { let node = this.root; for (let char of word) { if (!node.children[char]) { return false; } node = node.children[char]; } return node.isEndOfWord; } // Check if any word starts with the given prefix startsWith(prefix) { let node = this.root; for (let char of prefix) { if (!node.children[char]) { return false; } node = node.children[char]; } return true; } } // Example usage const trie = new Trie(); trie.insert("apple"); console.log(trie.search("apple")); // true console.log(trie.search("app")); // false console.log(trie.startsWith("app")); // true trie.insert("app"); console.log(trie.search("app")); // true
删除单词涉及递归方法,即删除不再需要的节点。
delete(word, node = this.root, depth = 0) { if (depth === word.length) { if (!node.isEndOfWord) return false; // Word doesn't exist node.isEndOfWord = false; return Object.keys(node.children).length === 0; // Check if node has children } const char = word[depth]; if (!node.children[char]) return false; const shouldDeleteChild = this.delete(word, node.children[char], depth + 1); if (shouldDeleteChild) { delete node.children[char]; return Object.keys(node.children).length === 0 && !node.isEndOfWord; } return false; }
计算有多少个单词以给定前缀开头。
countWordsWithPrefix(prefix) { let node = this.root; for (let char of prefix) { if (!node.children[char]) return 0; node = node.children[char]; } return this._countWords(node); } _countWords(node) { let count = node.isEndOfWord ? 1 : 0; for (let char in node.children) { count += this._countWords(node.children[char]); } return count; }
给定前缀,返回以其开头的所有单词。
getWordsWithPrefix(prefix) { let node = this.root; for (let char of prefix) { if (!node.children[char]) return []; node = node.children[char]; } return this._collectWords(node, prefix); } _collectWords(node, prefix) { let results = []; if (node.isEndOfWord) results.push(prefix); for (let char in node.children) { results = results.concat(this._collectWords(node.children[char], prefix + char)); } return results; }
以上是Trie(前缀树)简介的详细内容。更多信息请关注PHP中文网其他相关文章!