public void IsCorrectlyDoneInLevelOrderTestCase01()
        {
            var binaryTree        = BinaryTreeManager.Create(new char?[0]);
            var expectedTraversal = new char?[0];

            TestImplementations(binaryTree.Root, expectedTraversal);
        }
示例#2
0
        public void MaximumDepth_BT_with_null()
        {
            TreeNode          root = null;
            BinaryTreeManager binaryTreeManager = new BinaryTreeManager();

            Assert.AreEqual(0, binaryTreeManager.MaxDepth(root));
        }
        public void CorrectlyCountsGoodNodesTestCase01()
        {
            const int expectedResult = 4;
            var       binaryTree     = BinaryTreeManager.Create(new int?[] { 5, 3, 10, 20, 21, 1, null });

            TestImplementations(binaryTree.Root, expectedResult);
        }
        public void IsCorrectlyDoneInLevelOrderTestCase10()
        {
            var expectedTraversal = new char?[] { 'D', 'B', 'F', 'A' };

            //           D
            //          / \
            //         /   \
            //        /     \
            //       B       F
            //      /
            //     A
            var binaryTree1 = BinaryTreeManager.Create(new char?[] { 'D', 'B', 'F', 'A' });

            TestImplementations(binaryTree1.Root, expectedTraversal);

            var binaryTree2 = BinaryTreeManager.Create(new char?[] { 'D', 'B', 'F', 'A', null });

            TestImplementations(binaryTree2.Root, expectedTraversal);

            var binaryTree3 = BinaryTreeManager.Create(new char?[] { 'D', 'B', 'F', 'A', null, null });

            TestImplementations(binaryTree3.Root, expectedTraversal);

            var binaryTree4 = BinaryTreeManager.Create(new char?[] { 'D', 'B', 'F', 'A', null, null, null });

            TestImplementations(binaryTree4.Root, expectedTraversal);
        }
        public void CorrectlyCountsGoodNodesTestCase02()
        {
            const int expectedResult = 2;
            var       binaryTree     = BinaryTreeManager.Create(new int?[] { 8, 2, 6, 8, 7 });

            TestImplementations(binaryTree.Root, expectedResult);
        }
        public void ReturnsNullTestCase01()
        {
            //           2
            var binarySearchTree = BinaryTreeManager.Create(new int?[] { 2 });

            TestImplementations(binarySearchTree.Root, null);
        }
示例#7
0
        public BinaryTreeMaximumElementsByLevelSearch()
        {
            TypeToTest = typeof(FindBinaryTreeMaximumElementsByLevel);

            //           1
            //          / \
            //         /   \
            //        3     2
            //       / \     \
            //      5   3     9
            _binaryTree1 = BinaryTreeManager.Create(new int?[] { 1, 3, 2, 5, 3, null, 9 });

            //           3
            //          /
            //         4
            //          \
            //           5
            //          /
            //         2
            //          \
            //           1
            _binaryTree2 = BinaryTreeManager.Create(new int?[] { 3, 4, null, null, 5, 2, null, null, 1 });

            //           -9
            //           / \
            //          /   \
            //         /     \
            //       -6     -70
            //       / \     / \
            //     -15 -6  -2  -8
            //             /
            //           -3
            _binaryTree3 = BinaryTreeManager.Create(new int?[] { -9, -6, -70, -15, -6, -2, -8, -3, null });
        }
        public void PreOrderTraversalTest()
        {
            /*
             *                  10
             *          20               30
             *      40      50      60      70
             */

            var root = new BinaryTreeNode <int>(10)
            {
                Left  = new BinaryTreeNode <int>(20),
                Right = new BinaryTreeNode <int>(30)
            };

            root.Left.Left  = new BinaryTreeNode <int>(40);
            root.Left.Right = new BinaryTreeNode <int>(50);

            root.Right.Left  = new BinaryTreeNode <int>(60);
            root.Right.Right = new BinaryTreeNode <int>(70);

            var        treeManager = new BinaryTreeManager <int>();
            List <int> results     = treeManager.PreOrderTraversal(root);


            Assert.AreEqual(root.Data, results[0]);
            Assert.AreEqual(root.Left.Data, results[1]);
            Assert.AreEqual(root.Left.Left.Data, results[2]);
            Assert.AreEqual(root.Left.Right.Data, results[3]);
            Assert.AreEqual(root.Right.Data, results[4]);
            Assert.AreEqual(root.Right.Left.Data, results[5]);
            Assert.AreEqual(root.Right.Right.Data, results[6]);
        }
示例#9
0
        public void oneTreeNode()
        {
            int[] dataArray = new int[] { 5 };

            BinaryTreeManager btm      = new BinaryTreeManager();
            TreeNode          rootNode = btm.ConstructMaximumBinaryTree(dataArray);

            Assert.AreEqual(5, rootNode.val);
        }
示例#10
0
        public void ReturnsClosestValueTestCase01()
        {
            //           9
            var       binarySearchTree = BinaryTreeManager.Create(new int?[] { 9 });
            const int target           = 100;
            const int expectedResult   = 9;

            TestImplementations(binarySearchTree.Root, target, expectedResult);
        }
示例#11
0
        public void findMaxIntegerIndex()
        {
            int[] mainTree = new int[] { 3, 2, 1, 6, 0, 5 };

            BinaryTreeManager btm = new BinaryTreeManager();

            int maxIndex = btm.getIndexOfMaxValue(mainTree);

            Assert.AreEqual(3, maxIndex);
        }
示例#12
0
        public void ReturnsFirstSmallestElementOfASingleNodeTree()
        {
            //           2
            var binarySearchTree = BinaryTreeManager.Create(new int?[] { 2 });

            const int k = 1;
            const int expectedResult = 2;

            TestImplementations(binarySearchTree.Root, k, expectedResult);
        }
示例#13
0
        public void MaximumDepth_BT_depth_1()
        {
            TreeNode root = new TreeNode(3);

            root.left  = null;
            root.right = null;

            BinaryTreeManager binaryTreeManager = new BinaryTreeManager();

            Assert.AreEqual(1, binaryTreeManager.MaxDepth(root));
        }
        public void ReturnsSecondMaximumElementTestCase02()
        {
            //           2
            //          / \
            //         /   \
            //       -2     5
            var binarySearchTree = BinaryTreeManager.Create(new int?[] { 2, -2, 5 });

            const int expectedResult = 2;

            TestImplementations(binarySearchTree.Root, expectedResult);
        }
示例#15
0
        private void TestImplementations(IReadOnlyCollection <int?> nodesData, IReadOnlyCollection <int?> expectedResults)
        {
            foreach (var implementation in ImplementationsToTest())
            {
                var binaryTree       = BinaryTreeManager.Create(nodesData);
                var nodeActualResult = (BinaryNode <int>)implementation.Invoke(null, new object[] { binaryTree.Root });
                var actualResults    = new List <int?>();
                TraverseBinaryTreeInBreadthFirstSearchWay <int> .Visit = node => actualResults.Add(node?.Data);
                TraverseBinaryTreeInBreadthFirstSearchWay <int> .IterativeImplementation(nodeActualResult);

                actualResults.TrimTrailingNulls().ShouldBe(expectedResults);
            }
        }
        public void ReturnsSecondMaximumElementTestCase06()
        {
            //           25
            //             \
            //             50
            //            /
            //           37
            var binarySearchTree = BinaryTreeManager.Create(new int?[]
                                                            { 25, null, 50, 37, null });

            const int expectedResult = 37;

            TestImplementations(binarySearchTree.Root, expectedResult);
        }
        public void IsCorrectlyDoneInLevelOrderTestCase06()
        {
            //           D
            //          / \
            //         /   \
            //        /     \
            //       B       F
            //              / \
            //             E   G
            var binaryTree        = BinaryTreeManager.Create(new char?[] { 'D', 'B', 'F', null, null, 'E', 'G' });
            var expectedTraversal = new char?[] { 'D', 'B', 'F', null, null, 'E', 'G' };

            TestImplementations(binaryTree.Root, expectedTraversal);
        }
示例#18
0
        public void ReturnsClosestValueTestCase05()
        {
            //           10
            //          /  \
            //         /    \
            //        5     15
            //       / \   /  \
            //      2   5 13  22
            //     /        \
            //    1         14
            var       binarySearchTree = BinaryTreeManager.Create(new int?[] { 10, 5, 15, 2, 5, 13, 22, 1, null, null, 14 });
            const int target           = 12;
            const int expectedResult   = 13;

            TestImplementations(binarySearchTree.Root, target, expectedResult);
        }
        public BinaryTreeDepthFirstSearchTraversal()
        {
            TypeToTest = typeof(TraverseBinaryTreeInDepthFirstSearchWay <char>);

            //           F
            //          / \
            //         /   \
            //        /     \
            //       B       G
            //      / \       \
            //     A   D       I
            //        / \     /
            //       C   E   H
            _binaryTree = BinaryTreeManager.Create(new char?[]
                                                   { 'F', 'B', 'G', 'A', 'D', null, 'I', null, null, 'C', 'E', 'H', null });
        }
        public void ReturnsSecondMaximumElementTestCase04()
        {
            //           50
            //          /
            //         25
            //        /
            //       12
            //         \
            //          24
            var binarySearchTree = BinaryTreeManager.Create(new int?[]
                                                            { 50, 25, null, 12, null, null, 24 });

            const int expectedResult = 25;

            TestImplementations(binarySearchTree.Root, expectedResult);
        }
示例#21
0
        public void ReturnsClosestValueTestCase04()
        {
            //           9
            //          / \
            //         /   \
            //        4     17
            //       / \     \
            //      3   6     22
            //         / \   /
            //        5   7 20
            var binarySearchTree =
                BinaryTreeManager.Create(new int?[] { 9, 4, 17, 3, 6, null, 22, null, null, 5, 7, 20 });
            const int target         = 12;
            const int expectedResult = 9;

            TestImplementations(binarySearchTree.Root, target, expectedResult);
        }
        public void IsCorrectlyDoneInLevelOrderTestCase03()
        {
            //           F
            //          / \
            //         /   \
            //        /     \
            //       B       G
            //      / \       \
            //     A   D       I
            //        / \     /
            //       C   E   H
            var binaryTree = BinaryTreeManager.Create(new char?[]
                                                      { 'F', 'B', 'G', 'A', 'D', null, 'I', null, null, 'C', 'E', 'H', null });
            var expectedTraversal = new char?[] { 'F', 'B', 'G', 'A', 'D', null, 'I', null, null, 'C', 'E', 'H' };

            TestImplementations(binaryTree.Root, expectedTraversal);
        }
示例#23
0
        public BinarySearchTreeKthSmallestElementSearch()
        {
            TypeToTest = typeof(FindBinarySearchTreeKthSmallestElement);

            //           50
            //          /  \
            //         /    \
            //        /      \
            //       25      100
            //      /          \
            //     12          200
            //      \         /
            //      18      150
            //     /  \     /  \
            //    15  21  125  175
            _bigTree = BinaryTreeManager.Create(new int?[]
                                                { 50, 25, 100, 12, null, null, 200, null, 18, 150, null, 15, 21, 125, 175 });
        }
        public void ReturnsSecondMaximumElementTestCase05()
        {
            //           50
            //          /
            //         25
            //        /  \
            //       12  37
            //          /  \
            //         27  49
            //        /  \
            //       26  36
            var binarySearchTree = BinaryTreeManager.Create(new int?[]
                                                            { 50, 25, null, 12, 37, null, null, 27, 49, 26, 36 });

            const int expectedResult = 49;

            TestImplementations(binarySearchTree.Root, expectedResult);
        }
        public void ReturnsSecondMaximumElementTestCase03()
        {
            //           5
            //          / \
            //         /   \
            //        3     8
            //       / \   / \
            //      1   4 7   12
            //               /
            //              10
            //             /  \
            //            9   11
            var binarySearchTree = BinaryTreeManager.Create(new int?[]
                                                            { 5, 3, 8, 1, 4, 7, 12, null, null, null, null, null, null, 10, null, 9, 11 });

            const int expectedResult = 11;

            TestImplementations(binarySearchTree.Root, expectedResult);
        }
示例#26
0
        public void MaximumDepth_BT_leetcode_failed_1()
        {
            TreeNode root = new TreeNode(1);

            root.left             = new TreeNode(2);
            root.right            = new TreeNode(3);
            root.left.left        = new TreeNode(4);
            root.left.right       = new TreeNode(5);
            root.right.left       = null;
            root.right.right      = null;
            root.left.left.left   = null;
            root.left.left.right  = null;
            root.left.right.left  = null;
            root.left.right.right = null;

            BinaryTreeManager binaryTreeManager = new BinaryTreeManager();

            Assert.AreEqual(3, binaryTreeManager.MaxDepth(root));
        }
        public BinaryTreeMaximumElementSearch()
        {
            TypeToTest = typeof(FindBinaryTreeMaximumElement);

            //           5
            //          / \
            //         /   \
            //        4     7
            //         \     \
            //         18     8
            _binaryTree1 = BinaryTreeManager.Create(new int?[] { 5, 4, 7, null, 18, null, 8 });

            //           -9
            //           / \
            //          /   \
            //         /     \
            //       -6     -70
            //       / \     / \
            //     -15 -6  -2  -8
            //             /
            //           -3
            _binaryTree2 = BinaryTreeManager.Create(new int?[] { -9, -6, -70, -15, -6, -2, -8, -3, null });
        }
示例#28
0
 public void makeInstance()
 {
     BinaryTreeManager binaryTreeManager = new BinaryTreeManager();
 }
示例#29
0
 public void MakeInstance()
 {
     BinaryTreeManager btm = new BinaryTreeManager();
 }