字典树Trie_Tree
来源:互联网 发布:网络电视央视直播 编辑:程序博客网 时间:2024/06/14 05:22
#include<iostream>#include<string.h>using namespace std;struct Trie_Tree{ char *str; int nFlag; Trie_Tree *arr[26];};Trie_Tree *CreateNode(){ Trie_Tree *temp = new Trie_Tree; memset(temp,0,4*28); return temp;}void Insert_Trie_Tree(Trie_Tree **root,char *str){ if(root == NULL) { return ; }//Trie_Tree *temp = (*root); // char *strbak = str; // while((*strbak) != '\0') // { // if(temp->arr[(*strbak)-'a'] == NULL) // { // temp->arr[(*strbak)-'a'] = CreateNode(); // } // temp= temp->arr[(*strbak)-'a'];// strbak++; // } // temp->nFlag = 1; // temp->str = new char [strlen(str)+1]; // strcpy(temp->str,str); char *strbak = str; while((*strbak) != '\0') { if((*root)->arr[(*strbak)-'a'] == NULL) { (*root)->arr[(*strbak)-'a'] = CreateNode(); } root = &((*root)->arr[*strbak - 'a']);strbak++;} (*root)->nFlag = 1; (*root)->str = new char [strlen(str)+1]; strcpy((*root)->str,str);}void Create_Trie_Tree(Trie_Tree **root,char* arr[],int nLength){ if(root == NULL) { return ; } for(int i =0;i<nLength;i++) { Insert_Trie_Tree(root,arr[i]); }}void Print_Trie_Tree(Trie_Tree *root){ if(root == NULL) { return; } if(root->nFlag == 1) { cout<<root->str<<" "; } for(int i=0;i<26;i++) {Print_Trie_Tree(root->arr[i]); }}int Find_Trie_Tree(Trie_Tree *root,char *str){if(root == NULL || str == NULL){return 0;}while ((*str) != '\0'){if(root->arr[(*str)-'a'] == NULL){return 0;}root = root->arr[(*str)-'a'];str++;}if(root->nFlag == 1){return 1;}}int main(){Trie_Tree *root = CreateNode(); char *str[5]={"asdasdb","asdasd","fgdfg","ghg","xcvxcv"}; Create_Trie_Tree(&root,str,5); Print_Trie_Tree(root);cout<<endl<<Find_Trie_Tree(root,"oooooo");cout<<endl<<"-----------------------------------------"<<endl;Insert_Trie_Tree(&root,"oooooo");Print_Trie_Tree(root);cout<<endl<<Find_Trie_Tree(root,"oooooo")<<endl;system("pause");return 0;}
1 0
- 字典树trie_tree
- 字典树Trie_Tree
- Trie_tree
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 管理-OKR
- hdoj4941Magical Forest【stl map+离散化】
- ubantu的root用户
- Android具有粘性的小球,跌落反弹形成文字的动画效果
- 写在前面-本博客是与java相关的
- 字典树Trie_Tree
- Linux第二天02
- 不能再颓下去了,吧~
- 各种内部排序算法时间复杂度比较和排序方法的选择
- android单元测试
- error C2146 和C4430 错误
- I/O流
- Android中pendingIntent的深入理解
- DeepLearningToolBox学习——SAE(stacked auto encoders )