字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word

字典树(查找树)

26个分支
作用:检测字符串是否在这个字典里面
插入、查找

字典树与哈希表的对比:
时间复杂度:以字符来看:O(N)、O(N) 以字符串来看:O(1)、O(1)
空间复杂度:字典树远远小于哈希表

前缀相关的题目字典树优于哈希表
字典树可以查询abc是否有ab的前缀

字典树常考点:
1.字典树实现
2.利用字典树前缀特性解题
3.矩阵类字符串一个一个字符深度遍历的问题(DFS+trie)
dfs树和trie树同时遍历


word searchII
dfs+hash:时间复杂度大,后面遍历到有些字符就不用遍历了。
剪枝

在写结构体struct的时候,注意必须在{}之后加分号,不然会编译报错。

//错误
struct TrieNode {
        TrieNode* child[26];
        bool isWord = false;
        TrieNode(){
            for(int i = 0;i < 26;i++)
                child[i] = NULL;
        }
    }
//正确
struct TrieNode {
        TrieNode* child[26];
        bool isWord = false;
        TrieNode(){
            for(int i = 0;i < 26;i++)
                child[i] = NULL;
        }
    };

leetcode 208. Implement Trie (Prefix Tree)

https://www.cnblogs.com/grandyang/p/4491665.html

注意:在insert或者add新的词的时候,必须在最后将isWord置为true,以表示这是一个单词的结尾。

class Trie {
public:
    struct TrieNode {
    public:
        TrieNode *child[26];
        bool isWord;
        TrieNode() : isWord(false) {
            for (auto &a : child) a = NULL;
        }
    };
    /** Initialize your data structure here. */
    Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        TrieNode* p = root;
        for(char w : word){
            int i = w - 'a';
            if(!p->child[i])
                p->child[i] = new TrieNode();
            p = p->child[i];
        }
        p->isWord = true;
        return;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        TrieNode* p = root;
        for(char w : word){
            int i = w - 'a';
            if(!p->child[i])
                return false;
            p = p->child[i];
        }
        return p->isWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        TrieNode* p = root;
        for(char w : prefix){
            int i = w - 'a';
            if(!p->child[i])
                return false;
            p = p->child[i];
        }
        return true;
    }
    TrieNode* root;
};

211. Add and Search Word - Data structure design

https://www.cnblogs.com/grandyang/p/4507286.html

class WordDictionary {
public:
    struct TrieNode {
    public:
        TrieNode *child[26];
        bool isWord;
        TrieNode() : isWord(false) {
            for (auto &a : child) a = NULL;
        }
    };
    /** Initialize your data structure here. */
    WordDictionary() {
        root = new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word) {
        TrieNode* p = root;
        for(char w : word){
            int i = w - 'a';
            if(!p->child[i])
                p->child[i] = new TrieNode();
            p = p->child[i];
        }
        p->isWord = true;
        return;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    bool search(string word) {
        return search_core(word,root,0);
    }
    bool search_core(string word,TrieNode* p,int index){
        if(index == word.size())
            return p->isWord;
        if(word[index] == '.'){
            for(TrieNode* tmp : p->child){
                if(tmp && search_core(word,tmp,index+1))
                    return true;
            }
            return false;
        }
        else{
            int i = word[index] - 'a';
            if(!p->child[i])
                return false;
            return search_core(word,p->child[i],index+1);
        }
    }
    TrieNode* root;
};
原文地址:https://www.cnblogs.com/ymjyqsx/p/10936139.html