void bst_free(bstnode * n) { if (n != NULL) { bst_free(n->left); bst_free(n->right); bst_dispose(n); } }
void bst_free(NODE *r) { if (r == NULL) { return; } bst_free(r->left); bst_free(r->right); free(r); }
int main(){ int i; /* PART 1 */ int a[] = {8, 2, 6, 9, 11, 3, 7}; BST_PTR t = bst_create(); t_avl(); // t_height(); /* PART 2 */ // bst_inorder(t); // // bst_preorder(t); // // bst_postorder(t); bst_free(t); int sorted_a[] = {2, 3, 6, 7, 8, 9, 11}; t = bst_from_sorted_arr(sorted_a, 7); int *b = bst_to_array(t); printf("Height of root: %d\n", bst_height(t)); printf("Size of tree: %d\n", bst_size(t)); printf("ith smallest element: %d\n", bst_get_ith(t,10)); printf("Num LEQ: %d\n", bst_num_leq(t,3)); printf("A: "); for (i=0; i<7; i++) { printf("%d ", sorted_a[i]); } printf("\n"); printf("B: "); for (i=0; i<7; i++) { printf("%d ", b[i]); } printf("\n"); free(b); bst_free(t); }
/* Frees the BST */ void bst_free(bst *t) { if(t == NULL) //in event null tree passed return; if(t -> lsub != NULL) bst_free(t -> lsub); if(t -> rsub != NULL) bst_free(t -> rsub); vcard_free(t -> c); free(t); }
void bst_free(bst *t) { if (t == NULL) { return; } bst_free(t->lsub); bst_free(t->rsub); // once the leaves are reached, start freeing vcard_free(t->c); free(t); return; }
void t_avl() { int i; BST_PTR t = bst_create(); int *a = gen_random_arr(SAMPLE_SIZE); for(i=0; i<SAMPLE_SIZE; i++) bst_insert(t, i); printf("Height of root: %d\n", bst_height(t)); printf("Size of tree: %d\n", bst_size(t)); printf("Min elem: %d\n", bst_min(t)); printf("Max elem: %d\n", bst_max(t)); printf("Nearest elem: %d\n", bst_get_nearest(t,500)); printf("Num LEQ: %d\n", bst_num_leq(t,10)); for(i=0; i<SAMPLE_SIZE-1; i++) { bst_remove(t,a[i]); printf ("Delete %d\n", a[i]); } assert(bst_to_array(t)[0]==a[SAMPLE_SIZE-1]); printf("Height of root: %d\n", bst_height(t)); printf("Size of tree: %d\n", bst_size(t)); printf("Min elem: %d\n", bst_min(t)); printf("Max elem: %d\n", bst_max(t)); printf("Nearest elem: %d\n", bst_get_nearest(t,500)); printf("Num LEQ: %d\n", bst_num_leq(t,10)); free(a); bst_free(t); }
int main(void) { bst b = bst_new(); printf("inserting d,b,f,a,c,e,g\n"); b = bst_insert(b, "d"); b = bst_insert(b, "b"); b = bst_insert(b, "f"); b = bst_insert(b, "a"); b = bst_insert(b, "c"); b = bst_insert(b, "e"); b = bst_insert(b, "g"); printf("inorder traversal\n"); bst_inorder(b, print_key); printf("preorder traversal\n"); bst_preorder(b, print_key); printf("searching\n"); dosearch(b, "f"); dosearch(b, "o"); dosearch(b, "x"); dosearch(b, "e"); dosearch(b, "d"); bst_free(b); return EXIT_SUCCESS; }
int main(){ int i; int a[] = {8, 2, 7, 9, 11, 3, 2, 6}; BST_PTR t = bst_create(); for(i=0; i<8; i++) bst_insert(t, a[i]); assert(bst_size(t) == 7); test_insert(t); test_contains(t); bst_inorder(t); bst_preorder(t); bst_postorder(t); bst_ith_smallest(t, 1) bst_size(t); bst_free(t); }
void bst_free(bst_t *bst, bst_node_t *node) { if(bst->root == bst->sentinel) { free(bst); return ; } if (node == bst->sentinel) { return ; } bst_free(bst, node->left); bst_free(bst, node->right); printf_debug("free key:%d\n",node->key); free(node); return ; }
int t_bst_insert() { int i; int a[] = {8, 2, 6, 9, 11, 3, 7}; BST_PTR t = bst_create(); for(i=0; i<7; i++) bst_insert(t, a[i]); assert(bst_size(t) == 7); bst_free(t); }
void gpfs_free_list(void) { int i = gpfs_attr_list.count; int rc; struct rsync_gpfs_attr *a = gpfs_attr_list.items; while(i) { gpfs_free_attr(a++); i--; } if (gpfs_bst) { rc = bst_free(gpfs_bst); if (rc) { rprintf(FERROR, "bst_free rc=%d\n", rc); exit_cleanup(RERR_CRASHED); } } }
void t_height() { int i; int a[] = {8, 2, 6, 9, 11, 3, 7}; BST_PTR t = bst_create(); BST_PTR t2; for(i=0; i<7; i++) bst_insert(t, a[i]); bst_remove(t,11); bst_remove(t,2); bst_preorder(t); printf("Min elem: %d\n", bst_min(t)); printf("Max elem: %d\n", bst_max(t)); printf("Nearest elem: %d\n", bst_get_nearest(t,500)); printf("Num LEQ: %d\n", bst_num_leq(t,10)); bst_free(t); }
/* curr_file pointer must point to a heap-allocated string */ int process_cmd(char **curr_file, char *cmd, bst **address_book, int *quit) { int parse_succeeded; char *cnet; char *infile; char *first_char; switch (cmd[0]) { case 'q': if (strcmp(cmd,"q")==0) { *quit = 1; return 1; } return 0; case 's': if (strcmp(cmd,"s")==0) { if (strlen(*curr_file)>0) fprintf(stdout,"Current file: %s.\n",*curr_file); addr_book_stats(*address_book); return 1; } return 0; case 'r': infile = extract_arg(cmd, &parse_succeeded); if (parse_succeeded) { bst *tmp = *address_book; *address_book = admin_addr_book_from_file(infile); if (*address_book==NULL) { /* restore address book */ *address_book = tmp; } else { free(*curr_file); *curr_file = infile; bst_free(tmp); } } return parse_succeeded; case 'c': first_char = extract_arg(cmd, &parse_succeeded); if (parse_succeeded) { if (strlen(first_char)!=1) fprintf(stdout,"Please type exactly one character as argument to c.\n"); else { unsigned int z = bst_c(*address_book, first_char[0]); printf("(found %u CNETs starting with '%c')\n",z,first_char[0]); } putchar('\n'); free(first_char); } return parse_succeeded; case 'l': cnet = extract_arg(cmd, &parse_succeeded); if (parse_succeeded) { int n_comparisons = 0; vcard *c = bst_search(*address_book, cnet, &n_comparisons); if (c==NULL) fprintf(stdout,"%s not found in current address book.\n", cnet); else vcard_show(c); fprintf(stdout,"(Performed %d comparisons in search.)\n", n_comparisons); putchar('\n'); } free(cnet); return parse_succeeded; case 'h': help_text(); return 1; default: return 0; } }
/** * deallocates all memory associated with the map. * */ void tmap_destroy(TMAP *t){ bst_free((*t)->root); free(*t); free(t); }