Example #1
0
int bstree_delete_index( bstree_t *tree_in, long idx_in, long *idx_out )
{
	long x,y,z,idx;
	z = idx_in;
	if( tree_in->size > 1 )
	{
		if( idx_in == tree_in->min )
		{
			bstree_successor( tree_in, idx_in, &idx );
			tree_in->min = idx;
		}
		if( idx_in == tree_in->max )
		{
			bstree_predecessor( tree_in, idx_in, &idx );
			tree_in->max = idx;
		}
	}

	if( LEFT(z) == -1 || RIGHT(z) == -1 )
		y = z;
	else
		bstree_successor( tree_in, z, &y );
	if( LEFT(y) != -1 )
		x = LEFT(y);
	else
		x = RIGHT(y);
	PARENT(x) = PARENT(y);
	if( PARENT(y) == -1 )
		tree_in->root = x;
	else
	{
		if( y == LEFT(PARENT(y)) )
			LEFT(PARENT(y)) = x;
		else
			RIGHT(PARENT(y)) = x;
	}
	if( y != z )
	{
		KEY(z) = KEY(y);
		if( DATA(y) != NULL && DATA(z) != NULL )
			bcopy( DATA(y), DATA(z), tree_in->step );
	}
	if( idx_out != NULL )
		*idx_out = y;
	--tree_in->size;
	tree_in->start[y].avail = 0;
	return 0;
}
Example #2
0
/* 
 * 删除结点(z),并返回根节点
 *
 * 参数说明:
 *     tree 二叉树的根结点
 *     z 删除的结点
 * 返回值:
 *     根节点
 */
static Node* bstree_delete(BSTree tree, Node *z)
{
    Node *x=NULL;
    Node *y=NULL;

    if ((z->left == NULL) || (z->right == NULL) )
        y = z;
    else
        y = bstree_successor(z);

    if (y->left != NULL)
        x = y->left;
    else
        x = y->right;

    if (x != NULL)
        x->parent = y->parent;

    if (y->parent == NULL)
        tree = x;
    else if (y == y->parent->left)
        y->parent->left = x;
    else
        y->parent->right = x;

    if (y != z) 
        z->key = y->key;

    if (y!=NULL)
        free(y);

    return tree;
}
Example #3
0
BOOL 
bstree_remove_node(struct BSTree *bstree, struct TNode *target, BOOL mem_release)
{
	assert(bstree != NULL);

	if(target == NULL)
		return FALSE;

	struct TNode *ptr, *node;
	if(target->left==NULL || target->right==NULL)
		node = target;
	else
		node = bstree_successor(bstree, target);

	if(node->left != NULL)
		ptr = node->left;
	else
		ptr = node->right;
	if(ptr != NULL) 
		ptr->parent = node->parent;
	if(node->parent == NULL)
		bstree->root = ptr;
	else {
		if(node == node->parent->left)
			node->parent->left = ptr;
		else
			node->parent->right = ptr;
	}

	if(target != node) { /* copy satellite data */
		void *tmp = target->val;
		target->val = node->val;
		node->val = tmp;
	}

	if(bstree->release && mem_release)
		bstree->release(node->val);
	free(node);

	--(bstree->count);
	return TRUE;
}