本文共 3239 字,大约阅读时间需要 10 分钟。
Trie树是一种多叉树结构,通常用来统计和排序大量的字符串。
上图所示就是一棵Trie树。该树包含的字符串集合为{“at”, “bee”, “ben”, “bt”, “q”}。
Trie树有如下特点:Trie树是以空间换时间。用字符串的公共前缀来降低查询时间开销以达到提高效率的目的。
优点:缺点:
利用字符串创建一棵Trie树,这个Trie树保留了字符串的公共前缀。以小写英文单词创建字典树为例,每一个节点都有26个孩子(有26个字母)。
结构如下:
//Trie nodetypedef struct TrieNode{ int count;//统计单词前缀出现的次数 bool exist; //该节点是否存在 struct TrieNode* next[26];//指向子树的指针}TrieNode, *Trie;
#include#include #include #include #include #pragma warning(disable:4996)//vs//Trie nodetypedef struct TrieNode{ int count;//统计单词前缀出现的次数 struct TrieNode* next[26];//指向子树的指针 bool exist; //该节点是否存在}TrieNode, *Trie;//创建一个节点TrieNode* CreateTrieNode(){ TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode)); assert(node); //if (node == NULL) //{ // printf("Create Trie node error: Out of memoey!\n"); // return NULL; //} node->count = 0; node->exist = false; memset(node->next, 0, sizeof(node->next)); return node;}//插入到Trie中void TrieInsert(Trie root, char* word){ TrieNode* node = root; char* p = word; int id = 0; //遍历每一个char while (*p) { id = *p - 'a'; // find the char position in the child if (node->next[id] == NULL) //不存在*p这条路径 { node->next[id] = CreateTrieNode(); } node = node->next[id];//指针向下移动.这里必须明白:每一个节点代表的是从根节点到此节点路径上所有字符组成的单词 node->count++;//前缀加1 ++p; } printf("insert %s ok!\n", word); node->exist = true;}//查找,和插入类似,返回以此字符为前缀的单词的个数int TrieSearch(Trie root, char* word){ TrieNode* node = root; char* p = word; int id; while (*p) { id = *p - 'a'; node = node->next[id]; if (node == NULL) { printf("not find the word:%s\n", word); return 0; } ++p; } printf("find the word:%s\n", word); return node->count;}int main(){ freopen("read.txt", "r", stdin); Trie root = CreateTrieNode();//init char str[100][20];//100个单词,每个单词有20个字符 int n = 0; while (~scanf("%s", str[n])) { TrieInsert(root, str[n++]); } for (int i = 0; i < 10; ++i) { TrieSearch(root, str[i]); } //无法检测的情况 char str1[20] = "carbin"; TrieSearch(root, str1); return 0;}/*read.txt内容如下:carbohydratecartcarburetorcaramelcariboucarboniccartilagecarboncarriagecartoncarcarbonateoutput:insert carbohydrate ok!insert cart ok!insert carburetor ok!insert caramel ok!insert caribou ok!insert carbonic ok!insert cartilage ok!insert carbon ok!insert carriage ok!insert carton ok!insert car ok!insert carbonate ok!find the word:carbohydratefind the word:cartfind the word:carburetorfind the word:caramelfind the word:cariboufind the word:carbonicfind the word:cartilagefind the word:carbonfind the word:carriagefind the word:cartonnot find the word:carbin*/
转载地址:http://hmmxb.baihongyu.com/