Ejemplo n.º 1
0
        public void TestBSTCopyTo()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            int[] actual = new int[tree.Count];
            int[] expected = new int[] { 40, 11, 10, 34, 16, 62, 43, 63 };

            int[] actual2 = new int[tree.Count + 2];
            int[] expected2 = new int[] { 0, 0, 40, 11, 10, 34, 16, 62, 43, 63 };

            int[] actual3 = new int[tree.Count];
            int[] expected3 = new int[] { 0, 0, 40, 11, 10, 34, 16, 62 };

            tree.CopyTo(actual);
            tree.CopyTo(actual2, 2);
            tree.CopyTo(actual3, 2, 6);

            for (int i = 0; i < expected.Length; i++)
                Assert.AreEqual(expected[i], actual[i], "\nFailed at [" + i + "]");

            for (int i = 0; i < expected2.Length; i++)
                Assert.AreEqual(expected2[i], actual2[i], "\nFailed at [" + i + "]");

            for (int i = 0; i < expected3.Length; i++)
                Assert.AreEqual(expected3[i], actual3[i], "\nFailed at [" + i + "]");
        }
Ejemplo n.º 2
0
 public void TestContains()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     Assert.IsFalse(tree.Contains('c'));
     tree.Add('c');
     Assert.IsTrue(tree.Contains('c'));
     Assert.IsFalse(tree.Contains('b'));
 }
Ejemplo n.º 3
0
        public void TestBSTClear()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            tree.Clear();

            Assert.AreEqual(0, tree.Count, "\nTree counts no nodes.");
        }
Ejemplo n.º 4
0
        public void TestBSTRemove()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.IsTrue(tree.Contains(43), "\nChecking if 43 is there.");
            Assert.IsTrue(tree.Remove(43), "\nRemoving 43.");
            Assert.IsFalse(tree.Contains(43), "\nChecking if 43 is still there.");
        }
Ejemplo n.º 5
0
        public void TestBSTLowest()
        {
            var inttree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };
            var stringtree = new BinarySearchTree<string>() { "hello", "test", "Case", "scenario" };
            var doubletree = new BinarySearchTree<double>() { 74.24, 75.1, 1.75, 64.1, 2.535, 63.2, 1.4 };

            Assert.AreEqual(10, inttree.Lowest(), "\nLowest int.");
            Assert.AreEqual("Case", stringtree.Lowest(), "\nLowest string.");
            Assert.AreEqual(1.4, doubletree.Lowest(), "\nLowest double.");
        }
Ejemplo n.º 6
0
 public void TestAdd()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for(int i = 0; i < 50; i++)  {
         tree.Add(i);
     }
     for(int i = 0; i < 50; i++)  {
         Assert.IsTrue(tree.Contains(i));
     }
 }
Ejemplo n.º 7
0
 public void TestClear()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //count is 0 after clear
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     tree.Clear();
     Assert.IsTrue(tree.Count == 0);
 }
Ejemplo n.º 8
0
        public void InOrderTraversalOfTreeTest()
        {
            BinarySearchTree<int> bst = ConstructTree(4,2,5,1,3);
            bst.InOrderTraversalPrint(bst.RootNode);
            Console.WriteLine();

            bst = ConstructTree(5, 4, 8, 1, 12, 6, 20, 10);
            bst.InOrderTraversalPrint(bst.RootNode);
            Console.WriteLine();

            bst = ConstructTree(5);
            bst.InOrderTraversalPrint(bst.RootNode);
            Console.WriteLine();

            bst = new BinarySearchTree<int>();
            bst.InOrderTraversalPrint(bst.RootNode);
            Console.WriteLine();
        }
Ejemplo n.º 9
0
 public void TestCopyTo()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     int[] array = new int[0];
     tree.CopyTo(array, 0);
     Assert.IsTrue(array.Length == 0);
     //basic copy
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     array = new int[10];
     int startIndex = 0;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy at later position in middle
     array = new int[25];
     startIndex = 10;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
     //copy to final positions of array.
     array = new int[25];
     startIndex = 15;
     tree.CopyTo(array, startIndex);
     foreach (int curr in tree)
     {
         Assert.IsTrue(curr == array[startIndex]);
         startIndex++;
     }
 }
Ejemplo n.º 10
0
        public void TestBSTContain()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.IsTrue(tree.Contains(62), "\nFinding 43 in the tree.");
        }
Ejemplo n.º 11
0
 public void TestCopyToNullArray()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     int[] array  = null;
     tree.CopyTo(array, 0);
 }
Ejemplo n.º 12
0
 public void TestCount()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     Assert.IsTrue(tree.Count == 0);
     tree.Add(10);
     Assert.IsTrue(tree.Count == 1);
     for (int i = 100; i < 200; i++)
     {
         tree.Add(i);
     }
     Assert.IsTrue(tree.Count == 101);
     tree.Remove(10);
     Assert.IsTrue(tree.Count == 100);
     for (int i = 100; i < 200; i++)
     {
         tree.Remove(i);
     }
     Assert.IsTrue(tree.Count == 0);
     //count isnt decremented if unsuccessfully try to remove an item.
     tree.Remove(10);
     Assert.IsTrue(tree.Count == 0);
 }
Ejemplo n.º 13
0
 public void TestCopyToSmallArray()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[5];
     //try copying to array that is too small
     try
     {
         tree.CopyTo(array, 0);
         Assert.Fail("Should fail, array too small");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
     //try copying too close to the end of the array.
     array = new int[25];
     try
     {
         tree.CopyTo(array, 20);
         Assert.Fail("Should fail, Not enough room to copy");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentException);
     }
 }
Ejemplo n.º 14
0
        public void TestBSTBalance()
        {
            // The unbalanced int tree
            //      0
            //       \
            //        1
            //         \
            //          2
            //           ...
            //            \
            //             16

            // The balanced int tree
            //                 8
            //             /      \
            //           4         13
            //         /  \       /  \
            //       2     6     11   15
            //      / \   / \   / \   / \
            //     1   3 5   7 10 12 14 16
            //    /           /
            //   0           9
            var tree = new BinarySearchTree<int>() 
            {   
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16    
            };

            int[] expected = new int[]
            {
                8, 4, 2, 1, 0, 3, 6, 5, 7, 13, 11, 10, 9, 12, 15, 14, 16
            };

            tree.Balance();

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], tree[i], "Checkin {0} place of tree.", i);
            }
        }
Ejemplo n.º 15
0
 public void TestBSTAdd()
 {
     var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };
     tree.Add(3);
     Assert.AreEqual(9, tree.Count, "\nCounting the elements");
 }
Ejemplo n.º 16
0
 public void TestCopyToInvalidIndex()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     for (int i = 0; i < 10; i++)
     {
         tree.Add(i);
     }
     int[] array = new int[10];
     //Try to set start index outside of max size of array.
     try
     {
         tree.CopyTo(array, 11);
         Assert.Fail("Should fail, index is larger than size of array.");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
     //try to copy to start index below 0
     try
     {
         tree.CopyTo(array, -2);
         Assert.Fail("Should fail, index is below 0");
     }
     catch (Exception e)
     {
         Assert.IsTrue(e is ArgumentOutOfRangeException);
     }
 }
Ejemplo n.º 17
0
        public void TestBSTSort()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };
            int[] expected = new int[]{ 10, 11, 16, 34, 40, 43, 62, 63 };

            var sorted = tree.Sort();

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], sorted[i], "Checkin {0} place of tree.", i);
            }
        }
Ejemplo n.º 18
0
 private static BinarySearchTree<string> ConstructTree(params string[] values)
 {
     string[] numbers = values.Select(x => x.ToString()).ToArray();
     BinarySearchTree<string> bst = new BinarySearchTree<string>();
     foreach (string currentNumber in numbers)
     {
         bst.Insert(currentNumber);
     }
     return bst;
 }
Ejemplo n.º 19
0
        public void MaxDepthTest()
        {
            BinarySearchTree<int> bst = ConstructTree(5, 4, 8);
            Assert.AreEqual(2, bst.MaxDepth(bst.RootNode));

            bst = ConstructTree(5);
            Assert.AreEqual(1, bst.MaxDepth(bst.RootNode));

            bst = new BinarySearchTree<int>();
            Assert.AreEqual(0, bst.MaxDepth(bst.RootNode));
        }
Ejemplo n.º 20
0
        public void SizeTest()
        {
            BinarySearchTree<int> bst = ConstructTree(5,4,8);
            Assert.AreEqual(3, bst.Size(bst.RootNode));

            bst = ConstructTree(5);
            Assert.AreEqual(1, bst.Size(bst.RootNode));

            bst = new BinarySearchTree<int>();
            Assert.AreEqual(0, bst.Size(bst.RootNode));
        }
Ejemplo n.º 21
0
 public void TestIterator()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //empty tree should give no iterations.
     foreach (int curr in tree)
     {
         Assert.Fail("Should be empty.");
     }
     for(int i = 0; i < 100; i++)  {
         tree.Add(i);
     }
     int element = 0;
     foreach(int curr in tree) {
         Assert.AreEqual(element, curr);
         element++;
     }
     //remove root element, an item with 1 child in the middle and a leaf with no children at bottom. Recheck everything iterates in order.
     tree.Remove(0);
     tree.Remove(50);
     tree.Remove(100);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         if (element == 49)
             element += 2;
         else
             element++;
     }
     //re add 50 so that 51 now has child on left of 50 and child right of 52.
     tree.Add(50);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         element++;
     }
     //remove node with 2 children, check iterates correctly still.
     tree.Remove(51);
     element = 1;
     foreach (int curr in tree)
     {
         Assert.AreEqual(element, curr);
         if (element == 50)
             element += 2;
         else
             element++;
     }
 }
Ejemplo n.º 22
0
        public void testMassAddRemove()
        {
            BinarySearchTree<int> tree = new BinarySearchTree<int>();
            List<int> list = new List<int>();
            Random r = new Random();
            int numberToAdd = r.Next(1000000);
            for (int i = 0; i < 1000; i++)
            {
                while (list.Contains(numberToAdd))
                {
                    numberToAdd = r.Next(1000000);
                }
                list.Add(numberToAdd);
                tree.Add(numberToAdd);
            }
            Assert.IsTrue(list.Count == tree.Count && list.Count == 1000);
            list.Sort();
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));
            int curr;
            for (int i = 0; i < list.Count; i++)
            {
                curr = list.ElementAt(0);
                Assert.IsTrue(list.Remove(curr));
                Assert.IsTrue(tree.Remove(curr));
            }
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));

            numberToAdd = r.Next(1000000);
            for (int i = 0; i < 1000; i++)
            {
                while (list.Contains(numberToAdd))
                {
                    numberToAdd = r.Next(1000000);
                }
                list.Add(numberToAdd);
                tree.Add(numberToAdd);
                if (i % 2 == 0)
                {
                    curr = list.ElementAt(r.Next(list.Count));
                    Assert.IsTrue(list.Remove(curr));
                    Assert.IsTrue(tree.Remove(curr));
                }
            }
            list.Sort();
            Assert.IsTrue(Enumerable.SequenceEqual(list, tree));
        }
Ejemplo n.º 23
0
 public void TestRemove()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     //fails to remove item from empty list.
     Assert.IsFalse(tree.Remove(10));
     tree.Add(10);
     tree.Add(12);
     Assert.IsTrue(tree.Remove(10));
     Assert.IsFalse(tree.Contains(10));
     Assert.IsFalse(tree.Remove(10));
 }
Ejemplo n.º 24
0
 public void TestAddDuplicate()
 {
     BinarySearchTree<int> tree = new BinarySearchTree<int>();
     tree.Add(10);
     tree.Add(10);
 }
Ejemplo n.º 25
0
 private static BinarySearchTree<int> ConstructTree()
 {
     string fileName = "BinarySearchTreeBasic.txt";
     string[] numbers = ExtractContent(fileName);
     BinarySearchTree<int> bst = new BinarySearchTree<int>();
     foreach (string currentNumber in numbers)
     {
         bst.Insert(int.Parse(currentNumber));
     }
     return bst;
 }
Ejemplo n.º 26
0
        public void TestBSTIndexOf()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.AreEqual(0, tree.IndexOf(tree.Root), "Checking that root is in the 0. place.");
            Assert.AreEqual(4, tree.IndexOf(16), "Checking 16 is in the 4. place.");
        }
Ejemplo n.º 27
0
        public void TestBSTRemoveAt()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };

            Assert.AreEqual(34, tree[3], "Checking that 34 is on the 3. place in the array.");
            tree.RemoveAt(3);
            Assert.IsFalse(tree.Contains(34), "Checking if 34 is in the tree.");
        }
Ejemplo n.º 28
0
        public void TestBSTEnumerator()
        {
            var tree = new BinarySearchTree<int>() { 40, 11, 62, 43, 34, 16, 10, 63 };
            int[] expected = new int[] { 40, 11, 10, 34, 16, 62, 43, 63 };

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], tree[i], "Checkin {0} place of tree.", i);
            }
        }
Ejemplo n.º 29
0
        public void MaxValueInTreeTest()
        {
            BinarySearchTree<int> bst = ConstructTree(5, 4, 8);
            Assert.AreEqual(8, bst.MaxValue(bst.RootNode));

            bst = ConstructTree(5, 4, 8,1,12,6,20,10);
            Assert.AreEqual(20, bst.MaxValue(bst.RootNode));

            bst = ConstructTree(5);
            Assert.AreEqual(5, bst.MaxValue(bst.RootNode));

            bst = new BinarySearchTree<int>();
            Assert.AreEqual(0, bst.MaxValue(bst.RootNode));
        }
        public static void TreeTest()
        {
            Node node1 = new Node(30);
            Node node2 = new Node(35);
            Node node3 = new Node(40);
            Node node4 = new Node(45);
            Node node5 = new Node(50);
            Node node6 = new Node(55);
            Node node7 = new Node(60);

            BinaryTree bt1 = new BinaryTree(node1);

            bt1.Root.LeftChild             = node2;
            bt1.Root.LeftChild.LeftChild   = node3;
            bt1.Root.LeftChild.RightChild  = node4;
            bt1.Root.RightChild            = node5;
            bt1.Root.RightChild.LeftChild  = node6;
            bt1.Root.RightChild.RightChild = node7;



            List <int> preOrder = bt1.PreOrder(bt1.Root);

            Console.WriteLine("Pre-order traversal:");

            foreach (int item in preOrder)
            {
                Console.WriteLine($"{item}");
            }

            bt1.values.Clear();

            List <int> inOrder = bt1.InOrder(bt1.Root);

            Console.WriteLine("In-order traversal:");

            foreach (int item in inOrder)
            {
                Console.WriteLine($"{item}");
            }

            bt1.values.Clear();

            List <int> postOrder = bt1.PostOrder(bt1.Root);

            Console.WriteLine("Post-order traversal:");

            foreach (int item in postOrder)
            {
                Console.WriteLine($"{item}");
            }

            bt1.values.Clear();

            BinarySearchTree bts = new BinarySearchTree(node1);

            Console.WriteLine("Add node2 (35) to binary search tree:");

            bts.AddNode(node1, node2);
            Console.WriteLine($"Tree values: {bts.Root.Value}, {bts.Root.LeftChild.Value}");
        }