Trie
是一種類似樹的數據結構,用於有效存儲和搜索字符串,尤其是在諸如AutoComplete,Spell Checking和IP路由之類的應用程序中。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