classTrie{public:Trie(){root=newTrieNode();}classTrieNode// 内部类{public:boolisWord=false;TrieNode*children[26];// 数组TrieNode(){for(inti=0;i<26;i++){children[i]=nullptr;}}~TrieNode(){for(inti=0;i<26;i++){deletechildren[i];children[i]=nullptr;}}};/** Inserts a word into the trie. */voidinsert(string word){TrieNode*node=root;for(inti=0;i<word.length();i++){intindex=word[i]-'a';if(node->children[index]==nullptr){node->children[index]=newTrieNode();}node=node->children[index];}node->isWord=true;}/** Returns if the word is in the trie. */boolsearch(string word){TrieNode*node=root;for(inti=0;i<word.length();i++){intindex=word[i]-'a';if(node->children[index]==nullptr){returnfalse;}node=node->children[index];}returnnode->isWord;}/** Returns if there is any word in the trie that starts with the given prefix. */boolstartsWith(string prefix){TrieNode*node=root;for(inti=0;i<prefix.length();i++){intindex=prefix[i]-'a';if(node->children[index]==nullptr){returnfalse;}node=node->children[index];}returntrue;}private:TrieNode*root;};