【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;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值