您的位置:首页 > 其它

字典树模版

2015-12-03 18:02 309 查看
定义结构体:

#define Max 13
struct 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: