tipoNo* removerElemento(int numero, tipoNo *raiz){ tipoNo *no; tipoNo *aux; tipoNo *M; int auxCor; no = buscarElemento(numero, raiz); if(raiz == NULL || no == NULL){ return raiz; } auxCor = no->cor; if(no->noEsquerdo == NULL){ aux = no->noDireito; transplante(&raiz,no, no->noDireito); } else if(no->noDireito == NULL){ aux = no->noEsquerdo; transplante(&raiz, no, no->noEsquerdo); } else{ M = menorDosMaiores(no); auxCor = M->cor; aux = M->noDireito; if(M->noPai == no){ if(aux != NULL){ aux->noPai = M; } } else{ transplante(&raiz, M, M->noDireito); M->noDireito = no->noDireito; M->noDireito->noPai = M; } transplante(&raiz, no, M); M->noEsquerdo = no->noEsquerdo; M->noEsquerdo->noPai = M; M->cor = no->cor; } if(auxCor == NEG){ if(aux!= NULL){ conserta(&raiz, aux); } } free(no); return raiz; }
NO* removerElemento(int numero, NO *raiz) { NO *no; NO *aux; NO *M; int auxCor; no = buscarElemento(numero, raiz); if(raiz == NULL || no == NULL) { return raiz; } auxCor = no->cor; if(no->esquerda == NULL) { aux = no->direita; transplante(&raiz,no, no->direita); } else if(no->direita == NULL) { aux = no->esquerda; transplante(&raiz, no, no->esquerda); } else { M = menorDosMaiores(no); auxCor = M->cor; aux = M->direita; if(M->pai == no) { if(aux != NULL) { aux->pai = M; } } else { transplante(&raiz, M, M->direita); M->direita = no->direita; M->direita->pai = M; } transplante(&raiz, no, M); M->esquerda = no->esquerda; M->esquerda->pai = M; M->cor = no->cor; } if(auxCor == NEG) { if(aux!= NULL) { conserta(&raiz, aux); } } free(no); return raiz; }