Trees can be traversed in many ways, mostly used are:
Inorder
,
Preorder
and
Postorder
.
Following breakdown of steps would help:
1. Use one of the tree traversal methods and check if the current node matches with the node you are searching for.
2. If found, delete the node and stop traversing further
3. If the tree is completely traversed and none of the node matched then display Node not found.
bool checkIfNodeExists(struct Node* node, int key)
{
if (node == NULL)
return false;
if (node->data == key)
return true;
bool resultLeft = checkIfNodeExists(node->left, key);
if(resultLeft)
return true;
bool resultRight = checkIfNodeExists(node->right, key);
if(resultRight)
return true;
return false;
}
Quote:
void delete_tree(binarytree *tree, int val)
Your method looks way too complicated. You should extract the common logics out of it.
This method should pass the node you are searching to delete to the
checkIfNodeExists
method and based on the result, take an action of deleting or messaging (if not found). This would make it easier for you to see where you might be missing something and need to make change.