public void BSTFromSortedArrayTest1()
        {
            int[] arr      = { 1, 2, 3 };
            Node  result   = _treesAndGraphs.MinimalTree(arr);
            Node  expected = CreateBinarySearchTree();

            Assert.IsTrue(CheckIsTheSameTree(result, expected));
        }
Esempio n. 2
0
        public void ListOfDepths()
        {
            int[] array  = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var   x      = TreesAndGraphs.MinimalTree(array);
            var   result = TreesAndGraphs.ListOfDepths(x);

            Assert.Inconclusive();
        }
Esempio n. 3
0
        public void MinimalTree()
        {
            int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

            var x = TreesAndGraphs.MinimalTree(array);

            Assert.Inconclusive();
        }
Esempio n. 4
0
        public void IsBalanced()
        {
            //uses minimal tree as a helper.. (good unit tests wouldn't depend on anoter)
            int[] array  = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            var   x      = TreesAndGraphs.MinimalTree(array);
            var   result = TreesAndGraphs.IsBalanced(x);

            Assert.IsTrue(result);

            TreeNode t = new TreeNode(1);

            t.Left      = new TreeNode(2);
            t.Left.Left = new TreeNode(3);

            result = TreesAndGraphs.IsBalanced(t);
            Assert.IsFalse(result);

            t.Right = new TreeNode(4);
            result  = TreesAndGraphs.IsBalanced(t);
            Assert.IsTrue(result);
        }
Esempio n. 5
0
        public void TestMinimalTree()
        {
            // Arrange

            // empty list
            List <int> emptyList = new List <int>();
            List <int> nullList  = null;

            // single val base case
            List <int> singleValList = new List <int>();

            singleValList.Add(1);

            // 2 values base case
            List <int> twoValueList = new List <int>();

            twoValueList.Add(1);
            twoValueList.Add(2);

            // common case
            List <int> commonCaseList1 = new List <int>();

            commonCaseList1.Add(1);
            commonCaseList1.Add(2);
            commonCaseList1.Add(3);
            commonCaseList1.Add(4);

            List <int> commonCaseList2 = new List <int>();

            commonCaseList2.Add(1);
            commonCaseList2.Add(2);
            commonCaseList2.Add(2);
            commonCaseList2.Add(3);
            commonCaseList2.Add(4);

            // Act
            BSTNode emptyListResult = TreesAndGraphs.MinimalTree(emptyList);
            BSTNode nullListResult  = TreesAndGraphs.MinimalTree(nullList);

            BSTNode singleValResult = TreesAndGraphs.MinimalTree(singleValList);
            BSTNode twoValueResult  = TreesAndGraphs.MinimalTree(twoValueList);

            BSTNode commonCaseList1Result = TreesAndGraphs.MinimalTree(commonCaseList1);
            BSTNode commonCaseList2Result = TreesAndGraphs.MinimalTree(commonCaseList2);

            // Assert
            Assert.IsNull(emptyListResult);
            Assert.IsNull(nullListResult);
            Assert.AreEqual(1, singleValResult.Data);
            Assert.IsNull(singleValResult.Left);
            Assert.IsNull(singleValResult.Right);
            Assert.AreEqual(2, twoValueResult.Data);
            Assert.AreEqual(1, twoValueResult.Left.Data);
            Assert.IsNull(twoValueResult.Right);
            Assert.AreEqual(3, commonCaseList1Result.Data);
            Assert.AreEqual(2, commonCaseList1Result.Left.Data);
            Assert.AreEqual(1, commonCaseList1Result.Left.Left.Data);
            Assert.IsNull(commonCaseList1Result.Left.Left.Right);
            Assert.IsNull(commonCaseList1Result.Left.Left.Left);
            Assert.IsNull(commonCaseList1Result.Left.Right);
            Assert.AreEqual(4, commonCaseList1Result.Right.Data);
            Assert.IsNull(commonCaseList1Result.Right.Left);
            Assert.IsNull(commonCaseList1Result.Right.Right);
            Assert.AreEqual(2, commonCaseList2Result.Data);
            Assert.AreEqual(2, commonCaseList2Result.Left.Data);
            Assert.AreEqual(1, commonCaseList2Result.Left.Left.Data);
            Assert.IsNull(commonCaseList2Result.Left.Right);
            Assert.IsNull(commonCaseList2Result.Left.Left.Left);
            Assert.IsNull(commonCaseList2Result.Left.Left.Right);
            Assert.AreEqual(4, commonCaseList2Result.Right.Data);
            Assert.AreEqual(3, commonCaseList2Result.Right.Left.Data);
            Assert.IsNull(commonCaseList2Result.Right.Right);
            Assert.IsNull(commonCaseList2Result.Right.Left.Left);
            Assert.IsNull(commonCaseList2Result.Right.Left.Right);
        }