/* * Create Binary Search with given number of nodes. * Accept data part from stdin for each node. * Returns pointer to the root of the newly created tree on success and NULL on error. * * @n : Number of nodes. */ node_t *bst_create(unsigned int n) { node_t *root = NULL; unsigned int i = 0; for (i = 0; i < n; i++) { node_t *new_node = (node_t *) malloc(sizeof (node_t)); if (!new_node) { LOG_ERR("Failed to allocate memory for new node.\n"); bst_destroy(&root); break; } scanf("%d", &new_node->data); new_node->left_child = new_node->right_child = NULL; if (bst_add_node(&root, new_node) != 0) { LOG_ERR("Failed to add new node %d.\n", new_node->data); free(new_node); bst_destroy(&root); break; } } return root; }
bst_t bst_destroy(bst_t bst) { switch (bst_type(bst)) { case isNull: break; case isEmpty: case isNotEmpty: if (bst->izq != NULL) { bst_destroy(bst->izq); /*Destruye subarbol izquierdo */ } if (bst->der != NULL) { bst_destroy(bst->der); /*Destruye subarbol derecho */ } bst->pair = pair_destroy(bst->pair); free(bst); bst = NULL; break; } return (bst); }
// // Recursively delete BST tree rooted at ptr // void bst_destroy (tnode *ptr) { if (ptr == NULL) return ; bst_destroy(ptr->left) ; bst_destroy(ptr->right) ; ptr->left = NULL ; //paranoid ptr->right = NULL ; free(ptr) ; }
/* * Free the memory allocated for each node in the tree. * * @cur_root : Double pointer to the root of the Binary Search Tree. Reset it to NULL */ void bst_destroy(node_t **cur_root) { assert(cur_root); node_t *root = *cur_root; if (root) { bst_destroy(&root->left_child); bst_destroy(&root->right_child); free(root); *cur_root = NULL; } }
void bst_destroy(bst_node_t *node) { if(node == NULL) return; if(node->left != NULL) bst_destroy(node->left); if(node->right != NULL) bst_destroy(node->right); if(node->data.type != STABLE_UNDEFINED) stable_destroy_data(&(node->data)); free(node->key); free(node); node = NULL; }
bst_t bst_copy(bst_t bst) { bst_t copy = bst_empty(); switch (bst_type(bst)) { case isNull: bst_destroy(copy); return (NULL); break; case isEmpty: break; case isNotEmpty: copy->pair = pair_copy(bst->pair); copy->izq = bst_copy(bst->izq); copy->der = bst_copy(bst->der); break; } assert(bst_is_equal(bst, copy)); /*POST*/ return (copy); }
int main(int argc, char *argv[]) { if (2 != argc) { errx(EXIT_FAILURE, "Usage: %s [CONFIGURATION FILE]", argv[0]); } pw_client_bst = bst_init(); if (0 != atexit(clean_up)) { bst_destroy(&pw_client_bst, (enum bst_type)PW_CLIENT_BST); errx(EXIT_FAILURE, "atexit() : failed to register clean_up()"); } configname = argv[1]; struct sigaction sa = { .sa_handler = signal_handle, /*.sa_flags = SA_RESTART,*/ }; sigemptyset_or_die(&sa.sa_mask); sigaction_or_die(SIGINT, &sa, NULL); sigaction_or_die(SIGHUP, &sa, NULL); setbuf(stdout, NULL); /*Make stdout unbuffered.*/ procclean((enum pw_clean_type)PW_SERVER_CLEAN); procserver(); return EXIT_SUCCESS; }
int main( void ){ // Initialization int i,j; bst_t* tree = NULL; SystemInit(); init_scroll(); GLCD_Clear( Blue ); tree = malloc(sizeof(bst_t)); bst_init(tree); // Insertion for (i=0; i<100; i++) bst_insert(tree, value_array[i]); printf("0 | %d, %d\n",bst_min(tree), bst_max(tree)); // Deletion for (i=0; i<5; i++){ for (j=0; j<20; j++) bst_erase(tree, erase_array[i][j]); printf("%d | %d, %d\n", i+1, bst_min(tree), bst_max(tree)); } // Destruction bst_destroy(tree); while ( 1 ) { /* An emebedded system does not terminate... */ } }
void bbst_destroy(bbst *bb) { bst *b = &bb->b; sarray *sa = &bb->sa; /* Both bst and sarray are allocated on the caller side. */ bst_destroy(b); sarray_destroy(sa); }
void debug_stack_bst_array_destroy(bst_node_t **arr) { for(unsigned int i = 0; i < BST_ARRAY_SIZE; i++) { bst_destroy(arr[i]); } free(arr); arr = NULL; }
bst_t bst_destroy(bst_t bst) { /* To destroy the current node, first we need to destroy its pair, * and its children if it have */ if (bst != NULL) { bst->pair = pair_destroy(bst->pair); if (bst->left != NULL) { bst->left = bst_destroy(bst->left); } if (bst->right != NULL) { bst->right = bst_destroy(bst->right); } /* Free the allocated resources */ free(bst); } bst = NULL; return (bst); }
dict_t dict_destroy(dict_t dict) { dict->data = bst_destroy(dict->data); assert(dict->data == NULL); free(dict); dict = NULL; return dict; }
int bbst_insert(bbst *bb, void *elem_addr) { bst *b = &bb->b; sarray *sa = &bb->sa; int sa_size; sarray_add(sa, elem_addr); bst_destroy(b); sa_size = sarray_size(sa); reinsert(b, sa, 0, sa_size-1); }
int main (int argc, char *argv[]) { bstree_t *tree; tree = make_tree(BALANCED, 1); bst_destroy(tree); free(tree); test_bst(BALANCED); test_bst(LEFT_HEAVY); test_bst(RIGHT_HEAVY); return unittest_has_error; }
bst_t bst_remove(bst_t bst, index_t index) { if (bst_type(bst) == isNotEmpty) { switch (index_compare(index, bst)) { case EQ: switch (has_child(bst)) { case none: bst = bst_destroy(bst); break; case both: case justRigth: pair_destroy(bst->pair); bst->pair = pair_copy((bst->der)->pair); /*copia el par q le sigue (der) */ bst->der = bst_remove(bst->der, pair_fst((bst->der)->pair)); break; case justLeft: pair_destroy(bst->pair); bst->pair = pair_copy((bst->izq)->pair); /*copia el par q le sigue (der) */ bst->izq = bst_remove(bst->izq, pair_fst((bst->izq)->pair)); break; } break; case LT: bst->izq = bst_remove(bst->izq, index); break; case GT: bst->der = bst_remove(bst->der, index); break; } } return (bst); }
void graph_destroy(Graph *g) { DList *edgeList; List edgeListAll; ListElem *elem; int opRes; if (g == 0) { return; } list_init(&edgeListAll, 0); opRes = bst_listElements(&g->treeVertexUndirectedEdge, &edgeListAll); if (opRes == 0) { elem = list_head(&edgeListAll); while (elem != 0) { edgeList = (DList *) list_data(elem); dlist_destroy(edgeList); free((void *) edgeList); elem = list_next(elem); } } list_destroy(&edgeListAll); bst_destroy(&g->treeVertexUndirectedEdge); bst_destroy(&g->treeUndirectedEdgeSource1); bst_destroy(&g->treeUndirectedEdgeSource2); bst_destroy(&g->treeVertexData); bst_destroy(&g->treeUndirectedEdgeData); g->cmp_vertex = 0; g->cmp_edge = 0; return; }
dict_t dict_destroy(dict_t dict) { /*Precondition verification*/ assert(dict != NULL); dict->length = 0; bst_destroy(dict->data); free(dict); dict = NULL; return (dict); }
/* destroy a table */ void ht_destroy(hashtbl_t * tbl) { int i; /* Loop through the array of trees, freeing data if necessary, then * deallocating the tree. */ for (i = 0; i < tbl->arrsz; i++) { if (tbl->arr[i]) { if (tbl->free) ht_free_tree_data(tbl->free, tbl->arr[i]->root); bst_destroy(tbl->arr[i]); } free(tbl->arr[i]); } free(tbl->arr); mempool_destroy(tbl->ht_elem_pool); mempool_destroy(tbl->key_pool); memset(tbl, 0, sizeof(hashtbl_t)); }
int main(int argc, char *argv[]) { char *ta[] = { "key", "Key", "string", "sTrIng", "STRING", "id", "num", "i", "I", "key", "asdf3", "tea23", "_2314", NULL }; int rc = 0; bst_node_t *root = NULL; stable_data_t data = { .type = LEX_EOF, .var.val.i = 1 }; // Test alloc root = bst_new_node("j", &data); // Test insert for(unsigned int i = 0; ta[i] != NULL; i++) root = bst_insert_node(root, ta[i], &data); // Print tree debug_bst_print_tree(root); printf("Size: %d\n", debug_bst_size(root)); // Test lookup for(unsigned int i = 0; ta[i] != NULL; i++) { bst_node_t *search = bst_lookup_node(root, ta[i]); if(search == NULL) { fprintf(stderr, "[FAIL] Couldn't find node with value %s\n", ta[i]); rc = 1; } else { if(strcmp(ta[i], search->key) == 0) { fprintf(stderr, "[PASS] Found node with correct value (%s == %s) (%d)\n", ta[i], search->key, search->data.var.val.i); } else { fprintf(stderr, "[FAIL] Found node with incorrect value (%s != %s)\n", ta[i], search->key); rc = 1; } } } // Test free (check with valgrind) bst_destroy(root); return rc; }
void test_bst(test_balanced_t kind) { bstree_t *tree; bst_node_t *node; if (kind == BALANCED) fprintf(stderr, "Testing balanced tree\n"); else if (kind == LEFT_HEAVY) fprintf(stderr, "Testing left-heavy tree\n"); else if (kind == RIGHT_HEAVY) fprintf(stderr, "Testing right-heavy tree\n"); tree = make_tree(kind, 0); ASSERT_TRUE(bst_find(tree, "AAA") != NULL, "bst_find: look up existing value (0)"); ASSERT_TRUE(bst_find(tree, "BBB") != NULL, "bst_find: look up existing value (1)"); ASSERT_TRUE(bst_find(tree, "CCC") != NULL, "bst_find: look up existing value (2)"); ASSERT_TRUE(bst_find(tree, "DDD") == NULL, "bst_find: look up non-existing value"); node = bst_insert(tree, strdup("DDD")); ASSERT_TRUE(bst_find(tree, "DDD") != NULL, "bst_insert: insert an element."); /* This should exercise each path when run for all of the balance kinds. */ bst_delete(tree, "AAA"); ASSERT_TRUE(bst_find(tree, "AAA") == NULL, "bst_delete: remove element A"); bst_delete(tree, "BBB"); ASSERT_TRUE(bst_find(tree, "BBB") == NULL, "bst_delete: remove element B"); bst_delete(tree, "CCC"); ASSERT_TRUE(bst_find(tree, "CCC") == NULL, "bst_delete: remove element C"); bst_delete(tree, "DDD"); ASSERT_TRUE(bst_find(tree, "DDD") == NULL, "bst_delete: remove element D"); bst_destroy(tree); ASSERT_TRUE(tree->root == NULL, "bst_destroy: null out structure."); free(tree); }
int main(){ unsigned int n_lines, max_val, i, ret, a = 0, b = 0, c = 0; bst_ret b_ret; char op; bst * h = NULL; scanf(" %u %u", &n_lines, &max_val); /* Create the binary tree */ b_ret = bst_create(&h, n_lines); if(b_ret == bst_NoMem){ printf("ERROR: No memory to create bst tree!\n"); printf("\t\tSize: %u\n", n_lines); exit(1); } /* Intiatializing time variables */ insTime.reset(); searchTime.reset(); remTime.reset(); /* Read the input file */ for(i = 0; i < n_lines; i++){ scanf(" %c %*d %[^\n]", &op, set); switch( op ){ /* Insertion */ case 'i': ret = insert(h, set); #ifdef _LOG if( ret == OP_OK ) printf("i OK %s\n", set); else if( ret == PREV_INSERTED ) printf("i PREV %s\n", set); else if( ret == BST_FULL) { printf("ERROR: bst if full!\n\tcould not insert %s\n", set); exit( 1 ); } #endif a++; break; /* Search */ case 'b': ret = search(h, set); #ifdef _LOG if( ret == OP_OK ) printf("b FND %s\n", set); else printf("b ~FND %s\n", set); #endif b++; break; /* Removal */ case 'r': ret = _delete(h, set); #ifdef _LOG if( ret == OP_OK ) printf("r OK %s\n", set); else printf("r ~FND %s\n", set); #endif c++; break; /* Wrong code */ default: printf("ERROR: Wrong input at line %u!\n", i+1); exit(1); } } if(a == 0) a++; if(b == 0) b++; if(c == 0) c++; printf("\n\nSTATISTICS\n==========\n\n"); /* Insertion */ printf(" Insertion Total Time: %lfs\n\tInsertion Average Time: %.12lfms\n", insTime.getCPUTotalSecs(), (insTime.getCPUTotalSecs() / a) * 1000 ); /* Search */ printf(" Search Total Time: %lfs\n\tSearch Average Time: %.12lfms\n", searchTime.getCPUTotalSecs(), (searchTime.getCPUTotalSecs() / b) * 1000 ); /* Removal */ printf(" Remove Total Time: %lfs\n\tRemove Average Time: %.12lfms\n", remTime.getCPUTotalSecs(), (remTime.getCPUTotalSecs() / c) * 1000 ); /* Total running time */ printf("Total Running time: %lfs\n", ( remTime.getCPUTotalSecs() + searchTime.getCPUTotalSecs() + insTime.getCPUTotalSecs() ) ); bst_destroy(h); return 0; }
int main(int argc, char *argv[]) { /* declare variables */ bst *my_tree; int n = N; int i, k; /* for getopt */ int opt; /* for gettimeofday */ struct timeval start, end; long elapsed; /* seed rng */ srand48(SEED); /* process args */ while ((opt = getopt(argc, argv, "n:h")) != -1) { switch (opt) { case 'n': n = atoi(optarg); break; case 'h': default: /* '?' */ usage(argv[0]); } } /**********************************************************************/ /* PART1 - get timing for some random inserts */ /**********************************************************************/ /* start the clock */ gettimeofday(&start, NULL); /* init the tree */ my_tree = bst_create(); /* populate the tree with some random ints */ for (i=0; i<n; i++) { k = (int)(n * drand48()); bst_insert(my_tree, k); } /* disply tree */ //bst_display(my_tree); /* tidy up */ bst_destroy(my_tree); /* stop the clock */ gettimeofday(&end, NULL); elapsed = ((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)) / 1000; // milliseconds /* printf, start... more below */ printf("%d RANDOM %ld ", n, elapsed); /**********************************************************************/ /* PART2 - get timing for linear inserts */ /**********************************************************************/ /* start the clock */ gettimeofday(&start, NULL); /* init the tree */ my_tree = bst_create(); /* populate the tree with ints in linear order */ for (i=0; i<n; i++) { bst_insert(my_tree, i); } //bst_insert(my_tree, -1); /* display tree */ //bst_display(my_tree); //int val; //val=bst_remove(my_tree->root, 100); //printf("min: %d\n", bst_find_min(my_tree->root)); /* tidy up */ bst_destroy(my_tree); /* stop the clock */ gettimeofday(&end, NULL); elapsed = ((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)) / 1000; // milliseconds /* printf, continued */ printf("LINEAR %ld\n", elapsed); return(0); }