BinarySearchTree buscar la eficiencia de velocidad

votos
0
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();
}
}

Cuando ejecuto este programa se supone que debe estar haciendo 4 árboles de diferentes tamaños: 10000, 50000, 100000, 250000. Sé que la eficiencia de velocidad en BSTs búsqueda se supone que es O (log n), pero estoy recibiendo estos números:

cuando se hace 10,000 búsquedas consigo los siguientes números: (primera columna es el tamaño del árbol, el segundo es el tiempo que se tardó en hacer la búsqueda)

10000, 9.000000
50000, 3.000000
100000, 4.000000

cuando se hace 100.000 búsquedas:

10000, 41.000000
50000, 31.000000
100000, 40.000000
250000, 74.000000

Algún consejo son apreciados.

Publicado el 15/05/2011 a las 16:24
fuente por usuario
En otros idiomas...                            


1 respuestas

votos
1

Lo más probable es que se está viendo el efecto de "falla". Ya que sólo está en busca de números aleatorios, números que no están en el árbol tomará mucho más tiempo que el número que son.

Además, la eficiencia de un árbol binario de búsqueda es O (h), donde h es la altura del árbol. Árboles rojo-Negro y árboles AVL garantizan que van a ser construidas con una altura de O (log n), pero los árboles construidos al azar fácilmente podría terminar con una altura próxima a O (n).

Respondida el 15/05/2011 a las 16:34
fuente por usuario

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more