import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class BSTSearchTimer {
int [] n = {10000, 50000, 100000, 250000};
Random rand = new Random();
public static void main(String[] args) throws IOException{
BSTSearchTimer timer = new BSTSearchTimer();
timer.runBSTSearchTimer();
}
public void runBSTSearchTimer() throws IOException{
PrintWriter out = new PrintWriter( new FileWriter(tree2.csv));
int reps = 10000; // the number of searches that we will do on the tree
for (int i = 0; i < n.length; i++){
BinarySearchTree<Long> longBST = new BinarySearchTree<Long>();
boolean success = true;
int numOfElements = n[i];
while (longBST.size() < numOfElements){
success = longBST.add(rand.nextLong());
while (!success){ // should keep attempting to add values until success is true
success = longBST.add(rand.nextLong());
}
}
long start = System.currentTimeMillis(); // start the timer for searching
for ( int j = 0; j < reps; j++){ // search rep times
longBST.find(rand.nextLong());
}
long end = System.currentTimeMillis(); // end timer for searching tree
double time = end-start;
System.out.printf(%d, %f\n, longBST.size(), time);
out.printf(%d, %f\n, n[i], time);
}
out.close();
}
}
Quand je lance ce programme, il est censé être faire 4 différents arbres de taille: 10000, 50000, 100000, 250000. Je sais que l'efficacité de la vitesse sur la recherche BSTS est censé être O (log n) mais je reçois ces chiffres:
lorsque vous faites 10.000 recherches je reçois ces numéros: (première colonne est la taille de l'arbre, le second est le temps qu'il a fallu faire la recherche)
10000, 9.000000
50000, 3.000000
100000, 4.000000
lorsque vous effectuez 100.000 recherches:
10000, 41.000000
50000, 31.000000
100000, 40.000000
250000, 74.000000
Des conseils sont appréciés.













