Skip to content

Instantly share code, notes, and snippets.

@joffilyfe
Created July 6, 2015 23:33
Show Gist options
  • Save joffilyfe/ac01aad20d027671b2da to your computer and use it in GitHub Desktop.
Save joffilyfe/ac01aad20d027671b2da to your computer and use it in GitHub Desktop.
Arvore* remove_node(Arvore *arv, int d) {
if (arv == NULL) return NULL;
if (arv->dado > d) {
arv->esquerda = remove_node(arv->esquerda, d);
} else if (arv->dado < d) {
arv->direita = remove_node(arv->direita, d);
} else {
if (arv->esquerda == NULL && arv->direita == NULL) {
free(arv);
arv = NULL;
} else if (arv->esquerda == NULL) {
// printf("Filho a direita\n");
Arvore *aux = arv;
arv = arv->direita;
free(aux);
} else if (arv->direita == NULL) {
Arvore *aux = arv;
arv = arv->esquerda;
free(aux);
} else {
Arvore *aux = arv->esquerda;
while(aux->direita != NULL) {
aux = aux->direita;
}
// printf("[%d]\n", aux->dado);
arv->dado = aux->dado;
aux->dado = d;
arv->esquerda = remove_node(arv->esquerda, d);
}
}
return arv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment