/**
 * 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;
    }
}
/**
 * Get data value referenced by iterator.
 */
void _avl_tree_iterator_get_value(_avl_tree_iterator_t it_iter, void* pv_value)
{
    assert(pv_value != NULL);
    assert(_avl_tree_iterator_belong_to_avl_tree(_AVL_TREE_ITERATOR_TREE(it_iter), it_iter));
    assert(!_avl_tree_iterator_equal(it_iter, _avl_tree_end(_AVL_TREE_ITERATOR_TREE(it_iter))));

    /* char* */
    if (strncmp(_GET_AVL_TREE_TYPE_BASENAME(_AVL_TREE_ITERATOR_TREE(it_iter)), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
        *(char**)pv_value = (char*)string_c_str((string_t*)((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_iter))->_pby_data);
    } else {
        bool_t b_result = _GET_AVL_TREE_TYPE_SIZE(_AVL_TREE_ITERATOR_TREE(it_iter));
        _GET_AVL_TREE_TYPE_COPY_FUNCTION(_AVL_TREE_ITERATOR_TREE(it_iter))(
            pv_value, ((_avlnode_t*)_AVL_TREE_ITERATOR_COREPOS(it_iter))->_pby_data, &b_result);
        assert(b_result);
    }
}