1
0
This commit is contained in:
2025-10-10 02:12:44 +02:00
commit dac3abf431
212 changed files with 475579 additions and 0 deletions

25
PI2/.classpath Normal file
View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/ParteComun">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/DatosCompartidos">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry combineaccessrules="false" kind="src" path="/EjemplosParteComun">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

17
PI2/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PI2</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,6 @@
A(B,C)
A(B(D,E(F,G)),C)
A(B(D,E(F,G)),C(H,I))
A(B(D,E(F,G(J,K))),C(H,I))
A(B(/_,G(H,I)),C(D,E(F,/_)))
A(B(/_,G(H,I)),C(D,E(F(J,K),/_)))

View File

@@ -0,0 +1,6 @@
A(B,C,D)
A(B(E,F),C,D(G,H,I))
A(B(E(/_,J),F),C,D(G,H,I))
A(B(E(/_,J),F),C,D(G,H(K,L),I))
A(B(E(/_,J),F),C(M,/_),D(G,H(K,L),I))
A(B(E(/_,J),F),C(M,/_),D(G,H(K,L(O,O)),I))

View File

@@ -0,0 +1,6 @@
9(6(3,4(2(1,9),1)),7(5,8))
9(6(3,4(2(-1,-11),1)),7(5,8))
9(6(3,4(2(1(0,/_),9(0,/_)),1(0,/_))),7(5(0,/_),8(0,/_)))
9(6(3,4(2(-1(0,/_),-11(0,/_)),1(0,/_))),7(5(0,/_),8(0,/_)))
9(6(3,4(2(1(-2,3),9(0,5)),1(4,0))),7(5(0,6),8(0,/_)))
9(6(-3,4(2(-1(0,5),-11(5,0)),1(-4,-8))),7(5(-1,/_),8(-2,-3)))

View File

@@ -0,0 +1,6 @@
9(6(3,4(2(11,15),1),5),7(5,0,2),8)
9(6(3,4(2(-1,7,-11),-1),5),7(5,4,6))
9(5,6(2,3,4(2(1,-1,9,-5),1)),7(5,8(0,1,2,3)))
9(5,6(2,3,4(2(-1,-11),1(2,3,4,5))),7(5,8,0))
9(1,2(0,1),3(2(4,5),3),4(6,7(8,10)),5(2,3),6)
9(1,2(0,1),3(2(4,5),3(-1(2,-2),1)),4(6,7(8,10)),5(2,3),6)

View File

@@ -0,0 +1,47 @@
package adda.ejercicios;
import java.math.BigInteger;
public class Ejercicio1 {
public static Double fRecDouble(Integer a) {
if(a < 6) {
return 10.;
} else {
return Math.pow(a, 3) * fRecDouble(a-1);
}
}
public static BigInteger fRecBigInteger(Integer a) {
BigInteger res = BigInteger.ZERO;
if(a < 6) {
res = BigInteger.TEN;
} else {
res = BigInteger.valueOf(a).pow(3).multiply(fRecBigInteger(a-1));
}
return res;
}
public static Double fIterDouble(Integer a) {
if (a < 6) {
return 10.0;
} else {
Double result = 10.0;
for (int i = 6; i <= a; i++) {
result *= Math.pow(i, 3);
}
return result;
}
}
public static BigInteger fIterBigInteger(Integer a) {
BigInteger res = BigInteger.TEN;
if (a < 6) {
return res;
} else {
for (int i = 6; i <= a; i++) {
res = res.multiply(BigInteger.valueOf(i).pow(3));
}
}
return res;
}
}

View File

@@ -0,0 +1,57 @@
package adda.ejercicios;
import java.util.List;
import adda.util.TreeUtilities;
import us.lsi.tiposrecursivos.BinaryTree;
import us.lsi.tiposrecursivos.BinaryTree.BEmpty;
import us.lsi.tiposrecursivos.BinaryTree.BLeaf;
import us.lsi.tiposrecursivos.BinaryTree.BTree;
import us.lsi.tiposrecursivos.Tree;
import us.lsi.tiposrecursivos.Tree.TEmpty;
import us.lsi.tiposrecursivos.Tree.TLeaf;
import us.lsi.tiposrecursivos.Tree.TNary;
public class Ejercicio3 {
public static Boolean isBalancedBinary(BinaryTree<Character> tree) {
return switch(tree) {
case BEmpty<Character> t -> true;
case BLeaf<Character> t -> true;
case BTree<Character> t -> {
int heightLeft = TreeUtilities.heightBinary(t.left());
int heightRight = TreeUtilities.heightBinary(t.right());
boolean isLeftBalanced = isBalancedBinary(t.left());
boolean isRightBalanced = isBalancedBinary(t.right());
yield Math.abs(heightLeft - heightRight) <= 1 && isLeftBalanced && isRightBalanced;
}
};
}
public static Boolean isBalancedNary(Tree<Character> tree) {
return switch (tree) {
case TEmpty<Character> t -> true;
case TLeaf<Character> t -> true;
case TNary<Character> t -> {
List<Tree<Character>> children = t.children();
int maxHeight = t.children().stream().mapToInt(x -> TreeUtilities.heightNary(x)).max().getAsInt();
int minHeight = t.children().stream().mapToInt(x -> TreeUtilities.heightNary(x)).min().getAsInt();
boolean isSubtreesBalanced = isBalancedNary(children, 0);
yield (maxHeight - minHeight <= 1) && isSubtreesBalanced;
}
};
}
public static Boolean isBalancedNary(List<Tree<Character>> children, Integer level) {
if (level == children.size()) {
return true;
}
Tree<Character> child = children.get(level);
boolean isSubtreeBalanced = isBalancedNary(child);
return isSubtreeBalanced && isBalancedNary(children, level + 1);
}
}

View File

@@ -0,0 +1,72 @@
package adda.ejercicios;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import us.lsi.tiposrecursivos.BinaryTree;
import us.lsi.tiposrecursivos.BinaryTree.BEmpty;
import us.lsi.tiposrecursivos.BinaryTree.BLeaf;
import us.lsi.tiposrecursivos.BinaryTree.BTree;
import us.lsi.tiposrecursivos.Tree;
import us.lsi.tiposrecursivos.Tree.TEmpty;
import us.lsi.tiposrecursivos.Tree.TLeaf;
import us.lsi.tiposrecursivos.Tree.TNary;
public class Ejercicio4 {
public static List<List<Integer>> ej4Bin(BinaryTree<Integer> tree) {
Function<List<Integer>,Integer> suma = ls -> ls.stream().collect(Collectors.summingInt(i -> i + (i+1)));
List<List<Integer>> res = ej4BinAux(tree).stream()
.filter(path -> suma.apply(path) % path.size() == 0)
.collect(Collectors.toList());
return res;
}
public static List<List<Integer>> ej4BinAux(BinaryTree<Integer> tree) {
return switch (tree) {
case BEmpty<Integer> t -> List.of();
case BLeaf<Integer> t -> List.of(new ArrayList<>(List.of(t.optionalLabel().get())));
case BTree<Integer> t -> {
List<List<Integer>> res = new ArrayList<>();
for (List<Integer> path : ej4BinAux(t.left())) {
List<Integer> newPath = new ArrayList<>(path);
newPath.add(0, t.optionalLabel().get());
res.add(newPath);
}
for (List<Integer> path : ej4BinAux(t.right())) {
List<Integer> newPath = new ArrayList<>(path);
newPath.add(0, t.optionalLabel().get());
res.add(newPath);
}
yield res;
}
};
}
public static List<List<Integer>> ej4Nary(Tree<Integer> tree) {
Function<List<Integer>, Integer> suma = ls -> ls.stream().mapToInt(i -> i + (i + 1)).sum();
List<List<Integer>> res = ej4NaryAux(tree, suma).stream()
.filter(path -> suma.apply(path) % path.size() == 0)
.collect(java.util.stream.Collectors.toList());
return res;
}
private static List<List<Integer>> ej4NaryAux(Tree<Integer> tree, Function<List<Integer>, Integer> suma) {
return switch (tree) {
case TEmpty<Integer> t -> List.of();
case TLeaf<Integer> t -> List.of(new ArrayList<>(List.of(t.optionalLabel().get())));
case TNary<Integer> t -> {
List<List<Integer>> res = new ArrayList<>();
for (Tree<Integer> child : t.children()) {
List<List<Integer>> childPaths = ej4NaryAux(child, suma);
for (List<Integer> path : childPaths) {
path.add(0, t.optionalLabel().get());
res.add(new ArrayList<>(path));
}
}
yield res;
}
};
}
}

View File

@@ -0,0 +1,88 @@
package adda.ejercicios;
import java.math.BigInteger;
public record MatrixFibBigInteger (BigInteger a, BigInteger b) {
/**
* Clase de utilidades para las matrices de Fibonacci, de la forma
* |a+b a|
* | a b|
* Nos referiremos a esas matrices a partir de la tupla (a, b)
*/
public static MatrixFibBigInteger of(BigInteger a, BigInteger b) {
return new MatrixFibBigInteger(a, b);
}
/**
* @return (a, b)*(a,b)
*/
public MatrixFibBigInteger square() {
BigInteger a2 = a.pow(2);
BigInteger b2 = b.pow(2);
BigInteger ab2 = BigInteger.TWO.multiply(a.multiply(b));
return of(a2.add(ab2), a2.add(b2));
}
/**
* @return (1, 0)*(a, b)
*/
public MatrixFibBigInteger next() {
return of(a.add(b), a);
}
/**
* @return (1, 0)
*/
public static MatrixFibBigInteger unit() {
return of(BigInteger.ONE, BigInteger.ZERO);
}
/**
* @return (0, 1)
*/
public static MatrixFibBigInteger identity() {
return of(BigInteger.ZERO, BigInteger.ONE);
}
/**
* @param n
* @return potencia((1, 0), n)
*/
public static MatrixFibBigInteger unitPotencia(Integer n) {
MatrixFibBigInteger res = null;
if (n == 1) res = unit();
else if (n%2 == 0) res = unitPotencia(n/2).square();
else res = unitPotencia(n/2).square().next();
return res;
}
/**
* @param n
* @return potencia((a, b), n)
*/
public MatrixFibBigInteger potencia(Integer n) {
MatrixFibBigInteger res = null;
if (n == 1) res = of(a, b);
else if (n%2 == 0) res = potencia(n/2).square();
else res = potencia(n/2).square().multiply(this);
return res;
}
/**
* @param mf
* @return (a, b)*mf
*/
public MatrixFibBigInteger multiply(MatrixFibBigInteger mf) {
BigInteger aa = a.multiply(mf.a());
BigInteger ab = a.multiply(mf.b());
BigInteger ba = b.multiply(mf.a());
BigInteger bb = b.multiply(mf.b());
return of(aa.add(ab).add(ba), aa.add(bb));
}
}

View File

@@ -0,0 +1,86 @@
package adda.ejercicios;
public record MatrixFibDouble (Double a, Double b) {
/**
* Clase de utilidades para las matrices de Fibonacci, de la forma
* |a+b a|
* | a b|
* Nos referiremos a esas matrices a partir de la tupla (a, b)
*/
public static MatrixFibDouble of(Double a, Double b) {
return new MatrixFibDouble(a, b);
}
/**
* @return (a, b)*(a,b)
*/
public MatrixFibDouble square() {
Double a2 = a*a;
Double b2 = b*b;
Double ab2 = 2.*a*b;
return of(a2+ab2, a2+b2);
}
/**
* @return (1, 0)*(a, b)
*/
public MatrixFibDouble next() {
return of(a+b, a);
}
/**
* @return (1, 0)
*/
public static MatrixFibDouble unit() {
return of(1., 0.);
}
/**
* @return (0, 1)
*/
public static MatrixFibDouble identity() {
return of(0., 1.);
}
/**
* @param n
* @return potencia((1, 0), n)
*/
public static MatrixFibDouble unitPotencia(Integer n) {
MatrixFibDouble res = null;
if (n == 1) res = unit();
else if (n%2 == 0) res = unitPotencia(n/2).square();
else res = unitPotencia(n/2).square().next();
return res;
}
/**
* @param n
* @return potencia((a, b), n)
*/
public MatrixFibDouble potencia(Integer n) {
MatrixFibDouble res = null;
if (n == 1) res = of(a, b);
else if (n%2 == 0) res = potencia(n/2).square();
else res = potencia(n/2).square().multiply(this);
return res;
}
/**
* @param mf
* @return (a, b)*mf
*/
public MatrixFibDouble multiply(MatrixFibDouble mf) {
Double aa = a*mf.a();
Double ab = a*mf.b();
Double ba = b*mf.a();
Double bb = b*mf.b();
return of(aa+ab+ba, aa+bb);
}
}

View File

@@ -0,0 +1,130 @@
package adda.tests;
import java.util.List;
import java.util.function.Function;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
import adda.ejercicios.Ejercicio1;
import us.lsi.curvefitting.DataFile;
import us.lsi.curvefitting.Fit;
import us.lsi.curvefitting.GenData;
import us.lsi.curvefitting.PowerLog;
import us.lsi.graphics.MatPlotLib;
public class TestEjercicio1 {
public static void genDataRD() {
Integer nMin = 100; // Valor mínimo de n
Integer nMax = 1000; // Valor máximo de n
Integer nIncr = 33; // Incremento en los valores de n
Integer nIter = 100000; // Número de iteraciones para cada medición de tiempo
Integer nIterWarmup = 10000; // Número de iteraciones para warmup
String file = "ficheros_generados/rd.txt";
Function<Integer,Long> f1 = GenData.time(t -> Ejercicio1.fRecDouble(t));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f1,file,nMin,nMax,nIncr,nIter,nIterWarmup);
System.out.println("Fin Recursivo Double");
}
public static void genDataRBI() {
Integer nMin = 100; // Valor mínimo de n
Integer nMax = 1000; // Valor máximo de n
Integer nIncr = 33; // Incremento en los valores de n
Integer nIter = 2000; // Número de iteraciones para cada medición de tiempo
Integer nIterWarmup = 1000; // Número de iteraciones para warmup
String file = "ficheros_generados/rbi.txt";
Function<Integer,Long> f1 = GenData.time(t -> Ejercicio1.fRecBigInteger(t));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f1,file,nMin,nMax,nIncr,nIter,nIterWarmup);
System.out.println("Fin Recursivo BigInteger");
}
public static void genDataID() {
Integer nMin = 100; // Valor mínimo de n
Integer nMax = 1000; // Valor máximo de n
Integer nIncr = 33; // Incremento en los valores de n
Integer nIter = 2000; // Número de iteraciones para cada medición de tiempo
Integer nIterWarmup = 1000; // Número de iteraciones para warmup
String file = "ficheros_generados/id.txt";
Function<Integer,Long> f1 = GenData.time(t -> Ejercicio1.fIterDouble(t));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f1,file,nMin,nMax,nIncr,nIter,nIterWarmup);
System.out.println("Fin Iterativo Double");
}
public static void genDataIBI() {
Integer nMin = 100; // Valor mínimo de n
Integer nMax = 1000; // Valor máximo de n
Integer nIncr = 33; // Incremento en los valores de n
Integer nIter = 2000; // Número de iteraciones para cada medición de tiempo
Integer nIterWarmup = 1000; // Número de iteraciones para warmup
String file = "ficheros_generados/ibi.txt";
Function<Integer,Long> f1 = GenData.time(t -> Ejercicio1.fIterBigInteger(t));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f1,file,nMin,nMax,nIncr,nIter,nIterWarmup);
System.out.println("Fin Iterativo BigInteger");
}
public static void showRD() {
String file = "ficheros_generados/rd.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void showRBI() {
String file = "ficheros_generados/rbi.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void showID() {
String file = "ficheros_generados/id.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void showIBI() {
String file = "ficheros_generados/ibi.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void test() {
genDataRD();
genDataRBI();
genDataID();
genDataIBI();
showRD();
showRBI();
showID();
showIBI();
MatPlotLib.showCombined("Comparación", List.of("ficheros_generados/rd.txt","ficheros_generados/rbi.txt",
"ficheros_generados/id.txt","ficheros_generados/ibi.txt"),
List.of("Recursiva Double","Recursiva BigInteger","Iterativa Double","Iterativa BigInteger"));
}
}

View File

@@ -0,0 +1,147 @@
package adda.tests;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.commons.math3.fitting.WeightedObservedPoint;
import us.lsi.curvefitting.DataFile;
import us.lsi.curvefitting.Fit;
import us.lsi.curvefitting.GenData;
import us.lsi.curvefitting.PowerLog;
import us.lsi.graphics.MatPlotLib;
import us.lsi.recursivos.problemasdelistas.ProblemasDeListas;
public class TestEjercicio2 {
private static Integer nMin = 100; // Tamaño mínimo de la lista a ordenar
private static Integer nMax = 10000; // Tamaño máximo de la lista a ordenar
private static Integer nIncr = 3300; // Incremento en el tamaño de la lista
private static Integer nIter = 50; // Número de iteraciones para calcular el
private static Integer nIterWarmup = 500; // Número de iteraciones de calentamiento
private static List<Integer> LIST = new ArrayList<>();
private static Random rr = new Random(System.nanoTime());
// Genera una lista de enteros aleatorios de tamaño t y la asigna a la variable
// 'list'
private static void genList(Integer n) {
List<Integer> ls = new ArrayList<>();
for (int i = 0; i < n; i++) {
ls.add(rr.nextInt(n + 10000));
}
LIST = ls; // Genera una lista de enteros aleatorios de tamaño 't'
}
public static void genData1() {
String file = "ficheros_generados/mergeSort1.txt";
Consumer<Integer> cons = t -> genList(t);
Function<Integer, Long> f = GenData.time(cons, t -> ProblemasDeListas.mergeSort(LIST,1));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f, file, nMin, nMax, nIncr, nIter, nIterWarmup);
}
public static void genData4() {
String file = "ficheros_generados/mergeSort4.txt";
Consumer<Integer> cons = t -> genList(t);
Function<Integer, Long> f = GenData.time(cons, t -> ProblemasDeListas.mergeSort(LIST,4));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f, file, nMin, nMax, nIncr, nIter, nIterWarmup);
}
public static void genData16() {
String file = "ficheros_generados/mergeSort16.txt";
Consumer<Integer> cons = t -> genList(t);
Function<Integer, Long> f = GenData.time(cons, t -> ProblemasDeListas.mergeSort(LIST,16));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f, file, nMin, nMax, nIncr, nIter, nIterWarmup);
}
public static void genData64() {
String file = "ficheros_generados/mergeSort64.txt";
Consumer<Integer> cons = t -> genList(t);
Function<Integer, Long> f = GenData.time(cons, t -> ProblemasDeListas.mergeSort(LIST,64));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f, file, nMin, nMax, nIncr, nIter, nIterWarmup);
}
public static void genData256() {
String file = "ficheros_generados/mergeSort256.txt";
Consumer<Integer> cons = t -> genList(t);
Function<Integer, Long> f = GenData.time(cons, t -> ProblemasDeListas.mergeSort(LIST,256));
// Integer tMin,Integer tMax,Integer tInc,Integer numIter,Integer numIterWarmup
GenData.tiemposEjecucionAritmetica(f, file, nMin, nMax, nIncr, nIter, nIterWarmup);
}
public static void show1() {
String file = "ficheros_generados/mergeSort1.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void show4() {
String file = "ficheros_generados/mergeSort4.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void show16() {
String file = "ficheros_generados/mergeSort16.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void show64() {
String file = "ficheros_generados/mergeSort64.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void show256() {
String file = "ficheros_generados/mergeSort256.txt";
List<WeightedObservedPoint> data = DataFile.points(file);
Fit pl = PowerLog.of();
pl.fit(data);
System.out.println(pl.getExpression());
System.out.println(pl.getEvaluation().getRMS());
MatPlotLib.show(file, pl.getFunction(), pl.getExpression());
}
public static void test() {
genData1();
genData4();
genData16();
genData64();
genData256();
show1();
show4();
show16();
show64();
show256();
MatPlotLib.showCombined("Comparación", List.of("ficheros_generados/mergeSort1.txt","ficheros_generados/mergeSort4.txt",
"ficheros_generados/mergeSort16.txt","ficheros_generados/mergeSort64.txt","ficheros_generados/mergeSort256.txt"),
List.of("Umbral = 1","Umbral = 4","Umbral = 16","Umbral = 64","Umbral = 256"));
}
}

View File

@@ -0,0 +1,33 @@
package adda.tests;
import adda.ejercicios.Ejercicio3;
import us.lsi.common.Files2;
import us.lsi.tiposrecursivos.BinaryTree;
import us.lsi.tiposrecursivos.Tree;
public class TestEjercicio3 {
private static void testBinary() {
int i = 1;
System.out.println("========= ARBOLES BINARIOS =========");
for(String t : Files2.linesFromFile("data/PI2Ej3DatosEntradaBinary.txt")) {
BinaryTree<Character> tree = BinaryTree.parse(t,s -> s.charAt(0));
System.out.println("Arbol " + i + " equilibrado: " + Ejercicio3.isBalancedBinary(tree));
i++;
}
}
private static void testNary() {
int i = 1;
System.out.println("\n\n========= ARBOLES N-ARIOS =========");
for(String t : Files2.linesFromFile("data/PI2Ej3DatosEntradaNary.txt")) {
Tree<Character> tree = Tree.parse(t,s -> s.charAt(0));
System.out.println("Arbol " + i + " equilibrado: " + Ejercicio3.isBalancedNary(tree));
i++;
}
}
public static void test() {
testBinary();
testNary();
}
}

View File

@@ -0,0 +1,41 @@
package adda.tests;
import java.util.List;
import adda.ejercicios.Ejercicio4;
import us.lsi.common.Files2;
import us.lsi.tiposrecursivos.BinaryTree;
import us.lsi.tiposrecursivos.Tree;
public class TestEjercicio4 {
public static void testBinary() {
System.out.println("BINARIO:");
for(String linea : Files2.linesFromFile("data/PI2Ej4DatosEntradaBinary.txt")) {
BinaryTree<Integer> t = BinaryTree.parse(linea, Integer::parseInt);
List<List<Integer>> caminos = Ejercicio4.ej4Bin(t);
System.out.println("Arbol: " + linea);
for(List<Integer> camino : caminos) {
System.out.println(" " + camino);
}
}
}
public static void testNary() {
System.out.println("\n\nN-ARIO:");
for(String linea : Files2.linesFromFile("data/PI2Ej4DatosEntradaNary.txt")) {
Tree<Integer> t = Tree.parse(linea, Integer::parseInt);
List<List<Integer>> caminos = Ejercicio4.ej4Nary(t);
System.out.println("Arbol: " + linea);
for(List<Integer> camino : caminos) {
System.out.println(" " + camino);
}
}
}
public static void test() {
testBinary();
testNary();
}
}

View File

@@ -0,0 +1,49 @@
package adda.tests;
import java.util.Scanner;
public class Tests {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (true) {
System.out.println("\nIntroduce el ejercicio a ejecutar:");
System.out.println("1. Ejercicio 1");
System.out.println("2. Ejercicio 2");
System.out.println("3. Ejercicio 3");
System.out.println("4. Ejercicio 4");
System.out.println("5. Salir");
int opcion = s.nextInt();
try {
switch (opcion) {
case 1:
System.out.println("===== EJERCICIO 1 =====");
TestEjercicio1.test();
break;
case 2:
System.out.println("===== EJERCICIO 2 =====");
TestEjercicio2.test();
break;
case 3:
System.out.println("===== EJERCICIO 3 =====");
TestEjercicio3.test();
break;
case 4:
System.out.println("===== EJERCICIO 4 =====");
TestEjercicio4.test();
break;
case 5:
System.out.println("Saliendo...");
s.close();
System.exit(0);
default:
System.out.println("Ejercicio no válido.");
}
} catch(Exception e) {
System.out.println("Excepcion: \n" + " " + e);
}
}
}
}

View File

@@ -0,0 +1,28 @@
package adda.util;
import us.lsi.tiposrecursivos.BinaryTree;
import us.lsi.tiposrecursivos.BinaryTree.BEmpty;
import us.lsi.tiposrecursivos.BinaryTree.BLeaf;
import us.lsi.tiposrecursivos.BinaryTree.BTree;
import us.lsi.tiposrecursivos.Tree;
import us.lsi.tiposrecursivos.Tree.TEmpty;
import us.lsi.tiposrecursivos.Tree.TLeaf;
import us.lsi.tiposrecursivos.Tree.TNary;
public class TreeUtilities {
public static int heightBinary(BinaryTree<Character> tree) {
return switch (tree) {
case BEmpty<Character> t -> 0;
case BLeaf<Character> t -> 0;
case BTree<Character> t -> Math.max(heightBinary(t.left()), heightBinary(t.right())) +1;
};
}
public static int heightNary(Tree<Character> tree) {
return switch (tree) {
case TEmpty<Character> t -> 0;
case TLeaf<Character> t -> 1;
case TNary<Character> t -> t.children().stream().mapToInt(c -> heightNary(c)).max().orElse(0) + 1;
};
}
}

View File

@@ -0,0 +1,5 @@
module pi2 {
requires datos_compartidos;
requires partecomun;
requires ejemplos_parte_comun;
}