int main()
{
    struct b_tree_node_t *bst_root = DF_NODE(10, DF_NODE(6, DF_NODE(3, DF_LEAF(1), DF_LEAF(4)), DF_LEAF(7)), DF_NODE(19, DF_NODE(12, DF_LEAF(11), DF_NODE(17, DF_LEAF(15), DF_LEAF(18))), DF_LEAF(21)));

    printf("-- initial status:\n");
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- adding 17:\n");
    btree_insert_node(&bst_root, 17);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- adding 30:\n");
    btree_insert_node(&bst_root, 30);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- adding 1:\n");
    btree_insert_node(&bst_root, 1);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- adding 4:\n");
    btree_insert_node(&bst_root, 4);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- remove 11:\n");
    btree_delete_node(&bst_root, 11);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- remove 10:\n");
    btree_delete_node(&bst_root, 10);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- remove 3:\n");
    btree_delete_node(&bst_root, 3);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- remove 4:\n");
    btree_delete_node(&bst_root, 4);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);

    printf("-- remove 15:\n");
    btree_delete_node(&bst_root, 15);
    btree_depth_first_traversal_recursive(bst_root, btree_dummy_visitor);
    printf("\nmin value %u and max value %u\n", (*btree_find_min(&bst_root))->value, (*btree_find_max(&bst_root))->value);
}
int
btree_insert_node(struct b_tree_node_t **root,
                  unsigned int value)
{
    struct b_tree_node_t **child;

    if (!*root) {
        *root = DF_LEAF(value);
        return 0;
    }

    if ((*root)->value == value) {
        return 1;
    }

    child = ((*root)->value > value) ? &(*root)->left : &(*root)->right;

    return btree_insert_node(child, value);
}
Exemplo n.º 3
0
int
btree_add(btree_t *tree, uint32_t key, void *data)
{
    btree_node_t *x;

    btree_validate(tree);
    x = btree_search(tree->root, key);
    if (x != NULL) {
        debug_msg("Item already exists - key %ul\n", key);
        return FALSE;
    }

    x = (btree_node_t *)xmalloc(sizeof(btree_node_t));
    x->key    = key;
    x->data   = data;
    x->parent = NULL;
    x->left   = NULL;
    x->right  = NULL;
    x->magic  = BTREE_NODE_MAGIC;
    btree_insert_node(tree, x);

    return TRUE;
}
Exemplo n.º 4
0
int acl_btree_add(ACL_BTREE *tree, unsigned int key, void *data)
{
	const char *myname = "acl_btree_add";
	BTREE_NODE *x;

	btree_validate(tree);
	x = btree_search(tree->root, key);
	if (x != NULL) {
		acl_msg_error("%s(%d): Item already exists - key %u",
			myname, __LINE__, key);
		return (-1);
	}

	x = (BTREE_NODE *) acl_slice_alloc(tree->slice);
	x->key = key;
	x->data   = data;
	x->parent = NULL;
	x->left   = NULL;
	x->right  = NULL;
	x->magic  = BTREE_NODE_MAGIC;
	btree_insert_node(tree, x);

	return (0);
}