int main() { int exists_string = 0; Trie root = NULL; root = trie_init(); trie_insert(root, "daijinwei"); trie_insert(root, "hello"); trie_insert(root, "wangsan"); trie_insert(root, "ccnp"); trie_insert(root, "linux"); exists_string = trie_search(root,"daijinwei"); if(0 != exists_string){ fprintf(stdout, "The string is exists the trie\n"); }else{ fprintf(stdout, "The string is not exists the trie\n"); } exists_string = trie_search(root,"daijinw"); if(0 != exists_string){ fprintf(stdout, "The string is exists the trie\n"); }else{ fprintf(stdout, "The string is not exists the trie\n"); } trie_destroy(root); }
int main() { trie_node *root = NULL; trie_pair *entry; root = trie_insert(root, pair_new("dog\0", 2)); root = trie_insert(root, pair_new("cat\0", 3)); root = trie_insert(root, pair_new("top\0", 4)); root = trie_insert(root, pair_new("rat\0", 5)); trie_show(root, 0); entry = trie_search(root, "cat"); trie_delete(root, "dog"); entry = trie_search(root, "dog"); }
int main(void) { trie *t; t = new_trie(); printf("Added %s %d\n","texta",trie_insert(t,"texta")); printf("Added %s %d\n","textb",trie_insert(t,"textb")); printf("Added %s %d\n","texta",trie_insert(t,"texta")); printf("Searched %s %d\n","texta",trie_search(t,"texta")); printf("Searched %s %d\n","textc",trie_search(t,"textc")); free_trie(t); return 0; }
int main() { char str_hash[MAX_LEN]; int n; scanf ("%d", &n); getchar(); int i; for (i = 0; i < n; i++) gets (words[i]); // for (i = 0; i < n; i++) // quick_sort (words[i], 0, strlen (words[i]) - 1); struct trie_node* root = create_trie_node (); for (i = 0; i < n; i++) { strcpy (str_hash, words[i]); quick_sort (str_hash, 0, strlen (str_hash) - 1); trie_insert (root, str_hash, i); } int q; scanf ("%d", &q); getchar(); for (i = 0; i < q; i++) { char query[MAX_LEN]; gets (query); strcpy (str_hash, query); quick_sort (str_hash, 0, strlen (str_hash) - 1); struct trie_node* node = trie_search (root, str_hash); if (node == NULL) { answer[i] = 0; } else { // printf ("p->size: %d\n", node->size); answer[i] = node->size; int j; for (j = 0; j < node->size; j++) { // printf ("ori: %s\n", words[node->index[j]]); // printf ("current: %s\n", query); if (strcmp (words[node->index[j]], query) == 0) answer[i] -= 1; } } } for (i = 0; i < q; i++) printf ("%d\n", answer[i]); return 0; }
/* search word, returns word counter */ int trie_search(trie *t, char *s) { if(*s) { if(t->next[*s-'a'] == NULL) return 0; return trie_search(t->next[*s-'a'], s+1); } return t->val; }
void *trie_search(TrieNode *parent, const char *key) { TrieNode *child; if(*key) { child = find_child(parent, *key); if(child) { return trie_search(child, ++key); } else { return NULL; } } else { return parent->value; } }
int main(int argc, char **argv) { TrieNode *root = new_trie(); trie_insert(root, "/foo/bar", "baz"); trie_insert(root, "/foo/blubb", "bla"); trie_insert(root, "/asdf/dassf/fdas", "blubb"); trie_insert(root, "/asdd/f/dsa/s", "fasd"); //dump_tree(root); iterate_trie(root, print_line, NULL); char *result = (char*)trie_search(root, "/asdf/dassf/fdas"); fprintf(stderr, "RESULT: %s\n", result); return 0; }
const char *intern_soft(struct intern *pool, const char *string) { return trie_search(pool->trie, string); }