Trie
Trie的关键特性:
前缀搜索
类Trienode { constructor(){ this.Children = {}; //存储儿童节点 this.isendofword = false; //标记一个单词的结尾 } } 类Trie { constructor(){ this.root = new Trienode(); } //插入一个单词 插入(word){ 令node = this.root; for(让词字符){ 如果(!node.children [char]){ Node.Children [char] = new Trienode(); } node = node.Children [char]; } node.isendofword = true; //标记单词的结尾 } //搜索一个单词 搜索(Word){ 令node = this.root; for(让词字符){ 如果(!node.children [char]){ 返回false; } node = node.Children [char]; } 返回node.isendofword; } //检查是否有任何单词从给定的前缀开始 startswith(前缀){ 令node = this.root; 对于(让前缀的字符){ 如果(!node.children [char]){ 返回false; } node = node.Children [char]; } 返回true; } } //示例用法 const trie = new trie(); trie.insert(“苹果”); console.log(trie.search(“苹果”)); // 真的 console.log(trie.search(“ app”)); // 错误的 console.log(trie.startswith(“ app”)); // 真的 trie.insert(“ app”); console.log(trie.search(“ app”)); // 真的
1。
删除一个词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
3。
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; }
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3