Mastering Algorithms With C - Kyle Loudon [88]
* *
* Remove the nodes. *
* *
*****************************************************************************/
if (*position != NULL) {
bitree_rem_left(tree, *position);
bitree_rem_right(tree, *position);
if (tree->destroy != NULL) {
/***********************************************************************
* *
* Call a user-defined function to free dynamically allocated data. *
* *
***********************************************************************/
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/**************************************************************************
* *
* Adjust the size of the tree to account for the removed node. *
* *
**************************************************************************/
tree->size--;
}
return;
}
/*****************************************************************************
* *
* --------------------------- bitree_rem_right --------------------------- *
* *
*****************************************************************************/
void bitree_rem_right(BiTree *tree, BiTreeNode *node) {
BiTreeNode **position;
/*****************************************************************************
* *
* Do not allow removal from an empty tree. *
* *
*****************************************************************************/
if (bitree_size(tree) == 0)
return;
/*****************************************************************************
* *
* Determine where to remove nodes. *
* *
*****************************************************************************/
if (node == NULL)
position = &tree->root;
else
position = &node->right;
/*****************************************************************************
* *
* Remove the nodes. *
* *
*****************************************************************************/
if (*position != NULL) {
bitree_rem_left(tree, *position);
bitree_rem_right(tree, *position);
if (tree->destroy != NULL) {
/***********************************************************************
* *
* Call a user-defined function to free dynamically allocated data. *
* *
***********************************************************************/
tree->destroy((*position)->data);
}
free(*position);
*position = NULL;
/**************************************************************************
* *
* Adjust the size of the tree to account for the removed node. *
* *
**************************************************************************/
tree->size--;
}
return;
}
/*****************************************************************************
* *
* ----------------------------- bitree_merge ----------------------------- *
* *
*****************************************************************************/
int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void
*data) {
/*****************************************************************************
* *
* Initialize the merged tree. *
* *
*****************************************************************************/
bitree_init(merge, left->destroy);
/*****************************************************************************
* *
* Insert the data for the root node of the merged tree. *
* *
*****************************************************************************/
if (bitree_ins_left(merge, NULL, data) != 0) {
bitree_destroy(merge);
return -1;
}
/*****************************************************************************
* *
* Merge the two binary trees into a single binary tree. *
* *
*****************************************************************************/
bitree_root(merge)->left = bitree_root(left);
bitree_root(merge)->right = bitree_root(right);
/*****************************************************************************
* *
* Adjust the size of the new binary tree. *
* *
*****************************************************************************/
merge->size = merge->size +