BinarySearchTree keres sebesség hatékonyság

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

Amikor a program futtatásához ez állítólag lehet hogy 4 különböző méretű fák: 10000, 50000, 100000, 250000 tudom, hogy a sebesség hatékonyság keres BSTs állítólag O (log n), de kapok ezek a számok:

amikor egy 10.000 keresések kapok ezeket a számokat: (első oszlop a méret a fa, a második pedig az idő telt, hogy ezt a keresést)

10000, 9.000000
50000, 3.000000
100000, 4.000000

amikor egy 100.000 keresések:

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

Minden tipp értékelik.

A kérdést 15/05/2011 16:24
a forrás felhasználó
Más nyelveken...                            


1 válasz

szavazat
1

Legvalószínűbb látsz a hatása „hiányzik”. Mivel csak most keresi véletlen számok, számok, amelyek nincsenek a fa lesz sokkal hosszabb, mint szám, amelyek.

Továbbá, a hatékonyság egy bináris keresési fa O (h), ahol h a fa magasságát. Red-Black fák és AVL fák garantálja, hogy kerül kialakításra, magassága O (log n), de véletlenszerűen épített fák könnyen a végén egy magassága közel O (n).

Válaszolt 15/05/2011 16:34
a forrás felhasználó

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