Trie Tree Templates
https://leetcode.com/problems/implement-trie-prefix-tree/
classTrienode { Public: Char var; BOOLIsword; Trienode*next[ -]; Trienode () {var=0; This->isword =false; for(auto &c:next) c =NULL; } trienode (Charc) {var=C; This->isword =false; for(auto &c:next) c =NULL; }};classTrie { Public: Trie () {root=NewTrienode (); } //inserts a word into the trie. voidInsertstringword) {Trienode*p =Root; for(Auto &A:word) { intIDX = A-'a'; if(!p->next[idx]) p->next[idx] =NewTrienode (); P= p->Next[idx]; } P->isword =true; } //Returns If the word is in the trie. BOOLSearchstringword) {Trienode*p =Root; for(Auto &A:word) { intIDX = A-'a'; if(!p->next[idx])return false; P= p->Next[idx]; } returnP->Isword; } //Returns If there is any word in the trie//That's starts with the given prefix. BOOLStartsWith (stringprefix) {Trienode*p =Root; for(Auto &A:prefix) { intIDX = A-'a'; if(!p->next[idx])return false; P= p->Next[idx]; } return true; }Private: Trienode*root;};//Your Trie object would be instantiated and called as such://Trie Trie;//Trie.insert ("somestring");//trie.search ("key");
[email protected] [208] Implement Trie (Prefix Tree)