字典树模板

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 //字典树的数据结构
 6 struct Trie{
 7     Trie *child[26];//这里数组的大小看是小写字母还是数字还是都有
 8     int num;
 9     Trie(){//初始化
10         num = 0;
11         memset(child , 0 , sizeof(child));
12     }
13 };
14 Trie *root;
15 //字典树的构建
16 void Tree_Insert(char *str){
17     Trie *s = root;
18     int i = 0;
19     while(str[i]){
20         int id = str[i] -'a' ;
21         if(s -> child[id] == 0)//如果该字节点的字节点为空则要创建中间节点
22             s -> child[id] = new Trie();
23         s = s -> child[id];
24 
25         s -> num++;
26 
27         i++28     }  
29 }
30 //字典树的查找
31 int Tree_Find(char *str){
32     Trie *s = root;
33     int count ;
34     while(str[i]){
35         int id = str[i] - 'a';
36         if(s -> child[id] == 0){//如果当前指针s的对应于当前字符str[i]在字母表的位置的子节点为空
37             return count;//直接返回0
38         }
39         else{
40             s = s -> child[id];
41             count = s -> num ;
42 
43         }
44 
45         i++;
46     }
47     return count;
48 }
49 int main(){
50    int i , j;
51    //建立一个root分配空间
52 
53    root = new Trie();
54 
55   
56    return 0;
57 }
58     
59     
原文地址:https://www.cnblogs.com/10jschen/p/2646085.html