您的位置:首页 > 其它

字典树

2016-12-13 21:52 267 查看
//结构体定义
struct node{
node *next[60];
};

//建立
node *creat(){
node *p=new node;

for(int i=0;i<60;i++){
p->next[i]=NULL;
}

return p;
}

//插入
void insert(struct node **root,string s){
node *p;
if(!(p=*root)){
p=*root=creat();
}

int i=0;
while(s[i]){
int k=s[i++]-'a';
if(!(p->next[k])){
p->next[k]=creat();
}

p=p->next[k];
}
}

//查找
bool search(struct node **root,string s){
node *p;
if(!(p=*root)){
return false;
}

int i=0;
while(s[i]){
int k=s[i++]-'a';
if(!(p->next[k]))
return false;
p=p->next[k];
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: