Esempio n. 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):");
        }
Esempio n. 2
0
        // Tests red black tree insert/delete/traverse/find
        public static void RedBlackTest()
        {
            RedBlackTree <int> redBlack = new RedBlackTree <int>();

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

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

            Console.WriteLine("Testing RedBlack Insertion(root)" +
                              "\nItem to be inserted: 50");
            redBlack.Insert(50);
            Console.WriteLine("\nTree after insertion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after insertion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Insertion(black parent)" +
                              "\nItems to be inserted: 25, 70");
            redBlack.Insert(25);
            redBlack.Insert(70);
            Console.WriteLine("\nTree after insertion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after insertion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Insertion(red uncle)" +
                              "\nItem to be inserted: 80");
            redBlack.Insert(80);
            Console.WriteLine("\nTree after insertion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after insertion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Insertion(black uncle)" +
                              "\nItems to be inserted: 75");
            redBlack.Insert(75);
            Console.WriteLine("\nTree after insertion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after insertion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

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

            Console.WriteLine("Testing RedBlack Traversal:");
            Console.WriteLine("In order traversal: " + redBlack.InOrder());

            Console.WriteLine("----------------");
            redBlack.Insert(30);
            redBlack.Insert(20);
            Console.WriteLine("Testing RedBlack Deletion (2 children)" +
                              "\nItem to be deleted: 50");
            Console.WriteLine("Tree before deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours before deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());
            redBlack.Delete(50);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Deletion (red no children)" +
                              "\nItem to be deleted: 70");
            redBlack.Delete(70);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Deletion (left child)" +
                              "\nItem to be deleted: 20");
            redBlack.Delete(20);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Deletion (right child)" +
                              "\nItem to be deleted: 80");
            redBlack.Delete(80);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Deletion (black no children)" +
                              "\nItem to be deleted: 75");
            redBlack.Delete(75);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

            Console.WriteLine("\nTesting RedBlack Deletion (not in tree)" +
                              "\nItem to be deleted: 500");
            redBlack.Delete(500);
            Console.WriteLine("Tree after deletion:");
            Console.WriteLine(redBlack.DisplayTree());
            Console.WriteLine("\nTree colours after deletion:");
            Console.WriteLine(redBlack.DisplayTreeRB());

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

            Console.WriteLine("Testing RedBlack Find:");
            Console.WriteLine("Attempting to find 25: " + (redBlack.Find(25) ? "Found" : "Not Found"));
            Console.WriteLine("Attempting to find 50: " + (redBlack.Find(50) ? "Found" : "Not Found"));
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------------");
        }