Mastering Algorithms With C - Kyle Loudon [87]
* Normally allow insertion only at the end of a branch. *
* *
**************************************************************************/
if (bitree_left(node) != NULL)
return -1;
position = &node->left;
}
/*****************************************************************************
* *
* Allocate storage for the node. *
* *
*****************************************************************************/
if ((new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the node into the tree. *
* *
*****************************************************************************/
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/*****************************************************************************
* *
* Adjust the size of the tree to account for the inserted node. *
* *
*****************************************************************************/
tree->size++;
return 0;
}
/*****************************************************************************
* *
* --------------------------- bitree_ins_right --------------------------- *
* *
*****************************************************************************/
int bitree_ins_right(BiTree *tree, BiTreeNode *node, const void *data) {
BiTreeNode *new_node,
**position;
/*****************************************************************************
* *
* Determine where to insert the node. *
* *
*****************************************************************************/
if (node == NULL) {
/**************************************************************************
* *
* Allow insertion at the root only in an empty tree. *
* *
**************************************************************************/
if (bitree_size(tree) > 0)
return -1;
position = &tree->root;
}
else {
/**************************************************************************
* *
* Normally allow insertion only at the end of a branch. *
* *
**************************************************************************/
if (bitree_right(node) != NULL)
return -1;
position = &node->right;
}
/*****************************************************************************
* *
* Allocate storage for the node. *
* *
*****************************************************************************/
if ((new_node = (BiTreeNode *)malloc(sizeof(BiTreeNode))) == NULL)
return -1;
/*****************************************************************************
* *
* Insert the node into the tree. *
* *
*****************************************************************************/
new_node->data = (void *)data;
new_node->left = NULL;
new_node->right = NULL;
*position = new_node;
/*****************************************************************************
* *
* Adjust the size of the tree to account for the inserted node. *
* *
*****************************************************************************/
tree->size++;
return 0;
}
/*****************************************************************************
* *
* ---------------------------- bitree_rem_left --------------------------- *
* *
*****************************************************************************/
void bitree_rem_left(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->left;
/*****************************************************************************