字典树--模板
<<字典树的模板>>
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//字典树的节点的结构
typedef struct node{
struct node *br[26];
int num;
}node;
node *head;
//字典树的构建
void Tree_Insert(char str[]){
node *t , *s = head;
int i , j;
int len = strlen(str) - 1;
for(i = 0 ; i <= len ; i++){
int id = str[i] -'a' ;
if(s -> br[id] == NULL){//如果该字节点的字节点为空则要创建中间节点
t = new node;
for(j = 0 ; j <= 25 ; j++){
t -> br[j] = NULL;
}
t -> num = 0;
s -> br[id] = t;
}
s = s -> br[id];
s -> num++;
}
}
//字典树的查找
int Tree_Find(char str[]){
node *s = head;
int count ;
int len = strlen(str) - 1;
for(int i = 0 ; i <= len ; i++){
int id = str[i] - 'a';
if(s -> br[id] ==NULL){//如果当前指针s的对应于当前字符str[i]在字母表的位置的子节点为空
count = 0;
return count;//直接返回0
}
else{
s = s -> br[id];
count = s -> num ;
}
}
return count;
}
int main(){
int i , j;
//建立一个head分配空间
head = new node;
//树的初始化
for(i = 0 ; i <= 25 ;i++){
head -> br[i] = NULL;
head -> num = 0;
}
return 0;
}