0
I have the following binary tree :
Defined by the following data manure :
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:
- If the node is a sheet, simply remove it.
- When removing a key node x, with only 1 descending, the ancestor of x will be the ancestor of the descendant of x.
My problem is taking the background, I tried this way, but when I try to remove the 8, for example, the ant
is in 6.
How to proceed?