コード例 #1
0
/**
 * Insert the value into subtree.
 */
_avl_tree_insert_result_t _avl_tree_insert_avlnode(
    const _avl_tree_t* cpt_avl_tree, _avlnode_t* pt_root, const void* cpv_value)
{
    _avl_tree_insert_result_t t_insert_result;
    bool_t           b_result = false;

    assert(cpt_avl_tree != NULL);
    assert(cpv_value != NULL);
    assert(_avl_tree_is_inited(cpt_avl_tree));

    /* if root is NULL then allocate memory */
    if(pt_root == NULL)
    {
        pt_root = _alloc_allocate(
            (_alloc_t*)&cpt_avl_tree->_t_allocator, _AVL_TREE_NODE_SIZE(_GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree)), 1);
        assert(pt_root != NULL);
        _avl_tree_init_elem_auxiliary((_avl_tree_t*)cpt_avl_tree, pt_root);

        pt_root->_pt_left = pt_root->_pt_right = NULL;
        pt_root->_un_height = 0;
        t_insert_result._pt_adjust = pt_root;
        t_insert_result._pt_new = pt_root;
        b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
        _GET_AVL_TREE_TYPE_COPY_FUNCTION(cpt_avl_tree)(pt_root->_pby_data, cpv_value, &b_result);
        assert(b_result);

        return t_insert_result;
    }

    /* compare the value and current node */
    /* if value < current node then insert into left subtree */
    b_result = _GET_AVL_TREE_TYPE_SIZE(cpt_avl_tree);
    _avl_tree_elem_compare_auxiliary(cpt_avl_tree, cpv_value, pt_root->_pby_data, &b_result);
    if(b_result)
    {
        t_insert_result = _avl_tree_insert_avlnode(cpt_avl_tree, pt_root->_pt_left, cpv_value);
        pt_root->_pt_left = t_insert_result._pt_adjust;
        pt_root->_pt_left->_pt_parent = pt_root;

        pt_root = _avl_tree_rebalance(pt_root);
        t_insert_result._pt_adjust = pt_root;

        return t_insert_result;
    }
    /* else insert into right subtree */
    else
    {
        t_insert_result = _avl_tree_insert_avlnode(cpt_avl_tree, pt_root->_pt_right, cpv_value);
        pt_root->_pt_right =  t_insert_result._pt_adjust;
        pt_root->_pt_right->_pt_parent = pt_root;

        pt_root = _avl_tree_rebalance(pt_root);
        t_insert_result._pt_adjust = pt_root;

        return t_insert_result;
    }
}
コード例 #2
0
ファイル: cstl_avl_tree.c プロジェクト: ChenHui109/libcstl
/**
 * Inserts an element into a avl tree.
 */
_avl_tree_iterator_t _avl_tree_insert_equal(_avl_tree_t* pt_avl_tree, const void* cpv_value)
{
    _avl_tree_insert_result_t t_result;
    _avl_tree_iterator_t      it_iter = _create_avl_tree_iterator();

    assert(pt_avl_tree != NULL);
    assert(cpv_value != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));

    t_result = _avl_tree_insert_avlnode(pt_avl_tree, pt_avl_tree->_t_avlroot._pt_parent, cpv_value);
    assert(t_result._pt_adjust != NULL && t_result._pt_new != NULL);

    pt_avl_tree->_t_avlroot._pt_parent = t_result._pt_adjust;
    pt_avl_tree->_t_avlroot._pt_parent->_pt_parent = &pt_avl_tree->_t_avlroot;
    pt_avl_tree->_t_avlroot._pt_left = _avl_tree_get_min_avlnode(pt_avl_tree->_t_avlroot._pt_parent);
    pt_avl_tree->_t_avlroot._pt_right = _avl_tree_get_max_avlnode(pt_avl_tree->_t_avlroot._pt_parent);
    pt_avl_tree->_t_nodecount++;

    _AVL_TREE_ITERATOR_TREE_POINTER(it_iter) = pt_avl_tree;
    _AVL_TREE_ITERATOR_COREPOS(it_iter) = (_byte_t*)t_result._pt_new;

    return it_iter;
}