BinarySearchTree ricerca dell'efficienza velocità

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

Quando ho eseguito questo programma che dovrebbe essere fare 4 alberi di diverse dimensioni: 10000, 50000, 100000, 250000. Lo so che l'efficienza velocità su BST ricerca dovrebbe essere O (log n), ma sto ottenendo questi numeri:

quando si fa 10.000 ricerche ottengo questi numeri: (prima colonna è la dimensione dell'albero, il secondo è il tempo impiegato per fare la ricerca)

10000, 9.000000
50000, 3.000000
100000, 4.000000

quando si fa 100.000 ricerche:

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

Eventuali suggerimenti sono apprezzati.

È pubblicato 15/05/2011 alle 16:24
fonte dall'utente
In altre lingue...                            


1 risposte

voti
1

Molto probabilmente si sta vedendo l'effetto di "manca". Dal momento che sei solo alla ricerca di numeri casuali, numeri che non sono nella struttura ci vorrà molto più lungo di numero che sono.

Inoltre, l'efficienza di un albero binario di ricerca è O (h), dove h è l'altezza dell'albero. Alberi rosso-neri e alberi AVL garantiscono che saranno costruiti con un'altezza di O (log n), ma gli alberi costruiti in modo casuale potrebbe facilmente finire con un altezza vicino a O (n).

Risposto il 15/05/2011 a 16:34
fonte dall'utente

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