Exemple #1
0
        public void TestCheckSubtree()
        {
            // Arrange
            BTNode <int> root1 = new BTNode <int>(8);

            root1.Right                  = new BTNode <int>(9);
            root1.Left                   = new BTNode <int>(2);
            root1.Left.Left              = new BTNode <int>(1);
            root1.Left.Right             = new BTNode <int>(4);
            root1.Left.Right.Left        = new BTNode <int>(3);
            root1.Left.Right.Right       = new BTNode <int>(6);
            root1.Left.Right.Right.Right = new BTNode <int>(7);

            BTNode <int> root2 = new BTNode <int>(2);

            root2.Left        = new BTNode <int>(1);
            root2.Right       = new BTNode <int>(4);
            root2.Right.Left  = new BTNode <int>(3);
            root2.Right.Right = new BTNode <int>(6);

            // Act
            bool isSubtree = TreesAndGraphs.CheckSubtree(root1.Left, root2);

            // Assert
            Assert.IsTrue(isSubtree);
        }
Exemple #2
0
        public void CheckSubtree()
        {
            TreeNode n = new TreeNode(4)
            {
                Left = new TreeNode(1)
                {
                    Left  = new TreeNode(4),
                    Right = new TreeNode(11)
                },
                Right = new TreeNode(5)
                {
                    Left  = new TreeNode(444),
                    Right = new TreeNode(115)
                    {
                        Right = new TreeNode(33)
                        {
                            Right = new TreeNode(22)
                        }
                    }
                }
            };

            var subtree = new TreeNode(5)
            {
                Left  = new TreeNode(444),
                Right = new TreeNode(115)
                {
                    Right = new TreeNode(33)
                    {
                        Right = new TreeNode(22)
                    }
                }
            };

            var result = TreesAndGraphs.CheckSubtree(n, subtree);

            Assert.IsTrue(result);

            n = new TreeNode(4)
            {
                Left = new TreeNode(1)
                {
                    Left = new TreeNode(4)
                    {
                        Left = new TreeNode(1)
                    },
                }
            };

            subtree = new TreeNode(4)
            {
                Left = new TreeNode(1)
            };

            result = TreesAndGraphs.CheckSubtree(n, subtree);
            Assert.IsTrue(result);

            n.Left.Value = 2;
            result       = TreesAndGraphs.CheckSubtree(n, subtree);
            Assert.IsTrue(result);

            n.Left.Left.Left.Value = 2;
            result = TreesAndGraphs.CheckSubtree(n, subtree);
            Assert.IsFalse(result);
        }