字典树
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; }
相关文章推荐