Created
May 8, 2020 07:15
-
-
Save parzibyte/6a8614b31a5fa2417cac7a3ca42b9299 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 NodoCadena { | |
private String dato; | |
private NodoCadena izquierda, derecha; | |
public NodoCadena(String dato) { | |
this.dato = dato; | |
this.izquierda = this.derecha = null; | |
} | |
public String getDato() { | |
return dato; | |
} | |
public NodoCadena getIzquierda() { | |
return izquierda; | |
} | |
public void setIzquierda(NodoCadena izquierda) { | |
this.izquierda = izquierda; | |
} | |
public NodoCadena getDerecha() { | |
return derecha; | |
} | |
public void setDerecha(NodoCadena derecha) { | |
this.derecha = derecha; | |
} | |
public void imprimirDato() { | |
System.out.println(this.getDato()); | |
} | |
} | |
class ArbolCadenas { | |
private NodoCadena raiz; | |
public ArbolCadenas() { | |
} | |
public boolean existe(String busqueda) { | |
return existe(this.raiz, busqueda); | |
} | |
private boolean existe(NodoCadena 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 NodoCadena(dato); | |
} else { | |
this.insertar(this.raiz, dato); | |
} | |
} | |
private void insertar(NodoCadena padre, String dato) { | |
if (dato.compareTo(padre.getDato()) > 0) { | |
if (padre.getDerecha() == null) { | |
padre.setDerecha(new NodoCadena(dato)); | |
} else { | |
this.insertar(padre.getDerecha(), dato); | |
} | |
} else { | |
if (padre.getIzquierda() == null) { | |
padre.setIzquierda(new NodoCadena(dato)); | |
} else { | |
this.insertar(padre.getIzquierda(), dato); | |
} | |
} | |
} | |
private void preorden(NodoCadena n) { | |
if (n != null) { | |
n.imprimirDato(); | |
preorden(n.getIzquierda()); | |
preorden(n.getDerecha()); | |
} | |
} | |
private void inorden(NodoCadena n) { | |
if (n != null) { | |
inorden(n.getIzquierda()); | |
n.imprimirDato(); | |
inorden(n.getDerecha()); | |
} | |
} | |
private void postorden(NodoCadena 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