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 ){ // 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... */ } }
int main(int argc, char const* argv[]) { /* create a new city bst */ bst_t* bst = bst_new(city_free, city_cmp_pol); /* read data and insert into bst */ int read = 0; char name_buf[256]; char country_buf[256]; int population; while (scanf("%[^,],%[^,],%d\n", name_buf, country_buf, &population) == 3) { city_t* new_city = city_new(name_buf, country_buf, population); int ret = bst_insert(bst, new_city); if (ret != BST_SUCCESS) { printf("error inserting city. duplicate?"); city_free(new_city); } read++; } printf("parsed %d cities\n", bst->num_elements); assert(bst->num_elements == read); /* print all cities */ bst_traverse(bst, BST_INORDER, city_print); /* find the largest */ node_t* largest_city_node = bst_max(bst); if (largest_city_node) { printf("largest city is:\n"); city_print(largest_city_node->data); } return 0; }
pair_t bst_max(bst_t bst) { pair_t result = NULL; if (bst != NULL) { if (bst->right == NULL) { result = pair_copy(bst->pair); } else { result = bst_max(bst->right); } } return (result); }
void test_bst_min_max() { bst b; city *c; bst_init(&b, sizeof(city), comp_city_byname); // Insert some cities. city_insert(&b, (adt_add_fn_t)bst_insert, cities_sb_cj_ab); c = bst_min(&b); TEST_ASSERT_EQUAL_STRING(c->name, "alba"); c = bst_max(&b); TEST_ASSERT_EQUAL_STRING(c->name, "sibiu"); }
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); }
bst_t bst_remove(bst_t bst, index_t index) { bst_t current; if (bst != NULL) { if (index_is_less_than(index, pair_fst(bst->pair))) { bst->left = bst_remove(bst->left, index); } else if (index_is_equal(pair_fst(bst->pair), index) && bst->left == NULL) { current = bst->right; bst->pair = pair_destroy(bst->pair); free(bst); bst = current; } else if (index_is_equal(pair_fst(bst->pair), index) && bst->left != NULL) { bst->pair = pair_destroy(bst->pair); bst->pair = bst_max(bst->left); bst->left = delete_max(bst->left); } else { bst->right = bst_remove(bst->right, index); } } return (bst); }
void *bbst_max(bbst *bb) { bst *b = &bb->b; return bst_max(b); }