I have the following binary tree:
Defined by the following data struct:
typedef struct Node{
int data;
struct Node * left;
struct Node * right;
} node;
typedef struct Node * root;
And I have the following function:
void removeNode(root * start, int key){
node * ant;
if (*start == NULL){
return ;
}
//Verifica se a chave é igual ao nó naquele momento
if((*start)->data == key){
//Verifica se é uma folha
if((*start)->left == NULL && (*start)->right == NULL){
free(*start);
*start = NULL;
return;
}
//Verifica se tem somente um descendente à esquerda
else if((*start)->left != NULL && (*start)->right == NULL){
ant = (*start)->left;
free(*start);
*start = NULL;
return;
}
//Verifica se tem somente um descendente à direita
else if((*start)->left == NULL && (*start)->right != NULL){
ant = (*start)->right;
free(*start);
*start = NULL;
return;
}
} else {
ant = *start;
removeNode(&(*start)->left,key);
ant = *start;
removeNode(&(*start)->right,key);
}
}
I need to remove the nodes with the following specifications:
My problem is to get the antecedent, I tried this way, but when I try to remove 8, for example, ant
is in 6.
How to proceed?