【LeetCode】208. Implement Trie (Prefix Tree)

208. Implement Trie (Prefix Tree)

Description:
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
All inputs are guaranteed to be non-empty strings.
Difficulty:Medium
Example:

Trie trie = new Trie();

trie.insert("apple");
trie.search("apple");   // returns true
trie.search("app");     // returns false
trie.startsWith("app"); // returns true
trie.insert("app");   
trie.search("app");     // returns true

方法1:固定分配空间

思路:
预先分配100000*26大小的二维数组
这个100000是所有字符串的总长,举例说明插入操作,“cab”

c是第一个要插入的字符,'c'-'a'=2,trie[0][2]=1;
a是第二个要插入的字符,'a'-'a'=0,trie[1][0]=2;
c是第三个要插入的字符,'b'-'a'=1,trie[2][2]=3;
class Trie {
public:
    /** Initialize your data structure here. */
    const int maxn = 100000;
    int trie[100000][26];
    bool word_end[100000];
    int tot = 0;
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        int root = 0;
        for(int i = 0; i < word.size(); i++){
            int id = word[i] - 'a';
            if(!trie[root][id])
                trie[root][id] = ++tot;
            root = trie[root][id];
        }
        word_end[root] = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        int root = 0;
        for(int i = 0; i < word.size(); i++){
            int id = word[i] - 'a';
            if(!trie[root][id])
                return false;
            root = trie[root][id];
        }
        if(word_end[root] == true)
            return true;
        else
            return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        int root = 0;
        for(int i = 0; i < prefix.size(); i++){
            int id = prefix[i] - 'a';
            if(!trie[root][id])
                return false;
            root = trie[root][id];
        }
        return true;
    }
};

方法2:动态分配

思路:
动态分配新的节点

class TrieNode{
public:
    TrieNode * branches[26];
    bool is_end;
    TrieNode(bool end=false){
        memset(branches, 0, sizeof(branches));
        is_end = end;
    }
};


class Trie {
public:
    /** Initialize your data structure here. */
    TrieNode* root;
    Trie(){
        root = new TrieNode();
    }
    ~Trie(){
        destory(root);
    }
    
    void destory(TrieNode* node){
        for(int i = 0; i < 26; i++){
            if(node->branches[i])
                destory(node->branches[i]);
        }
        delete node;
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        TrieNode* node = root;
        int i;
        for(i = 0; i < word.size(); i++){
            int id = word[i] - 'a';
            if(!node->branches[id])
                break;
            else{
                node = node->branches[id];
                node->is_end = (i == word.size()-1) ? true : node->is_end;
            }
        }
        for(; i < word.size(); i++){
            int id = word[i] - 'a';
            node->branches[id] = new TrieNode((i == word.size()-1) ? true : node->is_end);
            node  = node->branches[id];
        }
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        TrieNode* node = root;
        for(int i = 0; i < word.size(); i++){
            int id = word[i] - 'a';
            if(!node->branches[id])
                return false;
            node  = node->branches[id];
        }
        if(node->is_end == true)
            return true;
        else
            return false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
     TrieNode* node = root;
        for(int i = 0; i < prefix.size(); i++){
            int id = prefix[i] - 'a';
            if(!node->branches[id])
                return false;
            node  = node->branches[id];
        }
        return true;
    }
};

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页