public void SearchDoubleTest()
        {
            var bst = new DSWBalancedBinarySearchTree<double>();

            Assert.IsFalse(bst.Search(0.4));

            for (var i = 0; i < 20; i++)
            {
                bst.Insert(i / 2.0);
            }

            Assert.IsTrue(bst.Search(8.0));
            Assert.IsFalse(bst.Search(20.4));
            Assert.IsTrue(bst.Search(0));
        }
        public void SearchIntTest()
        {
            var bst = new DSWBalancedBinarySearchTree<int>();

            Assert.IsFalse(bst.Search(0));

            for (var i = 0; i < 20; i++)
            {
                bst.Insert(i);
            }

            Assert.IsTrue(bst.Search(19));
            Assert.IsFalse(bst.Search(20));
            Assert.IsTrue(bst.Search(0));
        }
        public void SearchStringTest()
        {
            var bst = new DSWBalancedBinarySearchTree<string>();

            Assert.IsFalse(bst.Search("A"));

            bst.Insert("A");
            bst.Insert("B");
            bst.Insert("C");
            bst.Insert("D");
            bst.Insert("E");

            Assert.IsTrue(bst.Search("D"));
            Assert.IsFalse(bst.Search("F"));
            Assert.IsTrue(bst.Search("E"));
        }
Exemplo n.º 4
0
        public static void TimingTest <T>(int amount) where T : IComparable
        {
            var path = GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\\TimingTest.csv";
            var csv  = new StringBuilder();

            csv.AppendLine("generic_list;bst_insert;balanced_insert;balanced_search;remove_balanced;");

            for (var i = 20; i < amount; i++)
            {
                var bstBalanced = new DSWBalancedBinarySearchTree <T>();
                var bst         = new BinarySearchTree <T>();
                var genericList = new T[i];
                var watch       = Stopwatch.StartNew();

                for (var j = 0; j < i; j++)
                {
                    genericList[j] = (T)Convert.ChangeType(j, typeof(T));
                }

                watch.Stop();
                var listTime = watch.Elapsed.TotalMilliseconds;
                watch = Stopwatch.StartNew();

                for (var j = 0; j < i; j++)
                {
                    bst.Insert((T)Convert.ChangeType(j, typeof(T)));
                }

                watch.Stop();
                var bstTime = watch.Elapsed.TotalMilliseconds;
                watch = Stopwatch.StartNew();

                for (var j = 0; j < i; j++)
                {
                    bstBalanced.Insert((T)Convert.ChangeType(j, typeof(T)));
                }

                watch.Stop();
                var balancedTime = watch.Elapsed.TotalMilliseconds;

                watch = Stopwatch.StartNew();
                bstBalanced.Search((T)Convert.ChangeType(0, typeof(T)));
                var search = watch.Elapsed.TotalMilliseconds;

                watch = Stopwatch.StartNew();
                bstBalanced.Remove((T)Convert.ChangeType(0, typeof(T)));
                var remove = watch.Elapsed.TotalMilliseconds;

                var newLine = $"{listTime};{bstTime};{balancedTime},{search},{remove}";
                csv.AppendLine(newLine);
            }

            File.WriteAllText(path, csv.ToString());
        }