int main(){ int n; printf("\nEnter the number of strings you want to add: "); scanf("%d",&n); int i; char data[100]; TNode* root=NULL; for(i=0;i<n;i++){ //printf("\nEnter the length of string you want to add : "); printf("\nEnter the string you want to add : "); scanf("%s",data); InsertTrie(&root,data); } printf("\%d words added ",n); getch(); printf("\nEnter the string you want to search : "); scanf("%s",data); int result=SearchTrie(root,data); if(result){ printf("\nWord found !Hurray!"); }else{ printf("\nSorry! word not found !"); } char* buffer=(char*)malloc(sizeof(char)*100); printf("\nPress enter to print all the words in the dictionary you just created :\n"); getch(); PrintAll(root,buffer,0); getch(); return 0; }
void main() { TrieTree t; int i; char s[MAXKEYLEN+1]; KeysType k; Record *p; Record r[N]={{{"CAI"},1},{{"CAO"},2},{{"LI"},3},{{"LAN"},4}, {{"CHA"},5},{{"CHANG"},6},{{"WEN"},7},{{"CHAO"},8}, {{"YUN"},9},{{"YANG"},10},{{"LONG"},11},{{"WANG"},12}, {{"ZHAO"},13},{{"LIU"},14},{{"WU"},15},{{"CHEN"},16}}; /* 数据元素(以教科书式9-24为例) */ InitDSTable(&t); for(i=0;i<N;i++) { r[i].key.num=strlen(r[i].key.ch)+1; r[i].key.ch[r[i].key.num]=Nil; /* 在关键字符串最后加结束符 */ p=SearchTrie(t,r[i].key); if(!p) InsertTrie(&t,&r[i]); } printf("按关键字符串的顺序遍历Trie树(键树):\n"); TraverseDSTable(t,pr); printf("\n请输入待查找记录的关键字符串: "); scanf("%s",s); k.num=strlen(s)+1; strcpy(k.ch,s); k.ch[k.num]=Nil; /* 在关键字符串最后加结束符 */ p=SearchTrie(t,k); if(p) pr(p); else printf("没找到"); printf("\n"); DestroyDSTable(&t); }
int main(int argc, char *argv[]) { BuildTrie("/var/tmp/tw106.txt"); FILE *fp; fp = fopen(argv[1], "r"); char queryword[25]; while(fscanf(fp,"%s",queryword) != EOF) { MIN_COST = strlen(queryword); //in the worst case MIN_COST = strlen(queryword) SearchTrie(queryword); TOTAL_MIN_COST = TOTAL_MIN_COST + MIN_COST; } printf("%d\n",TOTAL_MIN_COST); fclose(fp); return 1; }