-
-
Save parzibyte/a053e27e7c50f60a09e1a82b758dac6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void eliminar(int busqueda) { | |
this.raiz = this.eliminar(this.raiz, busqueda); | |
} | |
private Nodo eliminar(Nodo nodo, int busqueda) { | |
if (nodo == null) { | |
return nodo; | |
} | |
if (busqueda > nodo.getDato()) { | |
nodo.setDerecha(this.eliminar(nodo.getDerecha(), busqueda)); | |
} else if (busqueda < nodo.getDato()) { | |
nodo.setIzquierda(this.eliminar(nodo.getIzquierda(), busqueda)); | |
} else { | |
if (nodo.getIzquierda() == null && nodo.getDerecha() == null) { | |
nodo = null; | |
} else if (nodo.getDerecha() != null) { | |
nodo.setDato(this.sucesor(nodo)); | |
nodo.setDerecha(this.eliminar(nodo.getDerecha(), nodo.getDato())); | |
} else { | |
nodo.setDato(this.predecesor(nodo)); | |
nodo.setIzquierda(this.eliminar(nodo.getIzquierda(), nodo.getDato())); | |
} | |
} | |
return nodo; | |
} | |
private int sucesor(Nodo nodo) { | |
nodo = nodo.getDerecha(); | |
while (nodo.getIzquierda() != null) { | |
nodo = nodo.getIzquierda(); | |
} | |
return nodo.getDato(); | |
} | |
private int predecesor(Nodo nodo) { | |
nodo = nodo.getIzquierda(); | |
while (nodo.getDerecha() != null) { | |
nodo = nodo.getDerecha(); | |
} | |
return nodo.getDato(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment