Added rotation.
Updated graphs.
This commit is contained in:
@@ -4,31 +4,71 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* Esta clase tiene la estrutura de dato de tipo arbol.
|
||||
*
|
||||
* @author Chris Cromer
|
||||
*/
|
||||
public class Arbol {
|
||||
/**
|
||||
* El arbol.
|
||||
*/
|
||||
private ArbolNodo arbol;
|
||||
|
||||
/**
|
||||
* La cantidad de nodos que están en el arbol.
|
||||
*/
|
||||
private int size;
|
||||
|
||||
/**
|
||||
* La altura del arbol.
|
||||
*/
|
||||
private int altura;
|
||||
|
||||
/**
|
||||
* Los nivles del arbol con todos sus nodos.
|
||||
*/
|
||||
private List<List<ArbolNodo>> niveles;
|
||||
|
||||
public enum PrimerLado {
|
||||
IZQUERDA,
|
||||
DERECHA
|
||||
}
|
||||
/**
|
||||
* El orden que ha sido usado.
|
||||
*/
|
||||
private List<ArbolNodo> order;
|
||||
|
||||
/**
|
||||
* Inicilizar el arbol.
|
||||
*/
|
||||
public Arbol() {
|
||||
this.arbol = null;
|
||||
this.altura = 0;
|
||||
this.niveles = new ArrayList<>();
|
||||
this.order = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Insertar un valor al arbol.
|
||||
*
|
||||
* @param valor int: El valor a insertar.
|
||||
*
|
||||
* @return boolean: Verdad si fue insertado, falso si ya existe el nodo.
|
||||
*/
|
||||
public boolean insertar(int valor) {
|
||||
if (this.arbol == null) {
|
||||
arbol = new ArbolNodo(valor, null);
|
||||
setAltura(1);
|
||||
size++;
|
||||
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int i = 0; i <= getAltura(); i++) {
|
||||
niveles.add(new ArrayList<>());
|
||||
}
|
||||
calcularNiveles(arbol, 0);
|
||||
niveles.remove(niveles.size() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
PrimerLado primerLado = null;
|
||||
ArbolNodo nuevo = new ArbolNodo(valor, null);
|
||||
ArbolNodo actual = arbol;
|
||||
ArbolNodo padre;
|
||||
@@ -40,15 +80,12 @@ public class Arbol {
|
||||
}
|
||||
else if (valor < actual.getValor()) {
|
||||
// Izquerda
|
||||
if (primerLado == null) {
|
||||
primerLado = PrimerLado.IZQUERDA;
|
||||
}
|
||||
actual = actual.getIzquerda();
|
||||
if (actual == null) {
|
||||
nuevo.setPadre(padre);
|
||||
padre.setIzquerda(nuevo);
|
||||
size++;
|
||||
setAltura(getAlturaRecursivo(arbol));
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int i = 0; i <= getAltura(); i++) {
|
||||
@@ -62,15 +99,12 @@ public class Arbol {
|
||||
}
|
||||
else {
|
||||
// Derecha
|
||||
if (primerLado == null) {
|
||||
primerLado = PrimerLado.DERECHA;
|
||||
}
|
||||
actual = actual.getDerecha();
|
||||
if (actual == null) {
|
||||
nuevo.setPadre(padre);
|
||||
padre.setDerecha(nuevo);
|
||||
size++;
|
||||
setAltura(getAlturaRecursivo(arbol));
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int i = 0; i <= getAltura(); i++) {
|
||||
@@ -86,33 +120,354 @@ public class Arbol {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Eliminar un valor del arbol.
|
||||
*
|
||||
* @param valor int: El valor a elminiar.
|
||||
*
|
||||
* @return boolean: Verdad si fue eliminado, falso si no existe.
|
||||
*/
|
||||
public boolean eliminar(int valor) {
|
||||
ArbolNodo actual = arbol;
|
||||
ArbolNodo padre = arbol;
|
||||
boolean izquerda = true;
|
||||
|
||||
while (actual.getValor() != valor) {
|
||||
padre = actual;
|
||||
if (valor < actual.getValor()) {
|
||||
izquerda = true;
|
||||
actual = actual.getIzquerda();
|
||||
}
|
||||
else {
|
||||
izquerda = false;
|
||||
actual = actual.getDerecha();
|
||||
}
|
||||
if (actual == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (actual.getIzquerda() == null && actual.getDerecha() == null) {
|
||||
if (actual == arbol) {
|
||||
arbol = null;
|
||||
}
|
||||
else if (izquerda) {
|
||||
padre.setIzquerda(null);
|
||||
}
|
||||
else {
|
||||
padre.setDerecha(null);
|
||||
}
|
||||
}
|
||||
else if(actual.getDerecha() == null) {
|
||||
if (actual == arbol) {
|
||||
arbol = actual.getIzquerda();
|
||||
}
|
||||
else if (izquerda) {
|
||||
padre.setIzquerda(actual.getIzquerda());
|
||||
}
|
||||
else {
|
||||
padre.setDerecha(actual.getIzquerda());
|
||||
}
|
||||
}
|
||||
else if(actual.getIzquerda() == null) {
|
||||
if (actual == arbol) {
|
||||
arbol = actual.getDerecha();
|
||||
}
|
||||
else if (izquerda) {
|
||||
padre.setIzquerda(actual.getDerecha());
|
||||
}
|
||||
else {
|
||||
padre.setDerecha(actual.getDerecha());
|
||||
}
|
||||
}
|
||||
else {
|
||||
ArbolNodo successorParent = actual;
|
||||
ArbolNodo successor = actual;
|
||||
ArbolNodo current = actual.getDerecha();
|
||||
while (current != null) {
|
||||
successorParent = successor;
|
||||
successor = current;
|
||||
current = current.getIzquerda();
|
||||
}
|
||||
if (successor != actual.getDerecha()) {
|
||||
successorParent.setIzquerda(successor.getDerecha());
|
||||
successor.setDerecha(actual.getDerecha());
|
||||
}
|
||||
|
||||
if (actual == arbol) {
|
||||
arbol = successor;
|
||||
}
|
||||
else if (izquerda) {
|
||||
padre.setIzquerda(successor);
|
||||
}
|
||||
else {
|
||||
padre.setDerecha(successor);
|
||||
}
|
||||
|
||||
successor.setIzquerda(actual.getIzquerda());
|
||||
}
|
||||
size--;
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int i = 0; i <= getAltura(); i++) {
|
||||
niveles.add(new ArrayList<>());
|
||||
}
|
||||
calcularNiveles(arbol, 0);
|
||||
niveles.remove(niveles.size() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotar el arbol usando el hijo a la derecha como un pivot.
|
||||
*
|
||||
* @param valor int: El valor a rotar.
|
||||
*
|
||||
* @return boolean: Verdad si fue rotado.
|
||||
*/
|
||||
public boolean rotarIzquerda(int valor) {
|
||||
for (int i = 0; i < getAltura(); i++) {
|
||||
for (int j = 0; j < niveles.get(i).size(); j++) {
|
||||
if (niveles.get(i).get(j) != null && niveles.get(i).get(j).getValor() == valor) {
|
||||
ArbolNodo padre = niveles.get(i).get(j);
|
||||
ArbolNodo abuelo = padre.getPadre();
|
||||
|
||||
ArbolNodo hijo = padre.getDerecha();
|
||||
if (hijo == null) {
|
||||
return false;
|
||||
}
|
||||
ArbolNodo hijoIzquerda = hijo.getIzquerda();
|
||||
|
||||
if (arbol == padre) {
|
||||
arbol = hijo;
|
||||
}
|
||||
else {
|
||||
if (abuelo.getDerecha() == padre) {
|
||||
abuelo.setDerecha(hijo);
|
||||
}
|
||||
else {
|
||||
abuelo.setIzquerda(hijo);
|
||||
}
|
||||
}
|
||||
|
||||
hijo.setIzquerda(padre);
|
||||
hijo.setPadre(abuelo);
|
||||
padre.setPadre(hijo);
|
||||
if (hijoIzquerda != null) {
|
||||
padre.setDerecha(hijoIzquerda);
|
||||
hijoIzquerda.setPadre(padre);
|
||||
}
|
||||
else {
|
||||
padre.setDerecha(null);
|
||||
}
|
||||
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int k = 0; k <= getAltura(); k++) {
|
||||
niveles.add(new ArrayList<>());
|
||||
}
|
||||
calcularNiveles(arbol, 0);
|
||||
niveles.remove(niveles.size() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotar el arbol usando el hijo a la izquerda como un pivot.
|
||||
*
|
||||
* @param valor int: El valor a rotar.
|
||||
*
|
||||
* @return boolean: Verdad si fue rotado.
|
||||
*/
|
||||
public boolean rotarDerecha(int valor) {
|
||||
for (int i = 0; i < getAltura(); i++) {
|
||||
for (int j = 0; j < niveles.get(i).size(); j++) {
|
||||
if (niveles.get(i).get(j) != null && niveles.get(i).get(j).getValor() == valor) {
|
||||
ArbolNodo padre = niveles.get(i).get(j);
|
||||
ArbolNodo abuelo = padre.getPadre();
|
||||
|
||||
ArbolNodo hijo = padre.getIzquerda();
|
||||
if (hijo == null) {
|
||||
return false;
|
||||
}
|
||||
ArbolNodo hijoDerecha = hijo.getDerecha();
|
||||
|
||||
if (arbol == padre) {
|
||||
arbol = hijo;
|
||||
}
|
||||
else {
|
||||
if (abuelo.getDerecha() == padre) {
|
||||
abuelo.setDerecha(hijo);
|
||||
}
|
||||
else {
|
||||
abuelo.setIzquerda(hijo);
|
||||
}
|
||||
}
|
||||
|
||||
hijo.setDerecha(padre);
|
||||
hijo.setPadre(abuelo);
|
||||
padre.setPadre(hijo);
|
||||
if (hijoDerecha != null) {
|
||||
padre.setIzquerda(hijoDerecha);
|
||||
hijoDerecha.setPadre(padre);
|
||||
}
|
||||
else {
|
||||
padre.setIzquerda(null);
|
||||
}
|
||||
|
||||
setAltura(calcularAltura(arbol));
|
||||
|
||||
niveles = new ArrayList<>();
|
||||
for (int k = 0; k <= getAltura(); k++) {
|
||||
niveles.add(new ArrayList<>());
|
||||
}
|
||||
calcularNiveles(arbol, 0);
|
||||
niveles.remove(niveles.size() - 1);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordenar el arbol usando preOrder.
|
||||
*
|
||||
* @return List: La lista del orden.
|
||||
*/
|
||||
public List<ArbolNodo> preOrder() {
|
||||
order = new ArrayList<>();
|
||||
preOrder(arbol);
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordenar el arbol usando inOrder.
|
||||
*
|
||||
* @return List: La lista del orden.
|
||||
*/
|
||||
public List<ArbolNodo> inOrder() {
|
||||
order = new ArrayList<>();
|
||||
inOrder(arbol);
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordenar el arbol usando postOrder.
|
||||
*
|
||||
* @return List: La lista del orden.
|
||||
*/
|
||||
public List<ArbolNodo> postOrder() {
|
||||
order = new ArrayList<>();
|
||||
postOrder(arbol);
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
* El meteodo recursivo de preOrder que acumulará los nodos en una lista.
|
||||
*
|
||||
* @param nodo ArbolNodo: El nodo a trabajar.
|
||||
*/
|
||||
public void preOrder(ArbolNodo nodo) {
|
||||
if (nodo != null) {
|
||||
order.add(nodo);
|
||||
preOrder(nodo.getIzquerda());
|
||||
preOrder(nodo.getDerecha());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* El meteodo recursivo de inOrder que acumulará los nodos en una lista.
|
||||
*
|
||||
* @param nodo ArbolNodo: El nodo a trabajar.
|
||||
*/
|
||||
public void inOrder(ArbolNodo nodo) {
|
||||
if (nodo != null) {
|
||||
preOrder(nodo.getIzquerda());
|
||||
order.add(nodo);
|
||||
preOrder(nodo.getDerecha());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* El meteodo recursivo de postOrder que acumulará los nodos en una lista.
|
||||
*
|
||||
* @param nodo ArbolNodo: El nodo a trabajar.
|
||||
*/
|
||||
public void postOrder(ArbolNodo nodo) {
|
||||
if (nodo != null) {
|
||||
preOrder(nodo.getIzquerda());
|
||||
preOrder(nodo.getDerecha());
|
||||
order.add(nodo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Devolver el arbol.
|
||||
*
|
||||
* @return ArbolNodo: La raiz del arbol.
|
||||
*/
|
||||
public ArbolNodo getArbol() {
|
||||
return arbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devolver la cantidad de nodos que están en el arbol.
|
||||
*
|
||||
* @return int: La cantidad.
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devolver la altura del arbol.
|
||||
*
|
||||
* @return int: La altura.
|
||||
*/
|
||||
public int getAltura() {
|
||||
return altura;
|
||||
}
|
||||
|
||||
public void setAltura(int altura) {
|
||||
/**
|
||||
* Cambiar la altura del arbol.
|
||||
*
|
||||
* @param altura int: La altura nueva.
|
||||
*/
|
||||
private void setAltura(int altura) {
|
||||
this.altura = altura;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devolver la lista de nivles del arbol.
|
||||
*
|
||||
* @return List: La lista de niveles.
|
||||
*/
|
||||
public List<List<ArbolNodo>> getNiveles() {
|
||||
return niveles;
|
||||
}
|
||||
|
||||
public int getAlturaRecursivo(ArbolNodo nodo) {
|
||||
/**
|
||||
* Encontrar y devolver la altura del arbol usando recursividad.
|
||||
*
|
||||
* @param nodo ArbolNodo: El nodo a trabajar.
|
||||
*
|
||||
* @return int: La altura.
|
||||
*/
|
||||
public int calcularAltura(ArbolNodo nodo) {
|
||||
if (nodo == null) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
int alturaIzquerda = getAlturaRecursivo(nodo.getIzquerda());
|
||||
int alturaDercha = getAlturaRecursivo(nodo.getDerecha());
|
||||
int alturaIzquerda = calcularAltura(nodo.getIzquerda());
|
||||
int alturaDercha = calcularAltura(nodo.getDerecha());
|
||||
if (alturaIzquerda > alturaDercha) {
|
||||
return (alturaIzquerda + 1);
|
||||
}
|
||||
@@ -122,6 +477,12 @@ public class Arbol {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcular los nivles del arbol usando recursividad.
|
||||
*
|
||||
* @param nodo ArbolNodo: El nodo a trabajar.
|
||||
* @param nivel int: El Nivel donde está el nodo.
|
||||
*/
|
||||
public void calcularNiveles(ArbolNodo nodo, int nivel) {
|
||||
try {
|
||||
if (nodo != null) {
|
||||
|
@@ -4,10 +4,12 @@ import javafx.fxml.FXML;
|
||||
import javafx.fxml.Initializable;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.text.Text;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
/**
|
||||
@@ -22,12 +24,24 @@ public class ArbolController implements Initializable {
|
||||
@FXML
|
||||
private TextFieldLimited valorArbol;
|
||||
|
||||
/**
|
||||
* El nodo a rotar.
|
||||
*/
|
||||
@FXML
|
||||
private TextFieldLimited valorRotar;
|
||||
|
||||
/**
|
||||
* Donde poner el contenido de array.
|
||||
*/
|
||||
@FXML
|
||||
private GridPane contenidoArbol;
|
||||
|
||||
/**
|
||||
* El contendido del orden.
|
||||
*/
|
||||
@FXML
|
||||
private HBox contenidoOrder;
|
||||
|
||||
/**
|
||||
* Donde va el codigo a mostrar a la pantalla.
|
||||
*/
|
||||
@@ -49,11 +63,6 @@ public class ArbolController implements Initializable {
|
||||
*/
|
||||
private Arbol arbol;
|
||||
|
||||
/**
|
||||
* Grafico rectangulos.
|
||||
*/
|
||||
private Grafico grafico;
|
||||
|
||||
/**
|
||||
* Inicializar todos los datos y dibujar las graficas.
|
||||
*
|
||||
@@ -66,37 +75,58 @@ public class ArbolController implements Initializable {
|
||||
|
||||
arbol = null;
|
||||
scene = null;
|
||||
}
|
||||
|
||||
// TODO: Remove this
|
||||
arbol = new Arbol();
|
||||
/*arbol.insertar(50);
|
||||
/**
|
||||
* Llenar un arbol al azar.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonLlenar() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
arbol.insertar(25);
|
||||
arbol.insertar(75);
|
||||
/*Random random = new Random();
|
||||
int maximo = 99;
|
||||
int minimo = 0;
|
||||
int rango = maximo - minimo + 1;
|
||||
|
||||
arbol.insertar(35);
|
||||
arbol.insertar(15);*/
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (arbol.size() >= 5) {
|
||||
break;
|
||||
}
|
||||
int numero = random.nextInt(rango) + minimo;
|
||||
while (!arbol.insertar(numero)) {
|
||||
numero = random.nextInt(rango) + minimo;
|
||||
if (arbol.size() >= 5) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
arbol.insertar(5);
|
||||
arbol.insertar(3);
|
||||
arbol.insertar(4);
|
||||
arbol.insertar(2);
|
||||
arbol.insertar(1);
|
||||
arbol.insertar(10);
|
||||
arbol.insertar(8);
|
||||
arbol.insertar(7);
|
||||
arbol.insertar(6);
|
||||
arbol.insertar(9);
|
||||
//arbol.insertar(7);
|
||||
|
||||
/*arbol.insertar(5);
|
||||
arbol.insertar(3);
|
||||
arbol.insertar(4);
|
||||
arbol.insertar(2);
|
||||
arbol.insertar(1);
|
||||
arbol.insertar(6);
|
||||
arbol.insertar(7);
|
||||
arbol.insertar(8);
|
||||
arbol.insertar(9);*/
|
||||
arbol.insertar(12);
|
||||
arbol.insertar(11);
|
||||
arbol.insertar(13);
|
||||
|
||||
generarGrafico();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Vaciar el arbol de todos los valores.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonVaciar() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
arbol = new Arbol();
|
||||
generarGrafico();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,21 +161,20 @@ public class ArbolController implements Initializable {
|
||||
else {
|
||||
Main.mostrarError(resourceBundle.getString("arbolNoValor"), resourceBundle);
|
||||
}
|
||||
generarGrafico();
|
||||
}
|
||||
|
||||
/**
|
||||
* Eliminar un valor del arbol y mostrar el codigo en la pantalla.
|
||||
*/
|
||||
/*@FXML
|
||||
@FXML
|
||||
protected void botonEliminar() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
if (valorArbol.getText() != null && ! valorArbol.getText().trim().equals("")) {
|
||||
try {
|
||||
@@ -155,7 +184,7 @@ public class ArbolController implements Initializable {
|
||||
generarGrafico();
|
||||
}
|
||||
else {
|
||||
Main.mostrarError(resourceBundle.getString("arbolValorExiste"), resourceBundle);
|
||||
Main.mostrarError(resourceBundle.getString("arbolNoEsta"), resourceBundle);
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException exception) {
|
||||
@@ -166,25 +195,141 @@ public class ArbolController implements Initializable {
|
||||
else {
|
||||
Main.mostrarError(resourceBundle.getString("arbolNoValor"), resourceBundle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotar el nodo a la izquerda.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonRotarIzquerda() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
arbol.rotarIzquerda(Integer.valueOf(valorRotar.getText()));
|
||||
|
||||
generarGrafico();
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotar el nodo a la izquerda.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonRotarDerecha() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
arbol.rotarDerecha(Integer.valueOf(valorRotar.getText()));
|
||||
|
||||
generarGrafico();
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar los elementos en orden de pre order.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonPreOrder() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
Colores colores = new Colores();
|
||||
|
||||
contenidoOrder.getChildren().clear();
|
||||
|
||||
List<ArbolNodo> orden = arbol.preOrder();
|
||||
for (ArbolNodo anOrden : orden) {
|
||||
contenidoOrder.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(anOrden.getValor()), String.valueOf(anOrden.getValor())));
|
||||
colores.siguinteColor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar los elementos en orden de in order.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonInOrder() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
Colores colores = new Colores();
|
||||
|
||||
contenidoOrder.getChildren().clear();
|
||||
|
||||
List<ArbolNodo> orden = arbol.inOrder();
|
||||
for (ArbolNodo anOrden : orden) {
|
||||
contenidoOrder.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(anOrden.getValor()), String.valueOf(anOrden.getValor())));
|
||||
colores.siguinteColor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mostrar los elementos en orden de post order.
|
||||
*/
|
||||
@FXML
|
||||
protected void botonPostOrder() {
|
||||
if (scene == null) {
|
||||
initializeArbol();
|
||||
}
|
||||
|
||||
// Mostrar el codigo
|
||||
/*String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next();
|
||||
codigoArray.setText(codigoTexto);*/
|
||||
|
||||
Colores colores = new Colores();
|
||||
|
||||
contenidoOrder.getChildren().clear();
|
||||
|
||||
List<ArbolNodo> orden = arbol.postOrder();
|
||||
for (ArbolNodo anOrden : orden) {
|
||||
contenidoOrder.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(anOrden.getValor()), String.valueOf(anOrden.getValor())));
|
||||
colores.siguinteColor();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Crear un arbol nuevo.
|
||||
*/
|
||||
private void initializeArbol() {
|
||||
scene = contenidoArbol.getScene();
|
||||
// TODO: remove this
|
||||
// Make the grid line present on the screen
|
||||
//contenidoArbol.setGridLinesVisible(true);
|
||||
grafico = new Grafico(scene);
|
||||
//this.arbol = new Arbol();
|
||||
this.arbol = new Arbol();
|
||||
Arbol.Tipos tipos = (Arbol.Tipos) scene.getUserData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Este metodo generará el grafico de arbol en la ventana.
|
||||
*/
|
||||
private void generarGrafico() {
|
||||
grafico.removerDestacar();
|
||||
contenidoOrder.getChildren().clear();
|
||||
|
||||
// Node 0 contains the grid
|
||||
Colores colores = new Colores();
|
||||
|
||||
for (int i = 0; i < arbol.size(); i++) {
|
||||
contenidoOrder.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i)));
|
||||
colores.siguinteColor();
|
||||
}
|
||||
|
||||
// Node 0 contains the grid lines, get them and restore them in the new drawing.
|
||||
//Node node = contenidoArbol.getChildren().get(0);
|
||||
contenidoArbol.getChildren().clear();
|
||||
//contenidoArbol.getChildren().add(0, node);
|
||||
@@ -196,6 +341,13 @@ public class ArbolController implements Initializable {
|
||||
int ancho = (int) Math.pow(2, altura) + (int) ((Math.pow(2, altura)) - 1);
|
||||
|
||||
Text text;
|
||||
if (altura == 0) {
|
||||
contenidoArbol.addColumn(0);
|
||||
text = new Text();
|
||||
text.setText(" ");
|
||||
text.setId(0 + "_" + 0);
|
||||
contenidoArbol.add(text, 0, 0);
|
||||
}
|
||||
for (int i = 0; i < altura; i++) {
|
||||
contenidoArbol.addRow(i);
|
||||
for (int j = 0; j < ancho; j++) {
|
||||
@@ -207,7 +359,7 @@ public class ArbolController implements Initializable {
|
||||
}
|
||||
}
|
||||
|
||||
Colores colores = new Colores();
|
||||
colores = new Colores();
|
||||
int k;
|
||||
int l = 0;
|
||||
for (int i = niveles.size() - 1; i >= 0 ; i--) {
|
||||
@@ -238,8 +390,6 @@ public class ArbolController implements Initializable {
|
||||
}
|
||||
}
|
||||
|
||||
// Check the left for visual conflicts
|
||||
|
||||
colores.siguinteColor();
|
||||
text = (Text) scene.lookup("#texto_" + j + "_" + i);
|
||||
text.setText(String.valueOf(niveles.get(i).get(j).getValor()));
|
||||
|
@@ -5,7 +5,7 @@ import javafx.scene.paint.Color;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Rotación de colores.
|
||||
* Rotación y generación de colores.
|
||||
*
|
||||
* @author Chris Cromer
|
||||
*/
|
||||
@@ -89,10 +89,68 @@ public class Colores {
|
||||
fondo = Color.BLUE;
|
||||
border = Color.BLACK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void randomColor() {
|
||||
/**
|
||||
* Cambiar el color al siguinte. Si no hay, voler al primer.
|
||||
*/
|
||||
public void colorAleatorio() {
|
||||
Random random = new Random();
|
||||
int maximo = MAX_COLORS;
|
||||
int minimo = 1;
|
||||
int rango = maximo - minimo + 1;
|
||||
|
||||
int color = random.nextInt(rango) + minimo;
|
||||
|
||||
switch (color) {
|
||||
case 1:
|
||||
this.color = 2;
|
||||
texto = Color.WHITE;
|
||||
fondo = Color.RED;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
case 2:
|
||||
this.color = 3;
|
||||
texto = Color.BLACK;
|
||||
fondo = Color.WHITE;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
case 3:
|
||||
this.color = 4;
|
||||
texto = Color.BLACK;
|
||||
fondo = Color.PINK;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
case 4:
|
||||
this.color = 5;
|
||||
texto = Color.BLACK;
|
||||
fondo = Color.YELLOW;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
case 5:
|
||||
this.color = 6;
|
||||
texto = Color.WHITE;
|
||||
fondo = Color.GREEN;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
case 6:
|
||||
this.color = 7;
|
||||
texto = Color.BLACK;
|
||||
fondo = Color.ORANGE;
|
||||
border = Color.BLACK;
|
||||
break;
|
||||
default:
|
||||
this.color = 1;
|
||||
texto = Color.WHITE;
|
||||
fondo = Color.BLUE;
|
||||
border = Color.BLACK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generar colores al azar.
|
||||
*/
|
||||
public void randomColorGenrator() {
|
||||
int r = (int)(Math.random() * 256);
|
||||
int g = (int)(Math.random() * 256);
|
||||
int b = (int)(Math.random() * 256);
|
||||
|
@@ -98,11 +98,15 @@ public class TablaHashController implements Initializable {
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int numero = random.nextInt(rango) + minimo;
|
||||
while (! tablaHash.insertar(palabras.getPalabra(), numero)) {
|
||||
if (tablaHash.size() == 10) {
|
||||
while (!tablaHash.insertar(palabras.getPalabra(), numero)) {
|
||||
numero = random.nextInt(rango) + minimo;
|
||||
if (tablaHash.size() >= 10) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (tablaHash.size() >= 10) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
generarGrafico();
|
||||
}
|
||||
|
@@ -92,7 +92,13 @@ colaNoValor=Please input a numeric value.
|
||||
arbolValorExiste=Value already exists.
|
||||
arbolNoEsta=Value does not exist.
|
||||
arbolNoValor=Please input a numeric value.
|
||||
arbolPreOrden=Pre Order
|
||||
arbolInOrden=In Order
|
||||
arbolPostOrden=Post Order
|
||||
arbolRotarIzquerda=Left Rotate
|
||||
arbolRotarDerecha=Right Rotate
|
||||
|
||||
grafoWeight=Weight:
|
||||
grafoNodos=Nodos:
|
||||
grafoEdges=Edges:
|
||||
grafoLleno=Node not inserted because of a maxium of 5 nodes in this implementation.
|
||||
|
@@ -91,7 +91,13 @@ colaNoValor=Ingresar un valor num\u00E9rico por favor.
|
||||
arbolValorExiste=El valor ya existe.
|
||||
arbolNoEsta=El valor no existe.
|
||||
arbolNoValor=Ingresar un valor num\u00E9rico por favor.
|
||||
arbolPreOrden=Pre Orden
|
||||
arbolInOrden=In Orden
|
||||
arbolPostOrden=Post Orden
|
||||
arbolRotarIzquerda=Rotar Izquerda
|
||||
arbolRotarDerecha=Rotar Derecha
|
||||
|
||||
grafoWeight=Peso:
|
||||
grafoNodos=Nodos:
|
||||
grafoEdges=Edges:
|
||||
grafoLleno=El nodo no fue insertado porque esta implementaci\u00F3n tiene un maxiumo de 5 nodos.
|
||||
|
@@ -12,15 +12,28 @@
|
||||
<HBox alignment="TOP_CENTER" VBox.vgrow="ALWAYS" spacing="50">
|
||||
<VBox spacing="10">
|
||||
<HBox alignment="CENTER" spacing="10">
|
||||
<!--<Button text="%llenar" onAction="#botonLlenar"/>-->
|
||||
<!--<Button text="%vaciar" onAction="#botonVaciar"/>-->
|
||||
<Button text="%llenar" onAction="#botonLlenar"/>
|
||||
<Button text="%vaciar" onAction="#botonVaciar"/>
|
||||
</HBox>
|
||||
<HBox alignment="CENTER" spacing="10">
|
||||
<Button text="%insertar" onAction="#botonInsertar"/>
|
||||
<!--<Button text="%eliminar" onAction="#botonEliminar"/>-->
|
||||
<Button text="%eliminar" onAction="#botonEliminar"/>
|
||||
<TextFieldLimited fx:id="valorArbol" maxLength="3" prefWidth="50"/>
|
||||
</HBox>
|
||||
<GridPane fx:id="contenidoArbol" alignment="CENTER"/>
|
||||
<HBox alignment="CENTER" spacing="10">
|
||||
<Button text="%arbolPreOrden" onAction="#botonPreOrder"/>
|
||||
<Button text="%arbolInOrden" onAction="#botonInOrder"/>
|
||||
<Button text="%arbolPostOrden" onAction="#botonPostOrder"/>
|
||||
</HBox>
|
||||
<HBox alignment="CENTER" spacing="10">
|
||||
<Button text="%arbolRotarIzquerda" onAction="#botonRotarIzquerda"/>
|
||||
<Button text="%arbolRotarDerecha" onAction="#botonRotarDerecha"/>
|
||||
<TextFieldLimited fx:id="valorRotar" maxLength="3" prefWidth="50" text="10"/>
|
||||
</HBox>
|
||||
<HBox fx:id="contenidoOrder" alignment="CENTER"/>
|
||||
<ScrollPane fitToHeight="true" fitToWidth="true" VBox.vgrow="ALWAYS">
|
||||
<GridPane fx:id="contenidoArbol" alignment="TOP_LEFT"/>
|
||||
</ScrollPane>
|
||||
</VBox>
|
||||
<StackPane alignment="TOP_LEFT" minWidth="450">
|
||||
<Text fx:id="codigoArbol"/>
|
||||
|
@@ -28,6 +28,8 @@
|
||||
<Button text="%eliminar" onAction="#botonEliminarEdge"/>
|
||||
<TextFieldLimited fx:id="valorNodo1" maxLength="3" prefWidth="50"/>
|
||||
<TextFieldLimited fx:id="valorNodo2" maxLength="3" prefWidth="50"/>
|
||||
<Text text="%grafoWeight"/>
|
||||
<TextFieldLimited fx:id="valorWeight" maxLength="3" prefWidth="50"/>
|
||||
</HBox>
|
||||
<Canvas fx:id="contenidoGrafo" width="300" height="500"/>
|
||||
</VBox>
|
||||
|
Reference in New Issue
Block a user