diff --git a/leetcode/p208_trie.cpp b/leetcode/p208_trie.cpp new file mode 100644 index 0000000..22aa1d7 --- /dev/null +++ b/leetcode/p208_trie.cpp @@ -0,0 +1,97 @@ +class TrieNode { +public: + // initializing one node + TrieNode(){ + is_word_complete = false; + // each node can have at most 26 children: a to z. + for(int i=0;i<26;i++){ + children.push_back(nullptr); + } + } + // so that we can access private member variables. + friend class Trie; +private: + bool is_word_complete; + std::vector children; +}; + +class Trie { +public: + // initializing the prefix tree + Trie() { + // which basically is initializing the root + root = new TrieNode; + } + + void insert(string word) { + int len = word.length(); + TrieNode* current = root; + for(int i=0; ichildren[word[i]-'a'] == nullptr){ + // create a node + TrieNode* node = new TrieNode; + current->children[word[i]-'a'] = node; + // after the insertion, update current + current = current->children[word[i]-'a']; + }else{ + // else, it means this node is already there, just update current + // we make the code duplicated here, but this way it's easier to understand. + current = current->children[word[i]-'a']; + } + } + // when we get out of the for loop, the whole word should have been inserted already, we mark this node as the end of a word. + current->is_word_complete = true; + } + + bool search(string word) { + int len = word.length(); + // once again, starting from root + TrieNode* current = root; + for(int i=0; ichildren[word[i]-'a'] == nullptr){ + return false; + }else{ + // else update current and keep checking never level + current = current->children[word[i]-'a']; + } + } + // when we get out of the for loop, the whole word is found, but we still need to make sure this node marks the end of a previously inserted word. + if(current->is_word_complete == true){ + return true; + }else{ + // else, it will be like, we are searching for app, but previously we inserted an apple. + return false; + } + } + + bool startsWith(string prefix) { + int len = prefix.length(); + // once again, starting from root + TrieNode* current = root; + for(int i=0; ichildren[prefix[i]-'a'] == nullptr){ + return false; + }else{ + // else update current and keep checking never level + current = current->children[prefix[i]-'a']; + } + } + // since we are only searching for a prefix, when we get here, clearly we found it. + return true; + } +private: + // just like every tree, we should have a root. + 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); + */