/****************************************************************************** **函数名称: avl_insert **功 能: 插入新结点(对外接口) **输入参数: ** tree: 平衡二叉树 ** key: 主键(Primary idx) ** len: 主键长 ** data: 附加数据 **输出参数: NONE **返 回: ** 1. AVL_OK:成功 ** 2. AVL_ERR:失败 ** 3. AVL_NODE_EXIST:已存在 **实现描述: ** 1. 当树的根结点为空时,则直接创建根结点,并赋值 ** 2. 当树的根结点不为空时,则调用_insert()进行处理 **注意事项: ** 1. 当key已经存在时,并不会重置data值. ** 2. 如果要重置data值,请先执行删除操作. **作 者: # Qifeng.zou # 2013.12.12 # ******************************************************************************/ int avl_insert(avl_tree_t *tree, void *_key, int len, void *data) { int64_t idx; /* 非主键 */ avl_key_t key; /* 主键 */ bool taller = false; avl_node_t *root = tree->root; idx = tree->key_cb(_key, len); /* 如果为空树,则创建第一个结点 */ if (NULL == root) { root = (avl_node_t *)tree->alloc(tree->pool, sizeof(avl_node_t)); if (NULL == root) { return AVL_ERR; } root->parent = NULL; root->rchild = NULL; root->lchild = NULL; root->bf = AVL_EH; root->idx = idx; root->data = data; tree->root = root; return AVL_OK; } key.v = _key; key.len = len; return _avl_insert(tree, root, idx, &key, &taller, data); }
AVL* _avl_insert(AVL* node, int num) { AVL **next_node = NULL; AVL * ret = NULL; //inserted node AVL * rnode = NULL; //next_node if (node->key == num) { return node; } if (num < node->key) next_node = &node->lchild; else next_node = &node->rchild; if (*next_node == NULL) { *next_node = (AVL*)calloc(1, sizeof(AVL)); (*next_node)->key = num; (*next_node)->height = 0; ret = *next_node; } else { ret = _avl_insert(*next_node, num); } rnode = *next_node; set_node_height(rnode); check_and_rorate(rnode, node); return ret; }
/****************************************************************************** **函数名称: avl_insert_left **功 能: 在node的左子树中插入新结点(内部接口) **输入参数: ** tree: 平衡二叉树 ** node: 需在该结点的子树上插入key ** idx: 需被插入的key ** key: 主键(Primary idx) ** data: 需要插入的数据 **输出参数: ** taller: 是否增高 **返 回: AVL_OK:成功 AVL_NODE_EXIST:已存在 AVL_ERR:失败 **实现描述: **注意事项: **作 者: # Qifeng.zou # 2013.12.13 # ******************************************************************************/ static int avl_insert_left(avl_tree_t *tree, avl_node_t *node, int64_t idx, const avl_key_t *key, bool *taller, void *data) { int ret; avl_node_t *add; if (NULL == node->lchild) { add = (avl_node_t *)tree->alloc(tree->pool, sizeof(avl_node_t)); if (NULL == add) { *taller = false; return AVL_ERR; } add->lchild = NULL; add->rchild = NULL; add->parent = node; add->idx = idx; add->bf = AVL_EH; add->data = data; node->lchild = add; *taller = true; /* node的高度增加了 */ } else { ret = _avl_insert(tree, node->lchild, idx, key, taller, data); if (AVL_OK != ret) { return ret; } } if (false == *taller) { return AVL_OK; } /* 左增高: 进行平衡化处理 */ switch(node->bf) { case AVL_RH: /* 右高: 左子树增高 不会导致失衡 */ { node->bf = AVL_EH; *taller = false; return AVL_OK; } case AVL_EH: /* 等高: 左子树增高 不会导致失衡 */ { node->bf = AVL_LH; *taller = true; return AVL_OK; } case AVL_LH: /* 左高: 左子树增高 导致失衡 */ { avl_left_balance(tree, node); *taller = false; return AVL_OK; } } return AVL_ERR; }
AVL* avl_insert(AVL** node, int num) { AVL *ret = NULL; AVL *rret = NULL; ret = _avl_insert(*node, num); set_node_height(*node); rret = check_and_rorate(*node, NULL); *node = rret; return ret; }