Beispiel #1
0
/*
 * (递归实现)查找"二叉树x"中键值为key的节点
 */
Node* bstree_search(BSTree x, Type key)
{
    if (x==NULL || x->key==key)
        return x;

    if (key < x->key)
        return bstree_search(x->left, key);
    else
        return bstree_search(x->right, key);
}
Beispiel #2
0
int bstree_delete_key_balanced( bstree_t *tree_in, target_t target_in, long *idx_out )
{
	if( bstree_search( tree_in, tree_in->root, target_in, idx_out ) >= 0 )
		return bstree_delete_index_balanced( tree_in, *idx_out );
	else
		return -1; /* Did not exist so no need to delete it */
	return 0;
}
Beispiel #3
0
/* 
 * 删除结点(key为节点的键值),并返回根节点
 *
 * 参数说明:
 *     tree 二叉树的根结点
 *     z 删除的结点
 * 返回值:
 *     根节点
 */
Node* delete_bstree(BSTree tree, Type key)
{
    Node *z, *node; 

    if ((z = bstree_search(tree, key)) != NULL)
        tree = bstree_delete(tree, z);

    return tree;
}
Beispiel #4
0
int bstree_search( bstree_t *tree_in, long begin_in, target_t target_in, long *idx_out )
{
	if( begin_in != -1 )
	{
		if( tree_in->compf == NULL )
		{
			if( tree_in->start[begin_in].key == target_in.integer )
			{
				*idx_out = begin_in;
				return 0; /* zero indicates that it was found */
			}
			else
			{
				if( target_in.integer < tree_in->start[begin_in].key )
					return bstree_search( tree_in, tree_in->start[begin_in].left, target_in, idx_out );
				else
					return bstree_search( tree_in, tree_in->start[begin_in].right, target_in, idx_out );
				
			}
		}
		else
		{
			if( tree_in->compf( tree_in->start[begin_in].data, target_in.pointer ) == 0 )
			{
				*idx_out = begin_in;
				return 0; /* zero indicates that it was found */
			}
			else
			{
				if( tree_in->compf( target_in.pointer, tree_in->start[begin_in].data ) < 0 )
					return bstree_search( tree_in, tree_in->start[begin_in].left, target_in, idx_out );
				else
					return bstree_search( tree_in, tree_in->start[begin_in].right, target_in, idx_out );
				
			}
		}
	}
	return -1;
}