BinarySearchTree खोज गति दक्षता

वोट
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();
}
}

मैं जानता हूँ कि 10000, 50000, 100000, 250000. कि खोज BSTs पर गति दक्षता ओ (लॉग इन करें n) माना जाता है, लेकिन मैं इन नंबरों हो रही है: जब मैं इस कार्यक्रम में यह 4 अलग आकार के पेड़ बना माना जाता है चलाएँ:

जब 10,000 खोज करने की मैं इन नंबरों मिलती है: (प्रथम स्तंभ पेड़ के आकार है, दूसरी बार यह खोज करने के लिए ले लिया है)

10000, 9.000000
50000, 3.000000
100000, 4.000000

जब 100,000 खोज करने की:

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

कोई सुझाव की सराहना की कर रहे हैं।

15/05/2011 को 16:24
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

वोट
1

सबसे अधिक संभावना आप "चूक" के प्रभाव को देख रहे हैं। जब से तुम सिर्फ यादृच्छिक संख्या, संख्या है कि पेड़ में नहीं हैं एक बहुत संख्या में हैं कि तुलना में अधिक समय के लिए खोज रहे हैं।

इसके अलावा, एक द्विआधारी खोज वृक्ष की दक्षता हे (ज), जहां ज पेड़ की ऊंचाई है। लाल काले पेड़ों और AVL पेड़ की गारंटी है कि वे हे (लॉग एन) की ऊंचाई के साथ निर्माण किया जाएगा, लेकिन बेतरतीब ढंग से निर्माण किया पेड़ आसानी से एक ऊंचाई हे (एन) के करीब लग सकती है।

15/05/2011 को 16:34
का स्रोत उपयोगकर्ता

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