예제 #1
0
        public void AddTest()
        {
            var bst = new BinarySearchTree<int, string>();
            bst.Add(5, "A");
            bst.Add(4, "B");
            bst.Add(6, "C");
            bst.Add(3, "D");
            bst.Add(7, "E");

            Assert.AreEqual(bst.Size, 5);
        }
예제 #2
0
        public void AllTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();
            var testData = new[]
            {
                new KeyValuePair<int, string>(5, "A"),
                new KeyValuePair<int, string>(3, "B"),
                new KeyValuePair<int, string>(6, "C"),
                new KeyValuePair<int, string>(2, "D"),
                new KeyValuePair<int, string>(7, "E"),
                new KeyValuePair<int, string>(4, "F"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var resultList = bst.All().ToList();

            //Assert
            Assert.AreEqual(resultList.Count, 6);

            Assert.AreEqual(resultList[0].Key, 2);
            Assert.AreEqual(resultList[1].Key, 3);
            Assert.AreEqual(resultList[2].Key, 4);
            Assert.AreEqual(resultList[3].Key, 5);
            Assert.AreEqual(resultList[4].Key, 6);
            Assert.AreEqual(resultList[5].Key, 7);
        }
예제 #3
0
 public void DeleteMinTest()
 {
     //Arrange
     var bst = new BinarySearchTree<int, string>();
     bst.Add(5, "A");
     bst.Add(3, "B");
     bst.Add(6, "C");
     bst.Add(2, "D");
     bst.Add(7, "E");
     bst.Add(4, "F");
     //Act
     bst.DeleteMin();
     var testResult = bst.Min();
     //Assert
     Assert.AreEqual(testResult.Key, 3);
 }
예제 #4
0
        static void BinarySearchTreePrintString()
        {
            var array = new string[]
            {
                "dog",
                "cat",
                "bird",
                "rabbit",
                "frog",
                "fox",
                "cow",
                "squirrel",
                "mouse"
            };
            var bst = new BinarySearchTree <string>(Comparer <string> .Create((x, y) => x.CompareTo(y)), null, true);

            bst.Add(array);

            Console.WriteLine($"The depth is {bst.GetTreeDepth()}");

            Console.WriteLine("Attempt to Find 9");
            Console.WriteLine(bst.Find("bird").Data);
            Hr();

            Console.WriteLine("Attempt to Remove 9");
            bst.Remove("fox");
            Hr();

            Console.WriteLine("PreOrder Traversal");
            bst.TraversePreOrder();
            Hr();

            Console.WriteLine("InOrder Traversal");
            bst.TraverseInOrder();
            Hr();

            Console.WriteLine("PostOrder Traversal");
            bst.TraversePostOrder();
            Hr();
        }
예제 #5
0
        public void DeleteTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();
            bst.Add(5, "A");
            bst.Add(3, "B");
            bst.Add(6, "C");
            bst.Add(2, "D");
            bst.Add(7, "E");
            bst.Add(4, "F");
            //Act
            bst.Delete(3);
            var testResult = bst.GetNodeByKey(5).LeftNode;

            //Assert
            Assert.AreEqual(testResult.Key, 4);
        }
예제 #6
0
 public void MaxTest()
 {
     //Arrange
     var bst = new BinarySearchTree<int, string>();
     bst.Add(5, "A");
     bst.Add(4, "B");
     bst.Add(6, "C");
     bst.Add(0, "D");
     bst.Add(7, "E");
     //Act
     var testResult = bst.Max();
     //Assert
     Assert.AreEqual(testResult.Key, 7);
 }
예제 #7
0
        public void LevelOrderTraversalTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();

            //43 10 78 84 75 65 89 34 29 54

            var testData = new[]
            {
                new KeyValuePair<int, string>(43, "43"),
                new KeyValuePair<int, string>(10, "10"),
                new KeyValuePair<int, string>(78, "78"),
                new KeyValuePair<int, string>(84, "84"),
                new KeyValuePair<int, string>(75, "75"),
                new KeyValuePair<int, string>(65, "65"),
                new KeyValuePair<int, string>(89, "89"),
                new KeyValuePair<int, string>(34, "34"),
                new KeyValuePair<int, string>(29, "29"),
                new KeyValuePair<int, string>(54, "54"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 10);

            Assert.AreEqual(result.ElementAt(0).Key, 43);
            Assert.AreEqual(result.ElementAt(1).Key, 10);
            Assert.AreEqual(result.ElementAt(2).Key, 78);
            Assert.AreEqual(result.ElementAt(3).Key, 34);
            Assert.AreEqual(result.ElementAt(4).Key, 75);
            Assert.AreEqual(result.ElementAt(5).Key, 84);
            Assert.AreEqual(result.ElementAt(6).Key, 29);
            Assert.AreEqual(result.ElementAt(7).Key, 65);
            Assert.AreEqual(result.ElementAt(8).Key, 89);
            Assert.AreEqual(result.ElementAt(9).Key, 54);
        }
예제 #8
0
        public void LevelOrderTraversalAfterRemovingTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();

            //88 48 91 26 55 11 43 65 12 57 23 62
            var testData = new[]
            {
                new KeyValuePair<int, string>(88, "88"),
                new KeyValuePair<int, string>(48, "48"),
                new KeyValuePair<int, string>(91, "91"),
                new KeyValuePair<int, string>(26, "26"),
                new KeyValuePair<int, string>(55, "55"),
                new KeyValuePair<int, string>(11, "11"),
                new KeyValuePair<int, string>(43, "43"),
                new KeyValuePair<int, string>(65, "65"),
                new KeyValuePair<int, string>(12, "12"),
                new KeyValuePair<int, string>(57, "57"),
                new KeyValuePair<int, string>(23, "23"),
                new KeyValuePair<int, string>(62, "62"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 12);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 48);
            Assert.AreEqual(result.ElementAt(2).Key, 91);
            Assert.AreEqual(result.ElementAt(3).Key, 26);
            Assert.AreEqual(result.ElementAt(4).Key, 55);
            Assert.AreEqual(result.ElementAt(5).Key, 11);
            Assert.AreEqual(result.ElementAt(6).Key, 43);
            Assert.AreEqual(result.ElementAt(7).Key, 65);
            Assert.AreEqual(result.ElementAt(8).Key, 12);
            Assert.AreEqual(result.ElementAt(9).Key, 57);
            Assert.AreEqual(result.ElementAt(10).Key, 23);
            Assert.AreEqual(result.ElementAt(11).Key, 62);

            //91 12 48
            bst.Delete(91);
            bst.Delete(12);
            bst.Delete(48);
            result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 9);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 55);
            Assert.AreEqual(result.ElementAt(2).Key, 26);
            Assert.AreEqual(result.ElementAt(3).Key, 65);
            Assert.AreEqual(result.ElementAt(4).Key, 11);
            Assert.AreEqual(result.ElementAt(5).Key, 43);
            Assert.AreEqual(result.ElementAt(6).Key, 57);
            Assert.AreEqual(result.ElementAt(7).Key, 23);
            Assert.AreEqual(result.ElementAt(8).Key, 62);
        }
예제 #9
0
        public void Start()
        {
            const int measures = 50;

            Graph.Data cb  = new Graph.Data("Array B");
            Graph.Data cl  = new Graph.Data("List L");
            Graph.Data ctr = new Graph.Data("Tree TR");
            Graph.Data ctb = new Graph.Data("Tree TB");
            Graph.Data sb  = new Graph.Data("Array B");
            Graph.Data sbb = new Graph.Data("Array B (dividing by half)");
            Graph.Data sl  = new Graph.Data("List L");
            Graph.Data str = new Graph.Data("Tree TR");
            Graph.Data stb = new Graph.Data("Tree TB");
            Graph.Data htr = new Graph.Data("Tree TR");
            Graph.Data htb = new Graph.Data("Tree TB");

            Stopwatch stopwatch = new Stopwatch();

            for (int i = 0; i < measures; i++)
            {
                int n = (i + 1) * 500;

                int[] a = GenerateArray(n);

                //Array B
                stopwatch.Start();
                int[] b = new int[n];
                for (int j = 0; j < n; j++)
                {
                    b[j] = a[j];
                }
                (new QuickSort()).Sort(b);
                stopwatch.Stop();
                cb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    Find(b, a[j]);
                }
                stopwatch.Stop();
                sb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    FindByHalf(b, a[j]);
                }
                stopwatch.Stop();
                sbb.AddPoint(n, stopwatch.ElapsedMilliseconds);

                //List L
                stopwatch.Restart();
                List <int> l = new List <int>();
                for (int j = 0; j < n; j++)
                {
                    l.Add(a[j]);
                }
                stopwatch.Stop();
                cl.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    l.IndexOf(a[j]);
                }
                stopwatch.Stop();
                sl.AddPoint(n, stopwatch.ElapsedMilliseconds);

                //Tree TR
                stopwatch.Restart();
                BinarySearchTree <int, int> tr = new BinarySearchTree <int, int>();
                for (int j = 0; j < n; j++)
                {
                    tr.Add(a[j], a[j]);
                }
                stopwatch.Stop();
                ctr.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    tr.GetElement(a[j]);
                }
                stopwatch.Stop();
                str.AddPoint(n, stopwatch.ElapsedMilliseconds);
                htr.AddPoint(n, tr.GetHeight());

                //Tree TB
                int[] temp = Median(b);
                stopwatch.Restart();
                BinarySearchTree <int, int> tb = new BinarySearchTree <int, int>();
                for (int j = 0; j < n; j++)
                {
                    tb.Add(temp[j], temp[j]);
                }
                stopwatch.Stop();
                ctb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                stopwatch.Restart();
                for (int j = 0; j < n; j++)
                {
                    tb.GetElement(a[j]);
                }
                stopwatch.Stop();
                stb.AddPoint(n, stopwatch.ElapsedMilliseconds);
                htb.AddPoint(n, tb.GetHeight());

                Console.WriteLine((i + 1) + "/" + measures + " completed.");
            }

            string path = @".\Graphs";

            Graph creationGraph = new Graph("Creation Time", "Number of elements", "Time[ms]");

            creationGraph.AddData(cb);
            creationGraph.AddData(cl);
            creationGraph.AddData(ctr);
            creationGraph.AddData(ctb);
            creationGraph.StartWithNewThread();
            creationGraph.WriteToFile(path);

            Graph searchGraph = new Graph("Search Time", "Number of elements", "Time[ms]");

            searchGraph.AddData(sb);
            searchGraph.AddData(sbb);
            searchGraph.AddData(sl);
            searchGraph.AddData(str);
            searchGraph.AddData(stb);
            searchGraph.StartWithNewThread();
            searchGraph.WriteToFile(path);

            Graph heightGraph = new Graph("Height", "Number of elements", "Height");

            heightGraph.AddData(htr);
            heightGraph.AddData(htb);
            heightGraph.StartWithNewThread();
            heightGraph.WriteToFile(path);
        }
예제 #10
0
        public static void TestBST()
        {
            var bst = new BinarySearchTree <int>(69);

            bst.Add(42);
            bst.Add(14);
            bst.Add(37);
            bst.Add(46);
            bst.Add(13);
            bst.Add(53);
            bst.Add(78);
            bst.Add(1);
            bst.Add(51);
            bst.Add(5);
            bst.Add(18);
            bst.Add(8);

            bst.ToList().ForEach(Console.WriteLine);
        }