position = &node->right;
}
/*为结点分配空间*/
if((new_node = (BiTreeNode*)malloc(sizeof(BiTreeNode)))==NULL)
return -1;
/*将结点插入树中*/
new_node->data = (void*)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
tree->size++;
return 0;
}
/*bitree_rem_left 移除以指定结点的左子结点为根的子树*/
void bitree_rem_left(BiTree *tree,BiTreeNode *node)
{
BiTreeNode **position;
/*从一颗空树中移除节点是不被允许的*/
if(bitree_size == 0)
return;
/*决定从何处移除分支*/
if(node == NULL)
position = &tree->root;
else
position = &node->left;
/*按后序遍历的顺序移除分支*/
if(*position != NULL)
{
bitree_rem_left(tree,*position);
bitree_rem_right(tree,*position);
if(tree->destroy != NULL)
{
/*调用自定义的析构函数释放动态空间*/
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
tree->size--;
}
return;
}
/*bitree_rem_left 移除以指定结点的右子结点为根的子树*/
void betree_rem_right(BiTree *tree,BiTreeNode *node)
{
BiTreeNode **position;
if(bitree_size(tree) == 0)
return ;
if(node == NULL)
position = &tree->root;
else
position = &node->right;
if(position != NULL)
{
bitree_rem_left(tree,*position);
bitree_rem_right(tree,*position);
if(tree->destroy != NULL)
{
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
tree->size--;
}
return ;
}
/*bitree_merge 将两颗二叉树合并为单颗二叉树*/
int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data)
{
/*初始化合并树*/
bitree_init(merge,left->destroy);
/*将传入的data插入到新树的根结点中*/
if(bitree_ins_left(merge,NULL,data) != 0)
{
bitree_destroy(merge);
return -1;
}
/*合并后的树的左右子结点,分别设置为left和right的根结点*/
bitree_root(merge)->left = bitree_root(left);
bitree_root(merge)->right = bitree_root(right);
/*调整合并后的树的结点的数量为本身的数量与左右两颗树的结点数量之和*/
merge->size = merge->size + bitree->size(left) + bitree_size(right);
/*解除原来树中的结点关系,并分别将size设置为0*/
left->root = NULL;
left->size = 0;
right->root = NULL;
right->size = 0;
return 0;
}