资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
为了适应公司新战略的发展,保障停车场安保新项目的正常、顺利开展,特制定安保从业人员的业务技能及个人素质的培训计划trie树实验报告/Trie树.cpp:Definestheentrypointfortheconsoleapplication./#include/此函数只考虑26个英文字母的情况#include#includeusingnamespacestd;#defineMAX_CHILD26typedefstructTreeintcount;/用来标记该节点是个可以形成一个单词,如果count!=0,则从根节点到该节点的路径可以形成一个单词structTree*childMAX_CHILD;Node,*Trie_node;Node*CreateTrie()/创建trie节点树voidinsert_node(Trie_noderoot,char*str)/trie树插入结点Node*node=(Node*)malloc(sizeof(Node);memset(node,0,sizeof(Node);returnnode;if(root=NULL|*str=0)return;Node*t=root;char*p=str;while(*p!=0)if(t-child*p-a=NULL)Node*tmp=CreateTrie();t-child*p-a=tmp;t=t-child*p-a;p+;t-count+;voidsearch_str(Trie_noderoot,char*str)/trie树中if(NULL=root|*str=0)printf(trieisemptyorstrisnulln);return;查找串是否在该Node*t=root;while(*p!=0)if(*p=0)elsecoutcount=0)coutchild*p-a!=NULL)elsebreak;t=t-c(来自:写论文网:trie树实验报告)hild*p-a;p+;elsecoutchildi!=NULL)free(root);int_tmain(intargc,_TCHAR*argv)return0;inti,n;charstr20;coutn;Trie_noderoot=NULL;root=CreateTrie();if(root=NULL)cout3usingnamespacestd;45constintbranchNum=26;/声明常量6inti;78structTrie_node910boolisStr;/记录此处是否构成一个串。11Trie_node*nextbranchNum;/指向各个子树的指针,下标0-25代表26字符1213141516171819202122Trie_node():isStr(false)memset(next,NULL,sizeof(next);classTriepublic:Trie();voidinsert(constchar*word);23boolsearch(char*word);24voiddeleteTrie(Trie_node*root);25private:26Trie_node*root;27;2829Trie:Trie()3031root=newTrie_node();323334voidTrie:insert(constchar*word)3536Trie_node*location=root;37while(*word)3839if(location-next*word-a=NULL)/不存在则建立4041Trie_node*tmp=newTrie_node();42location-next*word-a=tmp;4344location=location-next*word-a;/每插入一步,相当于有一个新串经过,指针要向下移动454647484950515253545556575859606162636465word+;location-isStr=true;/到达尾部,标记一个串boolTrie:search(char*word)Trie_node*location=root;while(*word&location)location=location-next*word-a;word+;return(location!=NULL&location-isStr);voidTrie:deleteTrie(Trie_node*root)for(i=0;inexti!=NULL)目的-通过该培训员工可对保安行业有初步了解,并感受到安保行业的发展的巨大潜力,可提升其的专业水平,并确保其在这个行业的安全感。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号