Exemple #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):");
        }
Exemple #2
0
        public static void _234Test()
        {
            int[]          numbers = { 50, 25, 75, 15, 40, 60, 95, 80, 55, 27, 72, 11, 45, 67, 99, 8 };
            BTree234 <int> _234    = new BTree234 <int>();

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

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

            Console.WriteLine("Testing 234 Insertion" +
                              "\nItems to be inserted:");
            foreach (int num in numbers)
            {
                Console.Write(" " + num);
            }
            Console.WriteLine("\nTree after insertion:");
            foreach (int item in numbers)
            {
                _234.Insert(item);
            }
            Console.WriteLine(_234.DisplayTree());

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

            Console.WriteLine("Testing 234 Traversal:");
            Console.WriteLine("In order traversal: " + _234.InOrder());

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

            Console.WriteLine("\nTesting 234 Deletion" +
                              "\nItem to be deleted: 25");
            _234.Delete(25);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(_234.DisplayTree());

            Console.WriteLine("\nTesting 234 Deletion" +
                              "\nItem to be deleted: 72");
            _234.Delete(72);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(_234.DisplayTree());

            Console.WriteLine("\nTesting 234 Deletion" +
                              "\nItem to be deleted: 55");
            _234.Delete(55);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(_234.DisplayTree());

            Console.WriteLine("\nTesting 234 Deletion" +
                              "\nItem to be deleted: 99");
            _234.Delete(80);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(_234.DisplayTree());

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

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

            Console.WriteLine("Testing 234 Find:");
            Console.WriteLine("Attempting to find 75: " + (_234.Find(75) ? "Found" : "Not Found"));
            Console.WriteLine("Attempting to find 12: " + (_234.Find(12) ? "Found" : "Not Found"));
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
        }