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... */ } }
struct bnode* bst_del(struct bnode* x, int val) { int cmp; struct bnode* t; if (x == NULL) return NULL; cmp = val - x->v; if (cmp < 0) x->left = bst_del(x->left, val); else if (cmp > 0) x->right = bst_del(x->right, val); else { if (x->right == NULL) return x->left; if (x->left == NULL) return x->right; t = x; x = bst_min(t->right); x->right = bst_del_min(t->right); x->left = t->left; } x->n = bst_size(x->left) + bst_size(x->right) + 1; return x; }
struct bst *bst_min(struct bst *t) { if (t == NULL) return NULL; if (t->lchild == NULL) return t; return bst_min(t->lchild); }
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"); }
struct bst *bst_remove_aux(struct bst *t, char e) { struct bst *p; if (t == NULL) return NULL; if (t->data > e) { // Should remove from left sub tree. t->lchild = bst_remove_aux(t->lchild, e); return t; } else if (t->data < e) { // Should remove from right sub tree. t->rchild = bst_remove_aux(t->rchild, e); return t; } // Remove current node. if (t->rchild == NULL) { // Replace with left child since right child is absent. p = t->lchild; t->lchild = NULL; t->rchild = NULL; bst_destory(t); return p; } if (t->lchild == NULL) { // Replace with right child since left child is absent. p = t->rchild; t->lchild = NULL; t->rchild = NULL; bst_destory(t); return p; } // Replace with right sub tree's smallest node. p = t; t = bst_min(p->rchild); t->rchild = bst_remove_min(p->rchild); t->lchild = p->lchild; p->lchild = NULL; p->rchild = NULL; bst_destory(p); return t; }
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); }
void *bbst_min(bbst *bb) { bst *b = &bb->b; return bst_min(b); }