From ab62ae5ad61ac48d78cd55911695a9a24c0f7be5 Mon Sep 17 00:00:00 2001 From: AlexVelezLl Date: Thu, 14 May 2020 09:47:06 -0500 Subject: [PATCH 1/4] Fix Issues, remove Test --- README.md | 10 +- src/Interfaz/InterfazArbol.java | 2 +- src/tda/AVL.java | 30 +-- test/tda/AVLTest.java | 397 -------------------------------- 4 files changed, 23 insertions(+), 416 deletions(-) delete mode 100644 test/tda/AVLTest.java diff --git a/README.md b/README.md index 80b912b..3c526d4 100644 --- a/README.md +++ b/README.md @@ -25,17 +25,17 @@ The representation of the avl tree in this application is only to save integers, As [freecodecamp](https://www.freecodecamp.org/news/avl-tree-insertion-rotation-and-balance-factor/) describes, a search binary tree is a TDA composed of nodes that have the following rules: -1. Each has a root node at the top. + 1. Each has a root node at the top. -2. Every node has at the most, two children. + 2. Every node has at the most, two children. -3. Every node has only one parent (the root is the one node that has no parents). + 3. Every node has only one parent (the root is the one node that has no parents). -4. For each node, its left child is less or equal than the node, wich is less or equal than its right child. + 4. For each node, its left child is less or equal than the node, wich is less or equal than its right child. And the Avl tree has additionally this rule: -1.- The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee. + 1. The difference between the depth of right and left subtrees cannot be more than one. In order to maintain this guarantee, an implementation of an AVL will include an algorithm to rebalance the tree when adding an additional element would upset this guarantee. There are several algotihm to aim that, a few of these are: LL rotation, RR rotation, LR rotation and RL rotation, you can watch all these algorithsm to balance the tree in this [video](https://www.youtube.com/watch?v=7m94k2Qhg68). diff --git a/src/Interfaz/InterfazArbol.java b/src/Interfaz/InterfazArbol.java index 413fec5..f9b53d8 100644 --- a/src/Interfaz/InterfazArbol.java +++ b/src/Interfaz/InterfazArbol.java @@ -34,7 +34,7 @@ public class InterfazArbol { private ScrollPane sp; private final TextField txtInsertar; private final TextField txtDelete; - StackPane arbol; + private StackPane arbol; public InterfazArbol() { root = new VBox(); diff --git a/src/tda/AVL.java b/src/tda/AVL.java index e1adfa9..cdd918e 100644 --- a/src/tda/AVL.java +++ b/src/tda/AVL.java @@ -485,7 +485,10 @@ private boolean idexOutOfBoundsException(final Pane vb, final int n) { // EX remove public boolean delete(final Object o) throws ClassCastException, NullPointerException { - Nodo borrar = null, mirar = null, cambiar = null, nPadre = null; + Nodo borrar = null; + Nodo mirar = null; + Nodo cambiar = null; + Nodo nPadre = null; Nodo raizTmp = this.getRoot(); E c_aux; boolean salir = false; @@ -622,7 +625,7 @@ else if (borrar.getRight() != null) { altIzq = mirar.getLeft().getAltura(); } - final Nodo cambiar2 = estructurar(mirar, altIzq, altDer); + final Nodo cambiar2 = estructurar(mirar, altDer); final Nodo superior = padre(mirar); if (compararDato(superior.getData(), mirar.getData()) != 0) { @@ -729,30 +732,31 @@ public int equilibrado(final Nodo n) { return Math.max(hIzq, hDer) + 1; } - private Nodo estructurar(Nodo nodo, final int altIzq, final int altDer) { - if (extraeFactorE(nodo) == 2) { - if (extraeFactorE(nodo.getRight()) == 1 || extraeFactorE(nodo.getRight()) == 0) { + private Nodo estructurar(final Nodo nodo, final int altDer) { + Nodo nodoTemp = nodo; + if (extraeFactorE(nodoTemp) == 2) { + if (extraeFactorE(nodoTemp.getRight()) == 1 || extraeFactorE(nodoTemp.getRight()) == 0) { // nodo = rotacionSimpleIzquierda1(nodo); - nodo = rotacionSimpleIzquierda(nodo); + nodoTemp = rotacionSimpleIzquierda(nodoTemp); } - else if (extraeFactorE(nodo.getRight()) == -1) { + else if (extraeFactorE(nodoTemp.getRight()) == -1) { // nodo = rotacionCompuestaDerecha(nodo); - nodo = rotacionDobleDerecha(nodo); + nodoTemp = rotacionDobleDerecha(nodoTemp); } - } else if (extraeFactorE(nodo) == -2) { - if (extraeFactorE(nodo.getLeft()) == -1 || extraeFactorE(nodo.getRight()) == 0) { + } else if (extraeFactorE(nodoTemp) == -2) { + if (extraeFactorE(nodoTemp.getLeft()) == -1 || extraeFactorE(nodoTemp.getRight()) == 0) { // nodo = rotacionSimpleDerecha1(nodo); - nodo = rotacionSimpleDerecha(nodo); + nodoTemp = rotacionSimpleDerecha(nodoTemp); } else if (extraeFactorE(nodo.getLeft()) == 1) { // nodo = rotacionCompuestaIzquierda(nodo); - nodo = rotacionDobleIzquierda(nodo); + nodoTemp = rotacionDobleIzquierda(nodoTemp); } } - return nodo; + return nodoTemp; } /* diff --git a/test/tda/AVLTest.java b/test/tda/AVLTest.java deleted file mode 100644 index 4865d32..0000000 --- a/test/tda/AVLTest.java +++ /dev/null @@ -1,397 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package tda; - -import javafx.scene.layout.Pane; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author ktiusk - */ -public class AVLTest { - - @BeforeClass - public static void setUpClass() { - } - - @AfterClass - public static void tearDownClass() { - } - - @Before - public void setUp() { - } - - @After - public void tearDown() { - } - - /** - * Test of getRoot method, of class AVL. - */ - @Test - public void testGetRoot() { - System.out.println("getRoot"); - AVL instance = null; - Nodo expResult = null; - Nodo result = instance.getRoot(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of setRoot method, of class AVL. - */ - @Test - public void testSetRoot() { - System.out.println("setRoot"); - AVL instance = null; - instance.setRoot(null); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of isEmpty method, of class AVL. - */ - @Test - public void testIsEmpty() { - System.out.println("isEmpty"); - AVL instance = null; - boolean expResult = false; - boolean result = instance.isEmpty(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of height method, of class AVL. - */ - @Test - public void testHeight() { - System.out.println("height"); - AVL instance = new AVL<>(Integer::compareTo); - instance.Insert(8); - instance.Insert(2); - int expResult = 2; - int result = instance.height(); - assertEquals(expResult, result); - if (result != expResult) - fail("The test case is a prototype."); - } - - /** - * Test of totalNodos method, of class AVL. - */ - @Test - public void testTotalNodos() { - System.out.println("totalNodos"); - AVL instance = new AVL<>(Integer::compareTo); - instance.Insert(8); - instance.Insert(2); - int expResult = 2; - int result = instance.totalNodos(); - assertEquals(expResult, result); - if (result != expResult) - fail("The test case is a prototype."); - } - - /** - * Test of contarHojas method, of class AVL. - */ - @Test - public void testContarHojas() { - System.out.println("contarHojas"); - AVL instance = new AVL<>(Integer::compareTo); - instance.Insert(8); - instance.Insert(2); - int expResult = 1; - int result = instance.contarHojas(); - assertEquals(expResult, result); - if (result != expResult) - fail("The test case is a prototype."); - } - - /** - * Test of add method, of class AVL. - */ - @Test - public void testAdd() { - System.out.println("add"); - Object element = null; - AVL instance = null; - boolean expResult = false; - boolean result = instance.add(element); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of Insert method, of class AVL. - */ - @Test - public void testInsert() { - System.out.println("Insert"); - Nodo element = null; - AVL instance = new AVL<>(Integer::compareTo); - instance.Insert(8); - instance.Insert(2); - boolean expResult = false; - boolean result = instance.Insert(element); - assertFalse(result); - // TODO review the generated test code and remove the default call to fail. - if (result != expResult) - fail("The test case is a prototype."); - } - - /** - * Test of deleteNode method, of class AVL. - */ - @Test - public void testDeleteNode() { - System.out.println("deleteNode"); - Object element = null; - AVL instance = null; - boolean expResult = false; - boolean result = instance.delete(element); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of esDegenerado method, of class AVL. - */ - @Test - public void testEsDegenerado() { - System.out.println("esDegenerado"); - AVL instance = null; - boolean expResult = false; - boolean result = instance.esDegenerado(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of calcularCarga method, of class AVL. - */ - @Test - public void testCalcularCarga() { - System.out.println("calcularCarga"); - AVL instance = new AVL<>(Integer::compareTo); - instance.Insert(8); - instance.Insert(2); - int expResult = -1; - int result = instance.calcularCarga(); - assertEquals(expResult, result); - if (result != expResult) - fail("The test case is a prototype."); - } - - /** - * Test of rotacionSimpleIzquierda method, of class AVL. - */ - @Test - public void testRotacionSimpleIzquierda() { - System.out.println("rotacionSimpleIzquierda"); - AVL instance = null; - Nodo expResult = null; - Nodo result = instance.rotacionSimpleIzquierda(null); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of rotacionSimpleDerecha method, of class AVL. - */ - @Test - public void testRotacionSimpleDerecha() { - System.out.println("rotacionSimpleDerecha"); - AVL instance = null; - Nodo expResult = null; - Nodo result = instance.rotacionSimpleDerecha(null); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of rotacionDobleIzquierda method, of class AVL. - */ - @Test - public void testRotacionDobleIzquierda() { - System.out.println("rotacionDobleIzquierda"); - AVL instance = null; - Nodo expResult = null; - Nodo result = instance.rotacionDobleIzquierda(null); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of rotacionDobleDerecha method, of class AVL. - */ - @Test - public void testRotacionDobleDerecha() { - System.out.println("rotacionDobleDerecha"); - AVL instance = null; - Nodo expResult = null; - Nodo result = instance.rotacionDobleDerecha(null); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of max method, of class AVL. - */ - @Test - public void testMax() { - System.out.println("max"); - AVL instance = null; - Object expResult = null; - Object result = instance.max(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of min method, of class AVL. - */ - @Test - public void testMin() { - System.out.println("min"); - AVL instance = null; - Object expResult = null; - Object result = instance.min(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of contains method, of class AVL. - */ - @Test - public void testContains() { - System.out.println("contains"); - Object element = null; - AVL instance = null; - boolean expResult = false; - boolean result = instance.contains(element); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of posOrde method, of class AVL. - */ - @Test - public void testPosOrde() { - System.out.println("posOrde"); - AVL instance = null; - instance.posOrde(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of preOrden method, of class AVL. - */ - @Test - public void testPreOrden() { - System.out.println("preOrden"); - AVL instance = null; - instance.preOrden(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of enOrden method, of class AVL. - */ - @Test - public void testEnOrden() { - System.out.println("enOrden"); - AVL instance = null; - instance.enOrden(); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of nivel method, of class AVL. - */ - @Test - public void testNivel() { - System.out.println("nivel"); - Object element = null; - AVL instance = null; - int expResult = 0; - int result = instance.nivel(element); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of equals method, of class AVL. - */ - @Test - public void testEquals() { - System.out.println("equals"); - Object obj = null; - AVL instance = null; - boolean expResult = false; - boolean result = instance.equals(obj); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of hashCode method, of class AVL. - */ - @Test - public void testHashCode() { - System.out.println("hashCode"); - AVL instance = null; - int expResult = 0; - int result = instance.hashCode(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - - /** - * Test of mostrarArbol method, of class AVL. - */ - @Test - public void testMostrarArbol() { - System.out.println("mostrarArbol"); - AVL instance = null; - Pane expResult = null; - Pane result = instance.mostrarArbol(); - assertEquals(expResult, result); - // TODO review the generated test code and remove the default call to fail. - fail("The test case is a prototype."); - } - -} From 62ef2d4d18f49daccffc8f8daf0717dd7b68b633 Mon Sep 17 00:00:00 2001 From: AlexVelezLl Date: Thu, 14 May 2020 09:53:03 -0500 Subject: [PATCH 2/4] Update readme with Codacy revision --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c526d4..702b933 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ java -jar dist/Avl-Tree.jar [language-shield]: https://img.shields.io/badge/Java-v1.8.0-blue?style=plastic [language-url]: https://www.java.com/es/download/ -[codacybadge-shield]: https://img.shields.io/codacy/grade/3c17ebf7c1954006a60b08b8af2c58e2?style=plastic +[codacybadge-shield]: https://img.shields.io/codacy/grade/4514b80a741147ba9cf70541aca4a806?style=plastic [codacybadge-url]: https://www.codacy.com/manual/AlexVelezLl/AVL-TREE?utm_source=github.com&utm_medium=referral&utm_content=AlexVelezLl/AVL-TREE&utm_campaign=Badge_Grade [repoSize-shield]: https://img.shields.io/github/repo-size/AlexVelezLl/AVL-TREE?style=plastic [repo]: https://github.com/AlexVelezLl/AVL-TREE From def989e07855421a1f381e1932463bde35b1bf1e Mon Sep 17 00:00:00 2001 From: AlexVelezLl Date: Fri, 15 May 2020 21:43:59 -0500 Subject: [PATCH 3/4] Change stuff --- src/tda/AVL.java | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/tda/AVL.java b/src/tda/AVL.java index cdd918e..8e7e085 100644 --- a/src/tda/AVL.java +++ b/src/tda/AVL.java @@ -625,7 +625,7 @@ else if (borrar.getRight() != null) { altIzq = mirar.getLeft().getAltura(); } - final Nodo cambiar2 = estructurar(mirar, altDer); + final Nodo cambiar2 = estructurar(mirar, altIzq, altDer); final Nodo superior = padre(mirar); if (compararDato(superior.getData(), mirar.getData()) != 0) { @@ -732,31 +732,30 @@ public int equilibrado(final Nodo n) { return Math.max(hIzq, hDer) + 1; } - private Nodo estructurar(final Nodo nodo, final int altDer) { - Nodo nodoTemp = nodo; - if (extraeFactorE(nodoTemp) == 2) { - if (extraeFactorE(nodoTemp.getRight()) == 1 || extraeFactorE(nodoTemp.getRight()) == 0) { + private Nodo estructurar(Nodo nodo, final int altIzq, final int altDer) { + if (extraeFactorE(nodo) == 2) { + if (extraeFactorE(nodo.getRight()) == 1 || extraeFactorE(nodo.getRight()) == 0) { // nodo = rotacionSimpleIzquierda1(nodo); - nodoTemp = rotacionSimpleIzquierda(nodoTemp); + nodo = rotacionSimpleIzquierda(nodo); } - else if (extraeFactorE(nodoTemp.getRight()) == -1) { + else if (extraeFactorE(nodo.getRight()) == -1) { // nodo = rotacionCompuestaDerecha(nodo); - nodoTemp = rotacionDobleDerecha(nodoTemp); + nodo = rotacionDobleDerecha(nodo); } - } else if (extraeFactorE(nodoTemp) == -2) { - if (extraeFactorE(nodoTemp.getLeft()) == -1 || extraeFactorE(nodoTemp.getRight()) == 0) { + } else if (extraeFactorE(nodo) == -2) { + if (extraeFactorE(nodo.getLeft()) == -1 || extraeFactorE(nodo.getRight()) == 0) { // nodo = rotacionSimpleDerecha1(nodo); - nodoTemp = rotacionSimpleDerecha(nodoTemp); + nodo = rotacionSimpleDerecha(nodo); } else if (extraeFactorE(nodo.getLeft()) == 1) { // nodo = rotacionCompuestaIzquierda(nodo); - nodoTemp = rotacionDobleIzquierda(nodoTemp); + nodo = rotacionDobleIzquierda(nodo); } } - return nodoTemp; + return nodo; } /* From 9372b7edce75cb470aee707acbdf7a5a933f026a Mon Sep 17 00:00:00 2001 From: AlexVelezLl Date: Fri, 15 May 2020 22:50:00 -0500 Subject: [PATCH 4/4] Fix bug al eliminar menor de los mayoree --- src/Interfaz/InterfazArbol.java | 2 +- src/tda/AVL.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Interfaz/InterfazArbol.java b/src/Interfaz/InterfazArbol.java index f9b53d8..7ae29fb 100644 --- a/src/Interfaz/InterfazArbol.java +++ b/src/Interfaz/InterfazArbol.java @@ -26,7 +26,7 @@ /** * - * @author CORE i7 ULTIMATE + * @author AlexVelezLl */ public class InterfazArbol { private Pane root; diff --git a/src/tda/AVL.java b/src/tda/AVL.java index 8e7e085..2830d31 100644 --- a/src/tda/AVL.java +++ b/src/tda/AVL.java @@ -599,6 +599,7 @@ else if (borrar.getRight() != null) { cambiar.setData(cambiar.getRight().getData()); cambiar.setRight(null); } + borrar.setData(c_aux); } else {