Trie tree of leetcode

1. Introduction of Trie tree

Trie tree definition

Yes, he is such a tree

Insert picture description here
Red indicates a word, and white indicates a prefix.

Trie tree application

Trie tree

2.Trie tree code

3. Examples

  • Implementation of 208, Trie tree in Leetcode

Note: It can be understood that the side saves letters and the click saves isWord,
so after traversing all the letters, judge whether it is a word again.

class Trie {

private:
    bool isEnd;
    Trie *next[26];
public:
    /** Initialize your data structure here. */
    Trie() {
        isEnd = false;
        memset(next, 0, sizeof(next));
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        //1.插入单词,如果没有则创建新的节点,最后将isEnd设置成true;
        //2.循环中node = node->next;
        //3.使用this指针,头结点是空的
        Trie* node = this;
        for (char c : word) {
            if (node->next[c-'a'] == NULL) {
                node->next[c-'a'] = new Trie();
            }
            node = node->next[c-'a'];
        }
        node->isEnd = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        //1.查找,如果中途为NULL return false;, 如果不是end return false;
        Trie *node = this;
        for(char ch : word){
            int c = ch - 'a';
            if(node->next[c] == NULL)
                return false;
            node = node->next[c];
        }
        return node->isEnd;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        //同search 只不过不需要进行isEnd判断,因为只要prefix使用完,就一定是前缀
        Trie *node = this;
        for(char ch: prefix){
            int c = ch - 'a';
            if(node->next[c] == NULL)
                return false;
            node = node->next[c];
        }
        return true;
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */
  • LeetCode211

Trie tree + dfs for fuzzy query

class WordDictionary {
private:
    bool isWord;
    WordDictionary *next[26];
public:
    /** Initialize your data structure here. */
    WordDictionary() {
        this->isWord = false;
        memset(next, 0, sizeof(next));
    }
    
    void addWord(string word) {
        WordDictionary *node = this;
        for(char ch: word){
            int c = ch - 'a';
            if(node->next[c] == NULL)
                node->next[c] = new WordDictionary();
            node = node->next[c];
        }
        node->isWord = true;
    }

    //判断是否存在
    bool dfs(string &word, WordDictionary *node, int cur){
        if(cur == word.size()) return node->isWord;     //边上存字母,点上存对错
        char ch = word[cur];
        if(ch != '.'){                            //特定匹配
            if(node->next[ch - 'a'] == NULL)      //如果没有就是错
                return false;
            return dfs(word, node->next[ch - 'a'], cur + 1);//继续判断以后的字符
        }
        for(int i = 0; i < 26; i++){        //26个英文字母有1个dfs是true就是true;
            if(node->next[i] != NULL &&
                dfs(word, node->next[i], cur + 1)
            ){
                return true;
            }
        }
        return false;
    }
    bool search(string word) {
        return dfs(word, this, 0);
    }
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary* obj = new WordDictionary();
 * obj->addWord(word);
 * bool param_2 = obj->search(word);
 */

Guess you like

Origin blog.csdn.net/fuzekun/article/details/113899909