首页 > 代码库 > Trie树 模板

Trie树 模板

typedef struct node{    int count;    struct node *next[MAX];}Trie;Trie *Newnode()//建立结点&初始化a{    int i;    Trie *T;    T = (Trie *)malloc(sizeof(Trie));    T->count = 0;    for(i=0;i<MAX;i++)        T->next[i] = NULL;    return T;}
void creatTrie(Trie *root, char *str)//创建{    int i;    int len = strlen(str);    int id;    Trie *p = root;    for(i=0;i<len;i++)    {        id = str[i]-a;        if(p->next[id] == NULL)            p->next[id] = Newnode();        p->next[id]->count++;        p = p->next[id];    }}
void findTrie(Trie *root, char *str)//查找{    int i;    int len = strlen(str);    int id;    Trie *p = root;    for(i=0;i<len;i++)    {        id = str[i]-a;        if(p->next[id] == NULL)            return ;        if(p->next[id]->count == 1){            //操作            return ;        }        if(p->next[id] > 1)        //操作        p = p->next[id];    }}
void delTrie(Trie *T)//删除字典树{    int i;    for(i=0;i<MAX;i++)        if(T->next[i] != NULL)            delTrie(T->next[i]);    free(T);}