So when I delete in binary search tree, do I need to have like 7 different cases i.e.
You can keep it a lot simpler than that, and simply restrict yourself to three cases when deleting a node from a BST (binary search tree) :
The wiki page contains an example of how this could look in code.
Or as a very basic example in C :
if (current->left==NULL && current->right==NULL) {
/* leaf node */
bst_replace(current, NULL);
}
else if (current->left==NULL || current->right==NULL) {
/* node with one child */
bst_replace(current, ((current->left) ? current->left : current->right));
}
else {
/* node with two children */
Node* successor = bst_next(current);
current->data = successor->data;
bst_replace(successor, successor->right);
}
Deleting a NULL pointer has no ill effect. So, you should be able to do this with no special cases. The basic part is just:
delete current->left;
delete current->right;
I don't really understand the protocol used for deleting here. You seem to not have a binary 'search' tree (no ordering in the tree).
But to just make the code simple. You could do something like this:
bool b1 = (current->left == NULL);
bool b2 = (current->right == NULL);
bool b3 = (current->key > prev->key);
int decision_case = b1 * 4 + b2 * 2 + b3;
switch(decision_case) {
case 0: // fill in code here
break;
...
...
case 7: // fill in code here
break;
}
Also, you should use delete to avoid memory leaks here. Hope that helps.