Commits (6)
...@@ -4,4 +4,5 @@ target/ ...@@ -4,4 +4,5 @@ target/
*.classpath *.classpath
*.project *.project
classes/ classes/
test-classes/ test-classes/
\ No newline at end of file /bin/
n,1000,2000,5000,10000,20000,50000
ASCENDANT,1231700,00,4418640,00,30034160,00,116455020,00,474819140,00,3056061360,00
ALEATOIRE,149580,00,167500,00,356340,00,830320,00,1814660,00,5609920,00
n,1000,2000,5000,10000,20000,50000
ASCENDANT,2658220,00,9699160,00,60452520,00,233926280,00,907753720,00,5800536280,00
ALEATOIRE,128340,00,122680,00,310240,00,577920,00,1311820,00,3409080,00
n,1000,2000,5000,10000,20000,50000
ASCENDANT,221140,00,242880,00,405760,00,720540,00,761520,00,1803680,00
ALEATOIRE,153900,00,219500,00,457300,00,938200,00,2106160,00,6222500,00
n,1000,2000,5000,10000,20000,50000
ASCENDANT,136940,00,128920,00,320120,00,733100,00,1475920,00,4872060,00
ALEATOIRE,51780,00,101540,00,308340,00,616120,00,1244160,00,3500360,00
structure,scenario,n,phase,avg_millis
ABR,random,1000,insert,260220,00
ABR,random,1000,search,191900,00
ABR,random,2000,insert,206640,00
ABR,random,2000,search,221760,00
ABR,random,5000,insert,435200,00
ABR,random,5000,search,438360,00
ABR,random,10000,insert,855380,00
ABR,random,10000,search,705300,00
ABR,random,20000,insert,2177680,00
ABR,random,20000,search,1845920,00
ABR,random,50000,insert,6423600,00
ABR,random,50000,search,5389400,00
ABR,ascending,1000,insert,718120,00
ABR,ascending,1000,search,2531360,00
ABR,ascending,2000,insert,4253440,00
ABR,ascending,2000,search,10143660,00
ABR,ascending,5000,insert,28879760,00
ABR,ascending,5000,search,63836740,00
ABR,ascending,10000,insert,115510000,00
ABR,ascending,10000,search,266706780,00
ABR,ascending,20000,insert,475341800,00
ABR,ascending,20000,search,1121616340,00
ABR,ascending,50000,insert,3014206600,00
ABR,ascending,50000,search,6716530900,00
ARN,random,1000,insert,221560,00
ARN,random,1000,search,139960,00
ARN,random,2000,insert,224680,00
ARN,random,2000,search,134720,00
ARN,random,5000,insert,432000,00
ARN,random,5000,search,305080,00
ARN,random,10000,insert,989980,00
ARN,random,10000,search,896760,00
ARN,random,20000,insert,2126220,00
ARN,random,20000,search,1506000,00
ARN,random,50000,insert,7219200,00
ARN,random,50000,search,3469420,00
ARN,ascending,1000,insert,33160,00
ARN,ascending,1000,search,47440,00
ARN,ascending,2000,insert,76180,00
ARN,ascending,2000,search,135580,00
ARN,ascending,5000,insert,216960,00
ARN,ascending,5000,search,374500,00
ARN,ascending,10000,insert,436280,00
ARN,ascending,10000,search,815480,00
ARN,ascending,20000,insert,901520,00
ARN,ascending,20000,search,1773640,00
ARN,ascending,50000,insert,2840800,00
ARN,ascending,50000,search,4844200,00
...@@ -6,148 +6,102 @@ ...@@ -6,148 +6,102 @@
<groupId>edu.Univ-lehavre.MACIA</groupId> <groupId>edu.Univ-lehavre.MACIA</groupId>
<artifactId>ArbreRougeNoir</artifactId> <artifactId>ArbreRougeNoir</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ArbreRougeNoir</name> <name>ArbreRougeNoir</name>
<!-- FIXME change it to the project's website --> <!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url> <url>http://www.example.com</url>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source> <maven.compiler.release>17</maven.compiler.release>
<maven.compiler.target>14</maven.compiler.target>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>6.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>3.6.0</version> <version>3.3.0</version>
<configuration> <configuration>
<configLocation> <archive>
src/properties/stylePerso.xml <manifest>
</configLocation> <mainClass>collection.Benchmark</mainClass>
</configuration> </manifest>
<dependencies> </archive>
<dependency> </configuration>
<groupId>com.puppycrawl.tools</groupId> </plugin>
<artifactId>checkstyle</artifactId> <plugin>
<version>12.1.1</version> <groupId>org.apache.maven.plugins</groupId>
</dependency> <artifactId>maven-project-info-reports-plugin</artifactId>
</dependencies> <version>3.5.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.21.0</version>
</plugin> </plugin>
<plugin> </plugins>
<groupId>org.apache.maven.plugins</groupId> <pluginManagement>
<artifactId>maven-surefire-plugin</artifactId> <plugins>
<version>3.5.1</version> <plugin>
</plugin> <groupId>org.apache.maven.plugins</groupId>
<plugin> <artifactId>maven-checkstyle-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId> <version>3.6.0</version>
<artifactId>maven-jar-plugin</artifactId> <dependencies>
<version>3.4.2</version> <dependency>
<configuration> <groupId>com.puppycrawl.tools</groupId>
<archive> <artifactId>checkstyle</artifactId>
<manifest> <version>12.1.1</version>
<mainClass>collection.Benchmark</mainClass> </dependency>
</manifest> </dependencies>
</archive> </plugin>
</configuration> </plugins>
</plugin> </pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M16</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<reporting> <reporting>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId> <artifactId>maven-jxr-plugin</artifactId>
<version>3.10.1</version> <version>3.6.0</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId> <artifactId>maven-surefire-report-plugin</artifactId>
<version>3.5.0</version> <version>3.5.4</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>
<version>3.5.1</version> <version>3.6.0</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.jacoco</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>jacoco-maven-plugin</artifactId> <artifactId>maven-javadoc-plugin</artifactId>
<version>0.8.12</version> <version>3.12.0</version>
<reportSets> </plugin>
<reportSet> </plugins>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.6.0</version>
</plugin>
</plugins>
</reporting> </reporting>
</project> </project>
...@@ -10,7 +10,7 @@ import java.util.Stack; ...@@ -10,7 +10,7 @@ import java.util.Stack;
/** /**
* Arbre binaire de recherche simple. Implémente l’interface Collection. * Arbre binaire de recherche simple. Implémente l’interface Collection.
*/ */
public class BinarySearchTree<E extends Comparable<E>> implements Collection<E> { public class ArbreBinaireRecherche<E extends Comparable<E>> implements Collection<E> {
/** Classe interne représentant un nœud de l'arbre */ /** Classe interne représentant un nœud de l'arbre */
private class Noeud { private class Noeud {
......
...@@ -9,71 +9,130 @@ import java.util.Collections; ...@@ -9,71 +9,130 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
class Benchmark { public class Benchmark {
private static final Random RNG = new Random(12345);
private static final Random RNG = new Random(12345);
public static void main(final String[] args) throws Exception {
final int[] ns = {1000, 2000, 5000, 10000, 20000, 50000}; // adapte public static void main(final String[] args) throws Exception {
final int reps = 5; // répétitions pour moyenne
String outFile = "benchmark_results.csv"; final int[] N_LIST = { 1000, 2000, 5000, 10000, 20000, 50000 };
final int REP = 5;
try (PrintWriter pw = new PrintWriter(new FileWriter(outFile))) {
pw.println("structure,scenario,n,phase,avg_millis"); // --- Create the 4 CSV writers ---
PrintWriter abrInsert = new PrintWriter(new FileWriter("ABR_insert.csv"));
for (String structure : Arrays.asList("ABR", "ARN")) { PrintWriter abrSearch = new PrintWriter(new FileWriter("ABR_search.csv"));
for (String scenario : Arrays.asList("random", "ascending")) { PrintWriter arnInsert = new PrintWriter(new FileWriter("ARN_insert.csv"));
for (int n : ns) { PrintWriter arnSearch = new PrintWriter(new FileWriter("ARN_search.csv"));
long insertSum = 0;
long searchSum = 0; // Header line : n,1000,2000,...
writeHeader(abrInsert, N_LIST);
for (int r = 0; r < reps; r++) { writeHeader(abrSearch, N_LIST);
List<Integer> keys = new ArrayList<>(); writeHeader(arnInsert, N_LIST);
writeHeader(arnSearch, N_LIST);
for (int i = 0; i < n; i++) {
keys.add(i); // Scenarios
} List<String> scenarios = Arrays.asList("ASCENDANT", "ALEATOIRE");
if (scenario.equals("random")) { // Process each structure
Collections.shuffle(keys, new Random(RNG.nextLong())); for (String structure : Arrays.asList("ABR", "ARN")) {
}
for (String scenario : scenarios) {
// choose structure
Collection<Integer> tree = structure.equals("ARN") // Buffers for one line of insert & search results
? new RedBlackTree<>() : new BinarySearchTree<>(); List<Double> insertLine = new ArrayList<>();
List<Double> searchLine = new ArrayList<>();
long t0 = System.nanoTime();
for (int n : N_LIST) {
for (Integer k : keys) {
tree.add(k); long insertSum = 0;
} long searchSum = 0;
long t1 = System.nanoTime(); for (int r = 0; r < REP; r++) {
long insertMs = (t1 - t0);
insertSum += insertMs; List<Integer> keys = new ArrayList<>();
for (int i = 0; i < n; i++)
// search phase: 0 .. 2n-1 keys.add(i);
long t2 = System.nanoTime();
if (scenario.equals("ALEATOIRE")) {
for (int x = 0; x < 2 * n; x++) { Collections.shuffle(keys, new Random(RNG.nextLong()));
tree.contains(x); }
}
long[] result = structure.equals("ABR") ? benchmarkABR(keys, n) : benchmarkARN(keys, n);
long t3 = System.nanoTime();
long searchMs = (t3 - t2); insertSum += result[0];
searchSum += searchMs; searchSum += result[1];
} }
pw.printf("%s,%s,%d,insert,%.2f%n", structure, scenario, n, insertLine.add(insertSum / (double) REP);
insertSum / (double) reps); searchLine.add(searchSum / (double) REP);
pw.printf("%s,%s,%d,search,%.2f%n", structure, scenario, n,
searchSum / (double) reps); System.out.printf("Done %s %s n=%d\n", structure, scenario, n);
pw.flush(); }
System.out.printf("Done %s %s n=%d%n", structure, scenario, n); // Write to correct CSV
} if (structure.equals("ABR")) {
} writeScenarioLine(abrInsert, scenario, insertLine);
} writeScenarioLine(abrSearch, scenario, searchLine);
} else {
writeScenarioLine(arnInsert, scenario, insertLine);
writeScenarioLine(arnSearch, scenario, searchLine);
}
}
}
abrInsert.close();
abrSearch.close();
arnInsert.close();
arnSearch.close();
System.out.println("4 CSV créés au format tableau.");
}
// -------------------------------------------------------
// Helper: header line
// -------------------------------------------------------
private static void writeHeader(PrintWriter pw, int[] nList) {
pw.print("n");
for (int n : nList)
pw.print("," + n);
pw.println();
}
// -------------------------------------------------------
// Helper: write one scenario line
// -------------------------------------------------------
private static void writeScenarioLine(PrintWriter pw, String scenario, List<Double> values) {
pw.print(scenario);
for (double v : values)
pw.printf(",%.2f", v);
pw.println();
}
// -------------------------------------------------------
// Benchmark ABR / ARN
// -------------------------------------------------------
private static long[] benchmarkABR(List<Integer> keys, int n) {
Collection<Integer> tree = new ArbreBinaireRecherche<>();
return runBenchmark(tree, keys, n);
}
private static long[] benchmarkARN(List<Integer> keys, int n) {
Collection<Integer> tree = new RedBlackTree<>();
return runBenchmark(tree, keys, n);
}
private static long[] runBenchmark(Collection<Integer> tree, List<Integer> keys, int n) {
long t0 = System.nanoTime();
for (Integer k : keys)
tree.add(k);
long t1 = System.nanoTime();
long t2 = System.nanoTime();
for (int x = 0; x < 2 * n; x++)
tree.contains(x);
long t3 = System.nanoTime();
return new long[] { t1 - t0, t3 - t2 };
} }
System.out.println("Wrote results to " + outFile);
}
} }
Ce diff est replié.
package collection;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import org.junit.jupiter.api.*;
public class RedBlackTreeTest {
@Test
void addContainsRemove() {
RedBlackTree<Integer> t = new RedBlackTree<>();
assertTrue(t.add(5));
assertTrue(t.contains(5));
assertFalse(t.add(5)); // no duplicates
assertTrue(t.add(3));
assertTrue(t.add(7));
assertEquals(3, t.size());
assertTrue(t.remove(3));
assertFalse(t.contains(3));
assertEquals(2, t.size());
}
@Test
void inOrderTraversalIsSorted() {
RedBlackTree<Integer> t = new RedBlackTree<>();
List<Integer> vals = Arrays.asList(5, 2, 8, 1, 3, 7, 9);
Collections.shuffle(vals, new Random(42));
t.addAll(vals);
int prev = Integer.MIN_VALUE;
for (Integer v : t) {
assertTrue(v > prev);
prev = v;
}
}
@Test
void largeRandomInsertions() {
RedBlackTree<Integer> t = new RedBlackTree<>();
int n = 10000;
Random rnd = new Random(123);
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < n; i++)
arr.add(i);
Collections.shuffle(arr, rnd);
for (int x : arr)
t.add(x);
assertEquals(n, t.size());
for (int i = 0; i < n; i++)
assertTrue(t.contains(i));
}
}