Test instructions: Implements the 3 functions of the trie tree with only lowercase strings.
Idea: be honest!
1 classTrienode {2 Public:3trienode* chd[ -];4 BOOLFlag;5 //Initialize your data structure here.6 Trienode () {7memset (CHD,0,sizeof(CHD));8flag=0;9 }Ten One }; A - classTrie { - Public: the Trie () { -Root =NewTrienode (); - } - + //inserts a word into the trie. - voidInsertstringword) { + intp=0; Atrienode* tmp=Root; at while(p<word.size ()) - { - if(!tmp->chd[word[p]-'a']) - { -trienode* newnode=NewTrienode (); -tmp->chd[word[p]-'a']=NewNode; in } -tmp=tmp->chd[word[p]-'a']; top++; + } -tmp->flag=1; the } * $ //Returns If the word is in the trie.Panax Notoginseng BOOLSearchstringword) { - intp=0; thetrienode* tmp=Root; + while(p<word.size ()) A { the //cout<<word[p]<< ""; + if(tmp->chd[word[p]-'a']) tmp=tmp->chd[word[p]-'a']; - Else return false; $p++; $ } - if(tmp->flag==1)return true; - return false; the } - Wuyi //Returns If there is any word in the trie the //That's starts with the given prefix. - BOOLStartsWith (stringprefix) { Wu intp=0; -trienode* tmp=Root; About while(p<prefix.size ()) $ { - if(tmp->chd[prefix[p]-'a']) tmp=tmp->chd[prefix[p]-'a']; - Else return false; -p++; A } + return true; the } - $ Private: thetrienode*Root; the};AC Code
Leetcode Implement Trie (Prefix tree) (Implement Trie trees 3 functions: Insert, find, prefix)