Sunday, July 23, 2017

Implement Trie (Prefix Tree)



Implement a trie with insertsearch, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.

class TrieNode {
    public:
    vector<TrieNode *> child;
    bool isWord;
    TrieNode(): isWord(false) {
        for (int i = 0; i < 26; i++) {
            child.push_back(NULL);
        }
    }
};

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

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


=========== Another attempt ======
class TrieNode {
public:
    TrieNode() {
        for (int i = 0; i < 26; i++) {
            child[i] = NULL;
        }
        is_word = false;
    }
    TrieNode* child[26];
    bool is_word;
};

class Trie {
public:
    /** Initialize your data structure here. */
    Trie() {
        root = new TrieNode();
    }
   
    /** Inserts a word into the trie. */
    void insert(string word) {
        TrieNode *trav = root;
        // Assume word is non empty.
        for (int i = 0; i < word.size(); i++) {
            char ch = word[i];
            if (trav -> child[ch - 'a'] == NULL) {
                trav -> child[ch - 'a'] = new TrieNode();
            }
            trav = trav -> child[ch - 'a'];
        }
        trav -> is_word = true;
    }
   
    /** Returns if the word is in the trie. */
    bool search(string word) {
        TrieNode *trav = root;
        for (int i = 0; i < word.size(); i++) {
            char ch = word[i];
            if (trav -> child[ch - 'a'] == NULL) {
                return false;
            }
            trav = trav -> child[ch - 'a'];
        }
        return trav -> is_word;
    }
   
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
        TrieNode *trav = root;
        for (int i = 0; i < prefix.size(); i++) {
            char ch = prefix[i];
            if (trav -> child[ch - 'a'] == NULL) {
                return false;
            }
            trav = trav -> child[ch - 'a'];
        }
        return true;
    }
private:
    TrieNode* root;
};

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

No comments:

Post a Comment