int tr_search(struct trie *t, const char *s) { struct trie tmp, *tmpp = &tmp, *memb = NULL; if (*s == '\0') { if (!t->tr_data) { printf("NOTFOUND: No entry for key\n"); return (-1); /* No entry for key */ } printf("\nFound '%s'\n", (char *)t->tr_data); return 0; } tmp.tr_key = *s; if (t->tr_nmemb) { memb = bsearch(&tmpp, t->tr_members, t->tr_nmemb, sizeof(struct trie *), tr_nodecmp); if (memb && *(struct trie **)memb) { return (tr_search(*(struct trie **)memb, ++s)); } printf("NOTFOUND: no member found for '%c'\n", *s); return (-1); } printf("NOTFOUND: No entry for key (no more members)\n"); return (-1); }
void tr_delete_task(Task *t) { if (t->aln) ma_free(t->aln); if (t->spec_tree) { tr_delete_tree_SDIptr(t->tree); if (t->subtree) tr_delete_tree_SDIptr(t->subtree); tr_delete_tree(t->spec_tree); } if (t->tree) tr_delete_tree_SDIptr(t->tree); if (t->compared) tr_delete_tree(t->compared); if (t->subtree && tr_search(t->tree, t->subtree) == 0) /* not a node in t->tree */ tr_delete_tree(t->subtree); if (t->tree) tr_delete_tree(t->tree); if (t->cut) free(t->cut); if (t->ortho) tr_delete_ortho(t->ortho); if (t->n_con) { int i; for (i = 0; i < t->n_con; ++i) tr_delete_tree(t->constraint[i]); free(t->constraint); } if (t->n_sub) { int i; for (i = 0; i < t->n_sub; ++i) free(t->sub_name[i]); free(t->sub_name); } free(t); }
int main(int argc, char *argv[]) { struct trie *t = NULL; char keybuf[1024], databuf[1024]; size_t i; tr_compar = charcmp; t = malloc(sizeof(struct trie)); if (!t) err(1, "malloc"); memset(t, 0, sizeof(struct trie)); stats_init(); while (fgets(keybuf, 1023, stdin)) { keybuf[strlen(keybuf)-1] = '\0'; /* FIXME \r\n */ snprintf(databuf, 1023, "%s is the data as well", keybuf); stats_depth_0(); int ret = tr_insert(t, keybuf, strdup(databuf)); if (ret != 0) err(1, "tr_insert"); } stats_dump(); for (i = 1; i < (size_t)argc; i++) { tr_search(t, argv[i]); } printf("<?xml version='1.0' standalone='no'?>\n" "<svg:svg width='100%%' height='100%%' version='1.1' xmlns:svg='http://www.w3.org/2000/svg'>\n"); t->tr_key='*'; tr_print(t, 0.0, canvas_width, 512.0, 32.0); printf("</svg:svg>\n"); tr_destroy(t); stats_fini(); return (0); }