void deleteTrie(Trie root){ int i; for (i = 0; i < 26; i++){ if (root->children[i]) deleteTrie(root->children[i]); } free(root); }
void Dictionary::deleteTrie(Trie traverse) { for(int i = 0; i < ALPHABET_SIZE; i++) { if(traverse->next[i] != nullptr) { deleteTrie(traverse->next[i]); } } delete traverse; }
int deleteTrie(Trie* T){ int i; if(T==NULL) return 0; for(i=0; i<MAX; i++) if(T->next[i]!=NULL) deleteTrie(T->next[i]); delete(T); return 0; }
int main(){ char str[3000]; char mar[15], eng[15]; char word[3000]; int len, i; char ch; scanf("%s", str); getchar(); Trie tree = createNode(); while (scanf("%s", eng) && strcmp(eng, "END")){ scanf("%s", mar); //eng = strtok(str, " "); //mar = strtok(NULL, " "); insert(tree, eng, mar); } scanf("%s", str); getchar(); while (gets(str) && strcmp(str, "END")){ len = strlen(str); str[len] = ' '; len++; int idx = 0; for (i = 0; i < len; i++){ ch = str[i]; if (ch >= 'a'&&ch <= 'z'){ word[idx] = ch; idx++; } else{ word[idx] = '\0'; char *english = find(tree, word); if (english == NULL){ printf("%s", word); } else{ printf("%s", english); } if (i != len - 1) printf("%c", ch); memset(word, 0, sizeof(word)); idx = 0; } } printf("\n"); } deleteTrie(tree); }
void deleteTrie( trieNode<T>* root ) { //Check each item of children array and call deleteTrie recursivly for values not equal to NULL for(int i = 0; i < ARRAY_SIZE; ++i ) { if( root->children[i] != NULL ) deleteTrie( root->children[i] ); } //Delete every item in the children array for( int i = 0; i < ARRAY_SIZE; ++i ) { if( root->children[i] != NULL ) delete root->children[i]; } }
int main(int argc, char** argv) { FILE* fp; // declare dict file char* programName = argv[0]; char* dictName = argv[1]; char* input = (char*)malloc(sizeof(char) * MAXLINE); char* oldInput = (char*)malloc(sizeof(char) * MAXLINE); if ((input == NULL) || (oldInput == NULL)) fprintf(stderr, MLCFAIL); if ((fp = fopen(dictName, "r")) == NULL) { fprintf(stderr, "%s: %s: No such file or directory\n", programName, dictName); // exit if dict is not loaded properly return 1; } //build tree from dict struct node* root = newNode(); root = getDict(fp, dictName, root); fclose(fp); // T9 interaction: printf("Enter \"exit\" to quit.\n"); while (1) { printf("Enter Key Sequence (or \"#\" for next word) :\n"); scanf("%s", input); int len = strlen(input); if (!strncmp(input, "exit", len)) break; if (!strncmp(input, "#", 1)) strcat(oldInput, input); else strcpy(oldInput, input); char* word = searchTrie(root, oldInput, strlen(oldInput), 0); printf("\t%s\n", word); free(word); //free alloc from appendquotes } // clean up free(input); free(oldInput); deleteTrie(root); return 0; }
void deleteTrie(struct ChrTrie* chr_root) { //遍历字典树,递归释放结点空间 for (int i = 0; i < MAX; i++) { auto chr_node = chr_root->next[i]; if (chr_node == nullptr) { break; } if (*(chr_node->next) == nullptr) { free(chr_node); return; } else { deleteTrie(chr_node); } } }
Trie<T>::~Trie() { deleteTrie( root ); delete root; }
Dictionary::~Dictionary() { deleteTrie(root); }
//该结点退出时,需要释放字典树 void ChrsGrid::onExit() { Node::onExit(); deleteTrie(&chr_root); }