-
-
Save parzibyte/94552589283def661e9e4b2b52e9b812 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
class Arbol { | |
private Nodo raiz; | |
public Arbol() { | |
} | |
public boolean existe(String busqueda) { | |
return existe(this.raiz, busqueda); | |
} | |
private boolean existe(Nodo n, String busqueda) { | |
if (n == null) { | |
return false; | |
} | |
if (n.getDato().equals(busqueda)) { | |
return true; | |
} else if (busqueda.compareTo(n.getDato()) < 0) { | |
return existe(n.getIzquierda(), busqueda); | |
} else { | |
return existe(n.getDerecha(), busqueda); | |
} | |
} | |
public void insertar(String dato) { | |
if (this.raiz == null) { | |
this.raiz = new Nodo(dato); | |
} else { | |
this.insertar(this.raiz, dato); | |
} | |
} | |
private void insertar(Nodo padre, String dato) { | |
if (dato.compareTo(padre.getDato()) > 0) { | |
if (padre.getDerecha() == null) { | |
padre.setDerecha(new Nodo(dato)); | |
} else { | |
this.insertar(padre.getDerecha(), dato); | |
} | |
} else { | |
if (padre.getIzquierda() == null) { | |
padre.setIzquierda(new Nodo(dato)); | |
} else { | |
this.insertar(padre.getIzquierda(), dato); | |
} | |
} | |
} | |
private void preorden(Nodo n) { | |
if (n != null) { | |
n.imprimirDato(); | |
preorden(n.getIzquierda()); | |
preorden(n.getDerecha()); | |
} | |
} | |
private void inorden(Nodo n) { | |
if (n != null) { | |
inorden(n.getIzquierda()); | |
n.imprimirDato(); | |
inorden(n.getDerecha()); | |
} | |
} | |
private void postorden(Nodo n) { | |
if (n != null) { | |
postorden(n.getIzquierda()); | |
postorden(n.getDerecha()); | |
n.imprimirDato(); | |
} | |
} | |
public void preorden() { | |
this.preorden(this.raiz); | |
} | |
public void inorden() { | |
this.inorden(this.raiz); | |
} | |
public void postorden() { | |
this.postorden(this.raiz); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment