int main(void) { int value; struct bstree *root = NULL; scanf("%d",&value); while(value != -1) { bstree_insert(&root, value); scanf("%d",&value); } printf("insert finished\n"); bstree_traversal_middle(root); putchar('\n'); bstree_traversal_middle_norecure(root); putchar('\n'); bstree_traversal_head(root); putchar('\n'); bstree_traversal_head_norecure(root); putchar('\n'); bstree_traversal_tail(root); putchar('\n'); bstree_traversal_tail_norecure(root); putchar('\n'); bstree_traversal_level(root); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int i; struct bstree *tree = bstree_new(©fn, &freefn, &int_cmp, 0); int test[] = {20, 15, 32, 40, 25}; for (i = 0; i < sizeof(test)/sizeof(test[0]); i++) { bstree_insert(tree, &test[i], NULL); } test_start(argv); test_case1(tree); assert(bstree_rotate_left(NULL, NULL) == 0); assert(bstree_rotate_left(NULL, tree->root) == 0); assert(bstree_rotate_left(tree, NULL) == 0); assert(bstree_rotate_left(tree, tree->root) == 1); test_case2(tree); assert(bstree_rotate_right(NULL, NULL) == 0); assert(bstree_rotate_right(NULL, tree->root) == 0); assert(bstree_rotate_right(tree, NULL) == 0); assert(bstree_rotate_right(tree, tree->root) == 1); test_case1(tree); test_end(); bstree_delete(tree); return 0; }
int bstree_insert_balanced( bstree_t *tree_in, void *data_in, long key_in, long *idx_out ) { if( bstree_insert( tree_in, data_in, key_in, idx_out ) < 0 ) return -1; if( bstree_balance_insert( tree_in, *idx_out ) < 0 ) return -1; return 0; }
int main(void) { BStree bst; // bst = bstree_ini(1000); // bstree_insert(bst, 6, "correct"); // bstree_insert(bst, 3, "is"); // bstree_insert(bst, 9, "output"); // bstree_insert(bst, 4, "the"); // bstree_insert(bst, 6, "incorrect"); // bstree_insert(bst, 20, "!"); // bstree_insert(bst, 1, "This"); // bstree_traversal(bst); // bstree_free(bst); //Array out fo bound error bst = bstree_ini(2); bstree_insert(bst, 6, "correct"); bstree_insert(bst, 3, "is"); bstree_insert(bst, 9, "output"); bstree_insert(bst, 4, "the"); bstree_insert(bst, 6, "incorrect"); bstree_insert(bst, 20, "!"); bstree_insert(bst, 1, "This"); bstree_traversal(bst); bstree_free(bst); }
/* * 新建结点(key),并将其插入到二叉树中 * * 参数说明: * tree 二叉树的根结点 * key 插入结点的键值 * 返回值: * 根节点 */ Node* insert_bstree(BSTree tree, Type key) { Node *z; // 新建结点 // 如果新建结点失败,则返回。 if ((z=create_bstree_node(key, NULL, NULL, NULL)) == NULL) return tree; return bstree_insert(tree, z); }
void generate_test_tree(int allow_dups) { size_t n = 0, i; int sum = 0, *e=NULL; bstree_t *t = bstree_init(free, (cmp_func_t)intcmp, allow_dups); bst_node_t *node; int inserted = 0, removed = 0; #ifndef _BALANCED_TREE_ int rightrot = 0, leftrot = 0; #endif size_t q = random() % 7000; for (i = 0; i < q; i++) { #ifdef _BALANCED_TREE_ switch (random() % 3) { #else switch (random() % 5) { #endif case 0: case 1: e = (int*)zmalloc(sizeof(int)); *e = random() % 512; n++; sum += *e; bstree_insert(t, e); inserted++; break; case 2: /* try n find a possibly inserted number to remove it */ e = (int*)zmalloc(sizeof(int)); *e = random() % 512; node = bstree_find(t, e); if (node) { n--; sum -= *(int*)node->data; bstree_remove(t, node); removed++; } free(e); break; #ifndef _BALANCED_TREE_ case 3: /* try n find a possibly inserted number to rotate it's node */ e = zmalloc(sizeof(int)); *e = random() % 512; node = bstree_find(t, e); if (node) { bstree_rotate_left(t, node); leftrot++; } free(e); break; case 4: /* try n find a possibly inserted number to rotate it's node */ e = zmalloc(sizeof(int)); *e = random() % 512; node = bstree_find(t, e); if (node) { bstree_rotate_right(t, node); rightrot++; } free(e); break; #endif } #ifdef _BALANCED_TREE_ /* check RB properties */ if (t->root) assert(t->root->color == black); check_red_node_childs(t->root); current_height = 0; black_height = -1; check_black_height(t->root); #endif } #ifndef _BALANCED_TREE_ fprintf(stderr, " i: %d, r: %d, rl: %d, rr:%d ", inserted, removed, leftrot, rightrot); #endif /* check all elements are correct */ if (allow_dups) { assert(t->size == n); totalsum = 0; bstree_foreach(t, (void (*)(void*))acumulate, inorder); assert(totalsum == sum); } else { while (t->size) { bst_node_t *r = t->root; int x = *(int*)r->data; bstree_remove(t, r); assert(bstree_find(t, &x) == NULL); } } /* check propper sorting of elements*/ minimum = 0; bstree_foreach(t, (void (*)(void*))check_set_min, inorder); bstree_destroy(t); } #define ITERATIONS 50 int main(void) { int i; for (i = 1; i <= ITERATIONS; i++) { fprintf(stderr, "\rtesting ... %d%%", 100*i/ITERATIONS); generate_test_tree(1); generate_test_tree(0); } fprintf(stderr, "\n"); return 0; }