字典树/前缀树/Trie

208. 实现 Trie (前缀树)

class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        isEnd = false;
        memset(next, 0, sizeof(next));
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie* t = this;
        for(auto& c : word){
            if(t->next[c-'a'] == NULL){
                t->next[c-'a'] = new Trie();
            }
            t = t->next[c-'a'];
        }
        t->isEnd = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie* t = this;
        for(auto& c : word){ 
            if(t->next[c-'a'] == NULL) return false;
            t = t->next[c-'a'];
        }
        return t->isEnd;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Trie* t = this;
        for(auto& c : prefix){
            if(t->next[c-'a'] == NULL) return false;
            t = t->next[c-'a'];
        }
        return true;
    }
private:
    Trie* next[26];
    bool isEnd;
};

/**
 * 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);
 */

677. 键值映射

// 带有键值映射的版本
class Trie {
public:
    /** Initialize your data structure here. */
    Trie() 
    : isEnd(false),
      val(0)
    {
        memset(next, 0, sizeof(next));
    }
    
    /** Inserts a word into the trie. */
    void insert(string word, int val) {
        Trie* t = this;
        for (auto& c : word) {
            if (t->next[c - 'a'] == nullptr) {
                t->next[c - 'a'] = new Trie();
            }
            t = t->next[c - 'a'];
        }
        t->isEnd = true;
        t->val = val;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie* t = this;
        for (auto& c : word) { 
            if (t->next[c - 'a'] == nullptr) return false;
            t = t->next[c - 'a'];
        }
        return t->isEnd;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        Trie* t = this;
        int ans = 0;
        for (auto& c : prefix) {
            if (t->next[c - 'a'] == nullptr) return false;
            t = t->next[c - 'a'];
        }
        return true;
    }

    /** Returns sum of val of root. */
    int rootSum(Trie* root) {
        int ans = root->val;
        for (auto& node : root->next) {
            if (node == nullptr) continue;
            ans += rootSum(node);
        }
        return ans;
    }

    /** Return sum of val which start with prefix. */
    int startWithSum(string prefix) {
        Trie* t = this;
        for (auto& c : prefix) {
            if (t->next[c - 'a'] == NULL) return 0;
            t = t->next[c - 'a'];
        }
        return rootSum(t);
    }

private:
    Trie* next[26];
    bool isEnd;
    int val;
};
posted @ 2020-12-27 22:51  miyanyan  阅读(59)  评论(0编辑  收藏  举报