字典树模版

来源:互联网 发布:介绍人工智能方面的书 编辑:程序博客网 时间:2024/04/30 08:20

定义结构体:

#define Max 13struct dot{dot *next[Max];int flag;} ;

产生新节点:

dot *newnode(){dot *temp=new dot;     temp->flag=0;for(int i=0;i<Max;i++)   temp->next[i]=NULL;return temp;}

这段代码根据要求可以不同形式的:

建字典树:
void tree(char *st,dot *root,int &k){dot *p=root;int id=0;for(int i=0;i<strlen(st);i++){id=st[i]-'a';if(p->next[id]==NULL){k++;p->next[id]=newnode();}p=p->next[id]; }}
删除操作:

void del(dot *t){if(t==NULL) return ;for(int i=0;i<Max;i++)   if(t->next[i]!=NULL)      del(t->next[i]);    delete t;}



0 0