字典树(前缀树)

实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作。

示例:

Trie trie = new Trie();

trie.insert("apple");
trie.search("apple"); // 返回 true
trie.search("app"); // 返回 false
trie.startsWith("app"); // 返回 true
trie.insert("app");
trie.search("app"); // 返回 true
说明:

你可以假设所有的输入都是由小写字母 a-z 构成的。
保证所有输入均为非空字符串。

//C++ 规定,在非静态成员函数内部可以直接使用 this 关键字,this 就代表指向该函数所作用的对象的指针
//静态成员函数并不作用于某个对象,所以在其内部不能使用 this 指针;否则,这个 this 指针该指向哪个对象呢?
class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        //构造函数初始化
        memset(next,NULL,sizeof(next));
        IsWord = false;
    }
    //析构
    ~Trie(){
        for(int i=0;i<26;i++){
            if(next[i])
                delete next[i];
        }
    }
    /** Inserts a word into the trie.Tire树的插入 */
    void insert(string word) {
        Trie* node = this;
        for(int i=0;i<word.size();i++){
            //例如插入apple , 再插入app,node->next已经非空了.app结尾的node也是true
            if(!node->next[(word[i]-'a')]){
                node->next[(word[i]-'a')] = new Trie();
            }
            node = node->next[(word[i]-'a')];
        }
        //默认节点IsWord为false,搜索的时候如果这个节点是某个单词的结尾节点,则Iswordweitrue,否则不是某个单词的尾节点
        node->IsWord = true;
    }
    
    /** Returns if the word is in the trie. 搜索*/
    bool search(string word) {
        Trie* node = this;
        for(int i=0;i<word.size();i++){
            if(node->next[word[i]-'a']){
                node = node->next[word[i]-'a'];
            }else{
                return false;
            }
        }
        return node->IsWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix.查找是否存在以某个Prefix开头的单词 */
    bool startsWith(string prefix) {
        Trie* node = this;
        for(int i=0;i<prefix.size();i++){
            if(node->next[prefix[i]-'a']){
                node = node->next[prefix[i]-'a'];
            }else{
                return false;
            }
        }
        return true;
    }
private:
    //26个节点的子树(每个节点中value为a-z),这里省一个value值(不同于二叉树),直接用next下标来表示
    Trie* next[26];
    bool IsWord;
};

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

 

//用map实现

//用map实现
class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        IsWord = false;
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
        Trie* node = this;
        for(auto c:word){
            if(!node->next[c]){
                node->next[c] = new Trie();
            }
            node = node->next[c];
        }
        node->IsWord = true;
    }
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
        Trie* node = this;
        for(auto c:word){
            if(node->next[c]){
                node = node->next[c];
            }else{
                return false;
            }
        }
        return node->IsWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
         Trie* node = this;
         for(auto c:prefix){
            if(node->next[c]){
                node = node->next[c];
            }else{
                return false;
            }
        }
        return true;
    }
private:
    map<char,Trie*> next;
    bool IsWord;
};

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

 

posted on 2020-12-09 20:44  wsw_seu  阅读(101)  评论(0编辑  收藏  举报

导航