예제 #1
0
        // Times inserting given number of objects into each tree, performs given number of trial and outputs average runtimes in MS
        public static void TimingTest(int numElements, int numTrials)
        {
            MobileObject[] mObjects      = GenerateMobileObjects(numElements);
            MobileObject[] sortedObjects = new MobileObject[mObjects.Length];
            mObjects.CopyTo(sortedObjects, 0);
            Array.Sort(sortedObjects);
            long elapsedTicBST            = 0;
            long elapsedTicBSTSorted      = 0;
            long elapsedTicAVL            = 0;
            long elapsedTicAVLSorted      = 0;
            long elapsedTicRedBlack       = 0;
            long elapsedTicRedBlackSorted = 0;
            long elapsedTicSplay          = 0;
            long elapsedTicSplaySorted    = 0;
            long elapsedTic234            = 0;
            long elapsedTic234Sorted      = 0;

            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            Console.Write("Timing insertion of " + numElements + " elements(sorted and unsorted)");
            for (int i = 1; i <= numTrials; i++)
            {
                BinarySearchTree <MobileObject> binarySearchTree  = new BinarySearchTree <MobileObject>();
                BinarySearchTree <MobileObject> binarySearchTree2 = new BinarySearchTree <MobileObject>();
                AVLTree <MobileObject>          avlTree           = new AVLTree <MobileObject>();
                AVLTree <MobileObject>          avlTree2          = new AVLTree <MobileObject>();
                RedBlackTree <MobileObject>     redBlackTree      = new RedBlackTree <MobileObject>();
                RedBlackTree <MobileObject>     redBlackTree2     = new RedBlackTree <MobileObject>();
                SplayTree <MobileObject>        splayTree         = new SplayTree <MobileObject>();
                SplayTree <MobileObject>        splayTree2        = new SplayTree <MobileObject>();
                BTree234 <MobileObject>         _234Tree          = new BTree234 <MobileObject>();
                BTree234 <MobileObject>         _234Tree2         = new BTree234 <MobileObject>();
                Console.Write("\nTrial " + i + ":");
                var watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    binarySearchTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicBST += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    binarySearchTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicBSTSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    avlTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicAVL += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    avlTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicAVLSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    redBlackTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicRedBlack += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    redBlackTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicRedBlackSorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    splayTree.Insert(obj);
                }
                watch.Stop();
                elapsedTicSplay += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    splayTree2.Insert(obj);
                }
                watch.Stop();
                elapsedTicSplaySorted += watch.ElapsedTicks;
                Console.Write(" .");

                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in mObjects)
                {
                    _234Tree.Insert(obj);
                }
                watch.Stop();
                elapsedTic234 += watch.ElapsedTicks;
                Console.Write(" .");
                watch = System.Diagnostics.Stopwatch.StartNew();
                foreach (MobileObject obj in sortedObjects)
                {
                    _234Tree2.Insert(obj);
                }
                watch.Stop();
                elapsedTic234Sorted += watch.ElapsedTicks;
                Console.Write(" .");
            }
            elapsedTicBST            /= numTrials;
            elapsedTicBSTSorted      /= numTrials;
            elapsedTicAVL            /= numTrials;
            elapsedTicAVLSorted      /= numTrials;
            elapsedTicRedBlack       /= numTrials;
            elapsedTicRedBlackSorted /= numTrials;
            elapsedTicSplay          /= numTrials;
            elapsedTicSplaySorted    /= numTrials;
            elapsedTic234            /= numTrials;
            elapsedTic234Sorted      /= numTrials;

            Console.WriteLine("\n{0, -55}" + elapsedTicBST + " MS"
                              + "\n{1, -55}" + elapsedTicBSTSorted + " MS"
                              + "\n{2, -55}" + elapsedTicAVL + " MS"
                              + "\n{3, -55}" + elapsedTicAVLSorted + " MS"
                              + "\n{4, -55}" + elapsedTicRedBlack + " MS"
                              + "\n{5, -55}" + elapsedTicRedBlackSorted + " MS"
                              + "\n{6, -55}" + elapsedTicSplay + " MS"
                              + "\n{7, -55}" + elapsedTicSplaySorted + " MS"
                              + "\n{8, -55}" + elapsedTic234 + " MS"
                              + "\n{9, -55}" + elapsedTic234Sorted + " MS"
                              , "BinarySearchTree Insert " + numElements + " MobileObjects:"
                              , "BinarySearchTree Insert " + numElements + " MobileObjects(sorted):"
                              , "AVLTree Insert " + numElements + " MobileObjects:"
                              , "AVLTree Insert " + numElements + " MobileObjects (sorted):"
                              , "RedBlackTree Insert " + numElements + " MobileObjects:"
                              , "RedBlackTree Insert " + numElements + " MobileObjects (sorted):"
                              , "SplayTree Insert " + numElements + " MobileObjects:"
                              , "SplayTree Insert " + numElements + " MobileObjects (sorted):"
                              , "234Tree Insert " + numElements + " MobileObjects:"
                              , "234LTree Insert " + numElements + " MobileObjects (sorted):");
        }
예제 #2
0
        // Tests avl tree insert/delete/traverse/find
        public static void AVLTest()
        {
            AVLTree <int> avl = new AVLTree <int>();

            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
            Console.WriteLine("Testing AVL Functions");

            Console.WriteLine("----------------");

            Console.WriteLine("Testing AVL Insertion(no rotation)" +
                              "\nItems to be inserted: 50, 25, 75");
            avl.Insert(50);
            avl.Insert(25);
            avl.Insert(75);
            Console.WriteLine("Tree after insertion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Insertion(left-left)" +
                              "\nItems to be inserted: 20, 10");
            avl.Insert(20);
            avl.Insert(10);
            Console.WriteLine("Tree after insertion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Insertion(left-right)" +
                              "\nItem to be inserted: 30");
            avl.Insert(30);
            Console.WriteLine("Tree after insertion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Insertion(right-right)" +
                              "\nItems to be inserted: 80, 90");
            avl.Insert(80);
            avl.Insert(90);
            Console.WriteLine("Tree after insertion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Insertion(right-left)" +
                              "\nItem to be inserted: 60");
            avl.Insert(60);
            Console.WriteLine("Tree after insertion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("----------------");

            Console.WriteLine("Testing AVL Traversal:");
            Console.WriteLine("In order traversal: " + avl.InOrder());

            Console.WriteLine("----------------");

            Console.WriteLine("Testing AVL Deletion (no children)" +
                              "\nItem to be deleted: 60");
            avl.Delete(60);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Deletion (2 children)" +
                              "\nItem to be deleted: 75");
            avl.Delete(75);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Deletion (left child)" +
                              "\nItem to be deleted: 20");
            avl.Delete(20);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Deletion (right child)" +
                              "\nItem to be deleted: 80");
            avl.Delete(80);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("\nTesting AVL Deletion (not in tree)" +
                              "\nItem to be deleted: 500");
            avl.Delete(500);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(avl.DisplayTree());

            Console.WriteLine("----------------");

            Console.WriteLine("Testing AVL Find:");
            Console.WriteLine("Attempting to find 40: " + (avl.Find(40) ? "Found" : "Not Found"));
            Console.WriteLine("Attempting to find 50: " + (avl.Find(50) ? "Found" : "Not Found"));
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
        }