Ejemplo n.º 1
0
        private void RunTest(MethodDelegate function, bool testExtra)
        {
            var root = BinaryTreeHelper.BuildTree(new int[] { 5, 4, 7, 8, 9, 6, 3, 2, 1 });

            var n1 = root.LeftChild.LeftChild.LeftChild;
            var n2 = root.LeftChild.RightChild;

            var expected = root.LeftChild;
            BinaryTreeNode <int> foundNode;
            var result = function(root, n1, n2, out foundNode);

            Assert.IsTrue(result);
            Assert.AreEqual(expected, foundNode);

            n1       = root.LeftChild.LeftChild.LeftChild;
            n2       = root.RightChild.RightChild;
            expected = root;
            result   = function(root, n1, n2, out foundNode);
            Assert.IsTrue(result);
            Assert.AreEqual(expected, foundNode);

            n1       = root.LeftChild.LeftChild.LeftChild;
            n2       = root.RightChild.LeftChild;
            expected = root;
            result   = function(root, n1, n2, out foundNode);
            Assert.IsTrue(result);
            Assert.AreEqual(expected, foundNode);

            result = function(null, n1, n2, out foundNode);
            Assert.IsFalse(result);

            result = function(root, null, n2, out foundNode);
            Assert.IsFalse(result);

            result = function(root, n1, null, out foundNode);
            Assert.IsFalse(result);

            result = function(root, n1, n1, out foundNode);
            Assert.IsFalse(result);

            if (testExtra)
            {
                n1 = root.LeftChild;
                n2 = root.LeftChild.LeftChild.RightChild;

                expected = root.LeftChild;
                result   = function(root, n1, n2, out foundNode);
                Assert.IsTrue(result);
                Assert.AreEqual(expected, foundNode);
            }

            if (testExtra)
            {
                n1     = root.LeftChild;
                n2     = new BinaryTreeNode <int>(5);
                result = function(root, n1, n2, out foundNode);
                Assert.IsTrue(result);
                Assert.IsNull(foundNode);
            }
        }
        public void Q501FindClosestBiggerValueInTree_1()
        {
            var root = BinaryTreeHelper.BuildTree(new[] { 100, 50, 150, 25, 75, 125, 175 });

            var result = Q501FindClosestBiggerValueInTree.FindClosestBiggerValue(root, 30);

            Assert.AreEqual(50, result.Value);
        }
Ejemplo n.º 3
0
        public void Q075FindMostCloseBinaryNodeTest2()
        {
            var root = BinaryTreeHelper.BuildTree(new int[] { 5, 4, 7, 8, 9, 6, 3, 2, 1 });

            var n1 = root.LeftChild.LeftChild.LeftChild;
            var n2 = root.LeftChild.RightChild;

            var expected = root.LeftChild;
            var result   = Q075FindMostCloseBinaryNode.FindNode(root, n1, n2);

            Assert.AreEqual(expected, result);

            n1       = root.LeftChild.LeftChild.LeftChild;
            n2       = root.RightChild.RightChild;
            expected = root;
            result   = Q075FindMostCloseBinaryNode.FindNode(root, n1, n2);
            Assert.AreEqual(expected, result);

            n1       = root.LeftChild.LeftChild.LeftChild;
            n2       = root.RightChild.LeftChild;
            expected = root;
            result   = Q075FindMostCloseBinaryNode.FindNode(root, n1, n2);
            Assert.AreEqual(expected, result);

            result = Q075FindMostCloseBinaryNode.FindNode(null, n1, n2);
            Assert.IsNull(result);

            result = Q075FindMostCloseBinaryNode.FindNode(root, null, n2);
            Assert.IsNull(result);

            result = Q075FindMostCloseBinaryNode.FindNode(root, n1, null);
            Assert.IsNull(result);

            result = Q075FindMostCloseBinaryNode.FindNode(root, n1, n1);
            Assert.IsNull(result);

            if (true)
            {
                n1 = root.LeftChild;
                n2 = root.LeftChild.LeftChild.RightChild;

                expected = root.LeftChild;
                result   = Q075FindMostCloseBinaryNode.FindNode(root, n1, n2);
                Assert.AreEqual(expected, result);
            }

            if (true)
            {
                n1     = root.LeftChild;
                n2     = new BinaryTreeNode <int>(5);
                result = Q075FindMostCloseBinaryNode.FindNode(root, n1, n2);
                Assert.AreEqual(n1, result);
            }
        }
Ejemplo n.º 4
0
        public void Q016PrintBinaryTreeNodeByLevelTest()
        {
            var          array          = new[] { 8, 6, 10, 5, 7, 9, 11 };
            var          invalidIndices = new[] { 0, 1, 2, 3, 4, 5, 6 };
            const string expected       = "8 6 10 5 7 9 11 ";

            var root = BinaryTreeHelper.BuildTree(array, invalidIndices);

            var actual = Q016PrintBinaryTreeNodeByLevel.Print(root);

            Assert.AreEqual(expected, actual);
        }
        public void Q004FindTreePathSumTest()
        {
            var root = BinaryTreeHelper.BuildTree(new [] { 10, 5, 12, 4, 7 }, new List <int> {
                0, 1, 2, 3, 4
            });
            var obj = new Q004FindTreePathSum();

            obj.FindTreePathSumInt32(root, 22);

            foreach (var path in obj.Paths)
            {
                Console.WriteLine(path);
            }

            var expect = new[] { "10->5->7", "10->12" };

            CollectionAssert.AreEquivalent(expect, obj.Paths);

            Console.WriteLine("==========================================");
            root = BinaryTreeHelper.BuildTree(new[] { 10, 5, 12, 4, 7, 0, 0, 3 }, new List <int> {
                0, 1, 2, 3, 4, 7
            });
            obj.FindTreePathSumInt32(root, 22);

            foreach (var path in obj.Paths)
            {
                Console.WriteLine(path);
            }

            expect = new[] { "10->5->4->3", "10->5->7", "10->12" };
            CollectionAssert.AreEquivalent(expect, obj.Paths);

            Console.WriteLine("==========================================");
            root = BinaryTreeHelper.BuildTree(new[] { 10, 5, 12, 4, 9, 0, 0, 3 }, new List <int> {
                0, 1, 2, 3, 4, 7
            });
            obj.FindTreePathSumInt32(root, 22);

            foreach (var path in obj.Paths)
            {
                Console.WriteLine(path);
            }

            expect = new[] { "10->5->4->3", "10->12" };
            CollectionAssert.AreEquivalent(expect, obj.Paths);
        }