字典树Trie-C++兑现

字典树Trie-C++实现转自?http://blog.csdn.net/topcoder1234/article/details/5887653。注意:在这里并没有

字典树Trie-C++实现

转自?http://blog.csdn.net/topcoder1234/article/details/5887653。

注意:在这里并没有专门一个CHAR来存储字符,而是通过位置来确定是哪个字符,num = str[i] - 'a';

?

struct node{bool isWord;node *next[26];node(){isWord = false;for(int i=0;i<26;i++)next[i] = NULL;}};class Trie{public:node *root;Trie(){root = NULL;}void insert(string str){if(!root)root = new node;node *location = root;for(int i=0;i<str.length();i++){int num = str[i] - 'a';if(location->next[num] == NULL){location->next[num] = new node;}location = location->next[num];}location->isWord = true;}bool search(string str){node *location = root;for(int i=0;i<str.length();i++){int num = str[i] - 'a';if(location->next[num] == NULL)return false;location = location->next[num];}return location->isWord;}};