コード例 #1
0
        public void ShouldDepthSearchSuccessfully()
        {
            var result = BinaryTreeUtil.DepthSearch(CreateValidBinaryTree(), 4);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.NodeValue == 4);
        }
コード例 #2
0
        public void ShouldFirstBreathSearchSuccessfully()
        {
            var result = BinaryTreeUtil.BreathFirstSearch(CreateInValidBinaryTree(), 7);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.NodeValue == 7);
        }
コード例 #3
0
        public void WhenValidatingAInValidBinaryTreeShouldReturnFalse()
        {
            var isBinaryTree = BinaryTreeUtil
                               .IsBinaryTree(CreateInValidBinaryTree(), int.MinValue, int.MaxValue);

            Assert.IsFalse(isBinaryTree);
        }
コード例 #4
0
        public void Test_PrintRootToLeafPathWithGivenSum_NoPath()
        {
            BinaryTreeUtil binaryTreeUtil = new BinaryTreeUtil();

            DataStructures.MyGenLinkedList <int> o = null;
            var hasPath = binaryTreeUtil.PrintRootToLeafPathWithGivenSum(root, 598, o);

            //Assert.IsTrue(hasPath, "314, 6, 271, 0");
        }
コード例 #5
0
        public void ShouldPerformALimitedDepthSearchSuccessfully()
        {
            int maxDepth       = 3;
            var binaryTreeUtil = new BinaryTreeUtil();
            var result         = binaryTreeUtil.MaxDepthSearch(CreateValidBinaryTree(), 4, maxDepth, 0);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.NodeValue == 4);
        }
コード例 #6
0
        public void ShouldOnlySearch1LevelDeepAndReturnNull()
        {
            int maxDepth       = 2;
            int currentDepth   = 0;
            var binaryTreeUtil = new BinaryTreeUtil();
            var tree           = CreateValidBinaryTree();

            var result = binaryTreeUtil.MaxDepthSearch(tree, 4, maxDepth, currentDepth);

            Assert.IsNull(result);
        }
コード例 #7
0
        public void ShouldGenerateInOrderBinaryTreeSucessfully()
        {
            int[] keys = new int[] { 1, 2, 3, 4, 5, 6, 7 };

            Node result = BinaryTreeUtil.CreateInOrderBinaryTree(keys);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.NodeValue == 4);
            Assert.IsTrue(result.Left.NodeValue == 2);
            Assert.IsTrue(result.Right.NodeValue == 6);
        }
コード例 #8
0
        public void ShouldMapAllNodesByPerformFullTreeTraversal()
        {
            var tree = CreateValidBinaryTree();

            var  nodeMap = BinaryTreeUtil.MapAllNodes(tree, n => new Node[] { n.Left, n.Right });
            Node nodeId3 = nodeMap.FirstOrDefault(n => n.NodeValue == 3);

            Assert.IsTrue(nodeMap.Count > 0);
            Assert.IsNotNull(nodeId3);
            Assert.IsTrue(nodeId3.NodeValue == 3);
            Assert.IsTrue(nodeId3.Left.NodeValue == 1);
            Assert.IsTrue(nodeId3.Right.NodeValue == 4);
        }
コード例 #9
0
        private static Node CreateValidBinaryTree()
        {
            /*
             *           6
             *          / \
             *        3     8
             *       / \   / \
             *      1   4 7   9
             */
            int[] keys = new int[] { 1, 3, 4, 6, 7, 8, 9, 10 };

            return(BinaryTreeUtil.CreateInOrderBinaryTree(keys));
        }
コード例 #10
0
        public void ShouldGetParentIds()
        {
            var tree         = CreateValidBinaryTree();
            var parentLookUp = new Dictionary <int, List <int> >();

            var nodeMap = BinaryTreeUtil.MapAllNodes(
                tree,
                n => new Node[] { n.Left, n.Right },
                BinaryTreeUtil.MapParentNodes(parentLookUp)
                );
            Node       nodeId3   = nodeMap.FirstOrDefault(n => n.NodeValue == 3);
            List <int> parentIds = parentLookUp[nodeId3.NodeValue];

            Assert.IsTrue(parentLookUp.Count > 0);
            Assert.IsNotNull(nodeId3);
            Assert.IsTrue(parentIds.Count == 1);
        }