void music_fini (void) { if (music_init_done) { music_init_done = false; if (all_music) { tree_destroy(&all_music, (tree_destroy_func)music_destroy); } } Mix_CloseAudio(); }
int main () { int val = 1; struct Node *tree = calloc (1, sizeof (struct Node)); scanf ("%d", &val); while (val != 0) { tree = tree_add (tree, val); scanf ("%d", &val); } printf ("%d\n", tree_hight(tree) - 1); tree_destroy(tree); return 0; }
/* Free's memory from tree and companies stored in the tree. * */ void tree_destroy( struct tree* root) { if (! root ) { return; } if( root->left ) { tree_destroy (root->left); root->left = NULL; } if ( root->right ) { tree_destroy(root->right); root->right = NULL; } if( root->data ) { if (root->data->name) { free(root->data->name); } free(root->data); } free(root); }
static bool recursively_flatten_libraries (CompileProject *project, Compile *compile) { TreeIterator *iterator; Tree *append; if (!(append = tree_create ())) { compile_debug_allocate_memory (); return false; } if (!(iterator = tree_iterator_create (compile->libraries))) { compile_debug_allocate_memory (); return false; } while (tree_iterator_next (iterator)) { if (!recursively_flatten_libraries_inner (project, compile, (Directory *)iterator->key, append)) { tree_iterator_destroy (iterator); tree_destroy (append); return false; } } tree_iterator_destroy (iterator); if (!(iterator = tree_iterator_create (append))) { compile_debug_allocate_memory (); return false; } while (tree_iterator_next (iterator)) { if (!tree_insert (compile->libraries, iterator->key, iterator->key)) { tree_iterator_destroy (iterator); tree_destroy (append); compile_debug_operation_failed (); return false; } } tree_iterator_destroy (iterator); tree_destroy (append); return true; }
int hashmap_destroy(var_p_t var_p) { if (var_p->type == V_MAP && var_p->v.m.map != NULL) { int i; for (i = 0; i < var_p->v.m.size; i++) { Node **table = (Node **)var_p->v.m.map; if (table[i] != NULL) { tree_destroy(table[i]); } } free(var_p->v.m.map); } return 0; }
void detruire_matrice(Matrice m){ if(m == NULL) return; else{ int i; for(i = 0; i<m->taille; i++){ free(m->tab[i]); } free(m->tab); tree_destroy(m->mot); free(m); } }
void blobtree_destroy(Blobtree **pblob){ if( *pblob == NULL ) return; Blobtree *blob = *pblob; if( blob->tree != NULL ){ tree_destroy(&blob->tree); } if( blob->tree_data != NULL) { free(blob->tree_data); blob->tree_data = NULL; } free(blob); *pblob = NULL; }
int main(void) { struct Node * tree=NULL; int i; while(1) { scanf("%d", &i); if(i == 0) break; else tree = tree_add(tree, i); } tree_print_l(tree); tree_destroy(tree); return 0; }
int main() { struct TreeNode* tree = NULL; int x; while ((scanf ("%d", &x) == 1) && (x != 0)) { tree = tree_add (tree, x); } tree_breadth_first_search (tree, &tree_print_node); putchar ('\n'); tree_destroy (tree); }
void music_fini (void) { FINI_LOG("%s", __FUNCTION__); if (music_init_done) { music_init_done = false; if (all_music) { tree_destroy(&all_music, (tree_destroy_func)music_destroy); } } Mix_CloseAudio(); }
int main(void) { struct Node * tree=NULL; int i; while(1) { scanf("%d", &i); if(i == 0) break; else tree = tree_add(tree, i); } tree_print(tree); // напечатает 1 2 3 4 5 6 7 8 9 tree_destroy(tree); return 0; }
void userlist_free (session *sess) { tree_foreach (sess->usertree, (tree_traverse_func *)free_user, NULL); tree_destroy (sess->usertree); sess->usertree = NULL; sess->me = NULL; sess->ops = 0; sess->hops = 0; sess->voices = 0; sess->total = 0; }
int test_regression_evaluate(void) { int i; int res; float f = 100.0; float **func_input; float solution_score = 5.0; struct tree *t; struct node **chromosome = malloc(sizeof(struct node *) * 5); struct data *d = csv_load_data(TEST_DATA, 1, ","); /* initialize func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* build chromosome */ chromosome[0] = node_new_constant(FLOAT, &f); chromosome[1] = node_new_input((char *) "x"); chromosome[2] = node_new_func(MUL, 2); chromosome[3] = node_new_func(RAD, 1); chromosome[4] = node_new_func(SIN, 1); /* build tree */ t = malloc(sizeof(struct tree)); t->root = NULL; t->size = 5; t->depth = -1; t->score = NULL; t->chromosome = chromosome; /* evaluate tree */ res = regression_evaluate(t, func_input, d, (char *) "y"); mu_check(res == 0); mu_check(fltcmp(t->score, &solution_score) == 0); mu_check(t->hits == 361); /* clean up */ free_chromosome(chromosome, 5); tree_destroy(t); data_destroy(d); free_mem_arr(func_input, 2, free); return 0; }
int main() { struct Node * tree = NULL; int nn; scanf("%d", &nn); while (nn > 0) { tree = tree_add(tree, nn); scanf("%d", &nn); }; printf("%s\n", tree_balanced(tree)?"YES":"NO"); tree_destroy(tree); return 0; }
/* finalise and deconstruct the eventlog */ void elog_fini() { int i, j; elog_checkinit(); /* close any open purls, that is severities for which elog_ opened * a route. Routes present in elog_opendest[] without purl values * were suplied externally and it is their responsibility to clear up! */ for (i=0; i < ELOG_NSEVERITIES; i++) if (elog_opendest[i].purl) { /* is there another severity with the same purl? * If so, clear that severity */ for (j=i+1; j < ELOG_NSEVERITIES; j++) { if ( elog_opendest[j].purl && strcmp(elog_opendest[j].purl, elog_opendest[i].purl) == 0 ) { nfree(elog_opendest[j].purl); elog_opendest[j].purl = NULL; elog_opendest[j].route = NULL; } } /* close old route opened and owned by us */ route_close(elog_opendest[i].route); nfree(elog_opendest[i].purl); elog_opendest[i].purl = NULL; elog_opendest[i].route = NULL; } /* free any format strings */ for (i=0; i < ELOG_NSEVERITIES; i++) if (elog_opendest[i].format) { nfree(elog_opendest[i].format); elog_opendest[i].format = NULL; } tree_clearoutandfree(elog_override); tree_destroy(elog_override); if (elog_origin) nfree(elog_origin); route_close(elog_errors); }
void tree_destroy(KeyBindingTree *tree) { KeyBindingTree *c; while (tree) { tree_destroy(tree->next_sibling); c = tree->first_child; if (c == NULL) { GList *it; GSList *sit; for (it = tree->keylist; it != NULL; it = it->next) g_free(it->data); g_list_free(tree->keylist); for (sit = tree->actions; sit != NULL; sit = sit->next) actions_act_unref(sit->data); g_slist_free(tree->actions); } g_free(tree); tree = c; } }
/* * Carries out all the pending actions and creates a table containing * the data. Table should be freed after use. The parent TABSET and TABLE * classes must still be in existance during the use of the table as certain * values depend on the existance of both classes. * Always creates a TABLE, but there may be no rows. */ TABLE tableset_into (TABSET tset) { TABLE target; TREE *row, *infonames, *inforow; if (tset->where) tableset_priv_execute_where(tset); /* set up new table, depending on whether there are custom columns */ if (tset->cols) { target = table_create_t(tset->cols); /* add info lines */ infonames = table_getinfonames(tset->tab); tree_traverse(infonames) { inforow = table_getinforow(tset->tab, tree_getkey(infonames)); table_addinfo_t(tset->tab, tree_getkey(infonames), inforow); tree_destroy(inforow); } } else {
/* * Linux specific routines */ void plinnames_collect(TABLE tab) { char *basename; plinnames_sysfiles = tree_create(); plinnames_readalldir("/proc/sys", plinnames_sysfiles); tree_traverse(plinnames_sysfiles) { table_addemptyrow(tab); table_replacecurrentcell(tab, "value", tree_get(plinnames_sysfiles)); table_replacecurrentcell(tab, "name", tree_getkey(plinnames_sysfiles)); basename = strrchr(tree_getkey(plinnames_sysfiles), '/'); if (basename) basename++; else basename = tree_getkey(plinnames_sysfiles); table_replacecurrentcell(tab, "vname", basename); table_freeondestroy(tab, tree_getkey(plinnames_sysfiles)); table_freeondestroy(tab, tree_get(plinnames_sysfiles)); } tree_destroy(plinnames_sysfiles); }
tree_t* tree_load(const char* folder, ProgressFunc func, void* data, unsigned depth) { tree_t* tree; TheProgressFunc = func; TheProgressData = data; RootDevice = 0; Aborted = FALSE; g_assert(folder); tree = tree_scan_rec(folder, folder, depth); TheProgressFunc = NULL; if (Aborted) { tree_destroy(tree); return NULL; } else { return tree; } }
/* use all but nocols, text version. tab and space used as delimiter */ void tableset_excludet(TABSET tset /* tableset instance */, char *nocols /* whitespace separated string */) { char *mycols, *thiscol; TREE *listcols; mycols = xnstrdup(nocols); tableset_freeondestroy(tset, mycols); thiscol = strtok(mycols, " \t"); if (thiscol) listcols = tree_create(); else return; /* no cols to index */ while (thiscol) { tree_add(listcols, thiscol, NULL); thiscol = strtok(NULL, " \t"); } tableset_exclude(tset, listcols); tree_destroy(listcols); return; }
int main(int argc, char ** argv) { if(argc != 3) { printf("ERROR! There should be three input arguments, the object file, the input file and the output file"); return EXIT_FAILURE; } FILE * ftpr; ftpr = fopen(argv[1],"r"); if(ftpr == NULL) { printf("\n Error! The input file could not be opened"); return EXIT_FAILURE; } fclose(ftpr); HuffNode * Huffmannroot = NULL; int filetype = readHeader(argv[1]); if(filetype == 0) { Huffmannroot = create_Huffmanntree(argv[1]); } else { Huffmannroot = create_HufftreeBit(argv[1]); } FILE * ftpr2 = fopen(argv[2],"w"); if(ftpr2 == NULL) { printf("Error! The output filename argument could not be opened."); return EXIT_FAILURE; } Huff_postOrderPrint(Huffmannroot, ftpr2); tree_destroy(Huffmannroot); fclose(ftpr2); return EXIT_SUCCESS; }
int main() { struct Node * tree = NULL; for (int i = 0; i <= 10001; ++i) { a[i] = 0; } int nn; scanf("%d", &nn); a[nn]++; while (nn > 0) { tree = tree_add(tree, nn); scanf("%d", &nn); a[nn]++; }; tree_print(tree); tree_destroy(tree); return 0; }
void testBSTree() { printf("\nNow is Create Binary tree with node size %d >>>>>>>>>>\n", ARRAY_SIZE); randomize_in_place(A, ARRAY_SIZE); // randomize_maxnum(A, ARRAY_SIZE, ARRAY_SIZE); print_array(A, ARRAY_SIZE); startProfileTime(); tree_t * tree = tree_create(A, ARRAY_SIZE); endProfileTime("Create Binary search tree "); #if 0 printf("\nPre order traverse:\n"); tree_preorder_traverse(tree->root, my_treenode_key_traverse); printf("\nPost order traverse:\n"); tree_postorder_traverse(tree->root, my_treenode_key_traverse); printf("\nIn order traverse:\n"); tree_inorder_traverse(tree->root, my_treenode_key_traverse); #endif int key = 50; startProfileTime(); treenode_t * search_result = tree_search(tree->root, key); endProfileTime("Binary tree search"); if (search_result != get_nil_node()) { printf("Found key:%d\n", key); } else { printf(" Not found key:%d\n", key); } tree_left_rotate(tree, search_result); tree_right_rotate(tree, search_result); traverse_no_recurise(tree->root, my_treenode_key_traverse); treenode_t * max, * min; max = tree_max(tree->root); min = tree_min(tree->root); printf("\nmax = %ld\n min = %ld\n", max->key, min->key); treenode_t * bigger = tree_successor(search_result); printf("successor = %ld\n", (bigger!=NULL) ? bigger->key : -1); treenode_t * smaller = tree_predecessor(search_result); printf("perdecessor = %ld\n", (smaller!=NULL) ? smaller->key : -1); //Test delete: treenode_t * deleted_node = RBTree_delete(tree, search_result); // treenode_t * deleted_node = tree_delete(tree, search_result); if (deleted_node) printf("del %p, key=%ld from tree.\n", deleted_node, deleted_node->key); tree_inorder_traverse(tree->root, my_treenode_key_traverse); // traverse_no_recurise(tree->root, my_treenode_key_traverse); int height = get_tree_height(tree->root); printf("\nget tree h = %d\n", height); tree_destroy(tree, NULL); }
int tree_read (state_t *ur, struct tree *tree, unsigned char sha1[20]) { int fd = -1, i; int blob_cnt = 0; int branch_cnt = 0; char * buf = NULL; char *name = NULL, *branch = NULL; unsigned char commit[20]; struct blob_tree_entry *blob_entry = NULL; struct branch_tree_entry *branch_entry = NULL; tree_init (tree); if ((fd = object_open (ur, sha1)) < -1) goto error; buf = readline (fd); if (buf == NULL) goto error; blob_cnt = atoi (buf); free (buf); buf = NULL; for (i = 0; i < blob_cnt; i ++) { buf = readline (fd); if (buf == NULL) goto error; name = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit); free (buf); buf = NULL; blob_entry = (struct blob_tree_entry *) malloc (sizeof (struct blob_tree_entry)); if (blob_entry == NULL) goto error; blob_entry->name = name; memcpy (blob_entry->commit, commit, 20); list_push_back (&tree->blob_entries, &blob_entry->elem); name = NULL; blob_entry = NULL; } buf = readline (fd); if (buf == NULL) goto error; branch_cnt = atoi (buf); free (buf); buf = NULL; for (i = 0; i < branch_cnt; i ++) { buf = readline (fd); if (buf == NULL) goto error; name = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; branch = buf; buf = NULL; buf = readline (fd); if (buf == NULL) goto error; hex_to_sha1 (buf, commit); free (buf); buf = NULL; branch_entry = (struct branch_tree_entry *) malloc (sizeof (struct branch_tree_entry)); if (branch_entry == NULL) goto error; branch_entry->name = name; branch_entry->branch = branch; memcpy (branch_entry->commit, commit, 20); list_push_back (&tree->branch_entries, &branch_entry->elem); name = NULL; branch = NULL; branch_entry = NULL; } close (fd); return 0; error: if (name != NULL) free (name); if (branch != NULL) free (branch); if (blob_entry != NULL) free (blob_entry); if (branch_entry != NULL) free (branch_entry); tree_destroy (tree); return -1; }
/* * Carry out aggregation on a complete data set held in a table * This is an alternative entry point to the class that does not need * the setting up of a session. * The table should identify keys, time, sequence and duration in the * standard way as defined by FHA spec. * Returns a TABLE of results compliant to the FHA spec, _time will be * set to the last time of the dataset, _seq to 0. _dur is not set * The result is independent of dataset's memory allocation, sothe caller * needs to run table_destroy() to free its memory. * Returns NULL if there is an error, if dataset is NULL or if there * was insufficent data. */ TABLE cascade_aggregate(enum cascade_fn func, /* aggregation function */ TABLE dataset /* multi-sample, multi-key * dataset in a table */ ) { TREE *inforow, *keyvals, *databykey, *colnames; char *keycol, *colname, *tmpstr, *type; int duration, haskey=0; TABLE itab, result; TABSET tset; ITREE *col; double val, tmpval1, tmpval2; time_t t1, t2, tdiff; /* assert special cases */ if ( ! dataset ) { elog_printf(DIAG, "no dataset given to aggregate"); return NULL; } if (table_nrows(dataset) == 0) { elog_printf(DIAG, "no rows to aggregate in dataset"); } if ( ! table_hascol(dataset, "_time")) { tmpstr = table_outheader(dataset); elog_printf(ERROR, "attempting to aggregate a table without _time " "column (columns: %s)", tmpstr); nfree(tmpstr); return NULL; } /* find any keys that might exist */ inforow = table_getinforow(dataset, "key"); if (inforow) { keycol = tree_search(inforow, "1", 2); if (keycol) { keyvals = table_uniqcolvals(dataset, keycol, NULL); if (keyvals) { /* separate the combined data set into ones of * separate keys */ haskey++; databykey = tree_create(); tset = tableset_create(dataset); tree_traverse(keyvals) { /* select out the data */ tableset_reset(tset); tableset_where(tset, keycol, eq, tree_getkey(keyvals)); itab = tableset_into(tset); tree_add(databykey, tree_getkey(keyvals), itab); } tableset_destroy(tset); } tree_destroy(keyvals); } tree_destroy(inforow); } /* if there were no keys found, pretend that we have a single one */ if ( ! haskey ) { databykey = tree_create(); tree_add(databykey, "nokey", dataset); } /* find the time span and duration of the dataset */ table_first(dataset); if (table_hascol(dataset, "_dur")) duration = strtol(table_getcurrentcell(dataset, "_dur"), NULL, 10); else duration = 0; t1 = strtol(table_getcurrentcell(dataset, "_time"), NULL, 10); table_last(dataset); t2 = strtol(table_getcurrentcell(dataset, "_time"), NULL, 10); tdiff = t2-t1+duration; /* go over the keyed table and apply our operators to each column * in turn */ result = table_create_fromdonor(dataset); table_addcol(result, "_seq", NULL); /* make col before make row */ table_addcol(result, "_time", NULL); table_addcol(result, "_dur", NULL); tree_traverse(databykey) { table_addemptyrow(result); itab = tree_get(databykey); colnames = table_getheader(itab); tree_traverse(colnames) { colname = tree_getkey(colnames); if ( ! table_hascol(result, colname)) { tmpstr = xnstrdup(colname); table_addcol(result, tmpstr, NULL); table_freeondestroy(result, tmpstr); } col = table_getcol(itab, colname); type = table_getinfocell(itab, "type", colname); if (type && strcmp(type, "str") == 0) { /* string value: report the last one */ itree_last(col); table_replacecurrentcell(result, colname, itree_get(col)); } else if (strcmp(colname, "_dur") == 0) { /* _dur: use the last value, can treat as string */ itree_last(col); table_replacecurrentcell(result, "_dur", itree_get(col)); } else if (strcmp(colname, "_seq") == 0) { /* _seq: only one result is produced so must be 0 */ table_replacecurrentcell(result, "_seq", "0"); } else if (strcmp(colname, "_time") == 0) { /* _time: use the last value, can treat as string */ itree_last(col); table_replacecurrentcell(result, "_time", itree_get(col)); } else { /* numeric value: treat as a float and report it */ switch (func) { case CASCADE_AVG: /* average of column's values */ val = 0.0; itree_traverse(col) val += atof( itree_get(col) ); val = val / itree_n(col); break; case CASCADE_MIN: /* minimum of column's values */ val = DBL_MAX; itree_traverse(col) { tmpval1 = atof( itree_get(col) ); if (tmpval1 < val) val = tmpval1; } break; case CASCADE_MAX: /* maximum of column's values */ val = DBL_MIN; itree_traverse(col) { tmpval1 = atof( itree_get(col) ); if (tmpval1 > val) val = tmpval1; } break; case CASCADE_SUM: /* sum of column's values */ val = 0.0; itree_traverse(col) val += atof( itree_get(col) ); break; case CASCADE_LAST: /* last value */ itree_last(col); val = atof( itree_get(col) ); break; case CASCADE_FIRST: /* last value */ itree_first(col); val = atof( itree_get(col) ); break; case CASCADE_DIFF: /* the difference in values of first and last */ itree_first(col); tmpval1 = atof( itree_get(col) ); itree_last(col); tmpval2 = atof( itree_get(col) ); val = tmpval2 - tmpval1; break; case CASCADE_RATE: /* difference in values (as CASCADE_DIFF) then * divided by the number of seconds in the set */ itree_first(col); tmpval1 = atof( itree_get(col) ); itree_last(col); tmpval2 = atof( itree_get(col) ); val = tmpval2 - tmpval1; val = val / tdiff; break; } /* save the floating point value */ table_replacecurrentcell_alloc(result, colname, util_ftoa(val)); } itree_destroy(col); } /* make sure that there are values for the special columns */ if ( ! table_hascol(dataset, "_time")) table_replacecurrentcell(result, "_time", util_decdatetime(time(NULL))); if ( ! table_hascol(dataset, "_seq")) table_replacecurrentcell(result, "_seq", "0"); if ( ! table_hascol(dataset, "_dur")) table_replacecurrentcell(result, "_dur", "0"); } /* clear up */ if (haskey) { tree_traverse(databykey) { itab = tree_get(databykey); table_destroy(itab); } } tree_destroy(databykey); return result; }
static unsigned int test_tree_node_removal(unsigned int keys) { struct tree *t = tree_create (); unsigned int i; struct tree_node *n; for (i = 0; i < keys; i++) { tree_add_node_value (t, i, sentinelvalue(i)); } /* remove half the nodes */ for (i = 0; i < keys; i+=2) { tree_remove_node (t, i); } /* search for the nodes that should still be present */ for (i = 1; i < keys; i+=2) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 15; } if (n->key != i) { tree_destroy(t); return 16; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 17; } } /* search for the nodes that should be missing */ for (i = 0; i < keys; i+=2) { n = tree_get_node (t, i); if (n != (struct tree_node *)0) { tree_destroy(t); return 18; } } /* search for an arbitrary node that can't be present */ n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 19; } /* we do the searches twice to stress the optimising algo once it's in */ /* search for the nodes that should still be present */ for (i = 1; i < keys; i+=2) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 20; } if (n->key != i) { tree_destroy(t); return 21; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 22; } } /* search for the nodes that should be missing */ for (i = 0; i < keys; i+=2) { n = tree_get_node (t, i); if (n != (struct tree_node *)0) { tree_destroy(t); return 23; } } /* remove the remaining nodes */ for (i = 1; i < keys; i+=2) { tree_remove_node (t, i); } if (t->root != (struct tree_node *)0) { tree_destroy(t); return 24; } tree_destroy(t); return 0; }
static cb_ret_t tree_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data) { WTree *tree = (WTree *) w; WDialog *h = w->owner; WButtonBar *b = find_buttonbar (h); switch (msg) { case MSG_DRAW: tree_frame (h, tree); show_tree (tree); return MSG_HANDLED; case MSG_FOCUS: tree->active = 1; buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), tree_map, w); buttonbar_set_label (b, 2, Q_ ("ButtonBar|Rescan"), tree_map, w); buttonbar_set_label (b, 3, Q_ ("ButtonBar|Forget"), tree_map, w); buttonbar_set_label (b, 4, tree_navigation_flag ? Q_ ("ButtonBar|Static") : Q_ ("ButtonBar|Dynamc"), tree_map, w); buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), tree_map, w); buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), tree_map, w); #if 0 /* FIXME: mkdir is currently defunct */ buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), tree_map, w); #else buttonbar_clear_label (b, 7, WIDGET (tree)); #endif buttonbar_set_label (b, 8, Q_ ("ButtonBar|Rmdir"), tree_map, w); widget_redraw (WIDGET (b)); /* FIXME: Should find a better way of only displaying the currently selected item */ show_tree (tree); return MSG_HANDLED; /* FIXME: Should find a better way of changing the color of the selected item */ case MSG_UNFOCUS: tree->active = 0; tree->searching = 0; show_tree (tree); return MSG_HANDLED; case MSG_KEY: return tree_key (tree, parm); case MSG_ACTION: /* command from buttonbar */ return tree_execute_cmd (tree, parm); case MSG_DESTROY: tree_destroy (tree); return MSG_HANDLED; default: return widget_default_callback (w, sender, msg, parm, data); } }
static unsigned int test_tree_value(unsigned int keys) { struct tree *t = tree_create (); unsigned int i; struct tree_node *n; for (i = 0; i < keys; i++) { tree_add_node_value (t, i, sentinelvalue(i)); } for (i = 0; i < keys; i++) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 7; } if (n->key != i) { tree_destroy(t); return 8; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 9; } } n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 10; } /* we do this twice to stress the optimising algo once it's in */ for (i = 0; i < keys; i++) { n = tree_get_node (t, i); if (n == (struct tree_node *)0) { tree_destroy(t); return 11; } if (n->key != i) { tree_destroy(t); return 12; } if (node_get_value (n) != sentinelvalue(i)) { tree_destroy(t); return 13; } } n = tree_get_node (t, keys + 1); if (n != (struct tree_node *)0) { tree_destroy(t); return 14; } tree_destroy(t); return 0; }
/** * Removes all entries from the table. * * @param[in] table the table from which all entries are to be removed */ void treetable_remove_all(TreeTable *table) { tree_destroy(table, table->root); table->size = 0; table->root = table->sentinel; }
int main() { tnode_t *n; searchTree_t root = NULL;; int loops = 80000000; int i,rd; int max = 99999999; srand(time(NULL)); for(i = 1;i<= loops;i++) { rd = rand() % max + 1; // printf("rd:%d\n",rd); n = malloc(sizeof(tnode_t)); n->data = rd; root = tree_insert(root,n); if(root == NULL) printf("insert error\n"); // inorder_walk_tree(root,print_tnode); printf("\n"); } inorder_walk_tree(root,print_tnode); printf("\n"); struct timeval start,end; gettimeofday(&start,NULL); tnode_t *r = tree_search_v1(root , 9999998); gettimeofday(&end,NULL); if(r == NULL) printf("not found\n"); else printf("found %d at 0x%x\n",r->data,(unsigned int)r); printf("it takes %ld seconds,%ld micorseconds in v1\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec)); r = NULL; gettimeofday(&start,NULL); r = tree_search_v2(root , 9999998); gettimeofday(&end,NULL); if(r == NULL) printf("not found\n"); else printf("found %d at 0x%x\n",r->data,(unsigned int)r); printf("it takes %ld seconds,%ld micorseconds in v2\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec)); tnode_t * min = tree_min(root); if(min != NULL) printf("min is %d\n",min->data); tnode_t * mx = tree_max(root); if(mx != NULL) printf("max is %d\n",mx->data); tree_destroy(root); }