Example #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);
            }
        }
Example #2
0
        public void GetTreeHeight_Zero_WhenNull()
        {
            int h = BinaryTreeHelper.GetTreeHeight(null);

            // Expecting no exception
            h.Should().Be(0);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Creating Binary Tree ... !");
            var root = BinaryTreeHelper.InitializeBinaryTreeV2();

            Console.WriteLine(IsBST(root));
        }
        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);
        }
Example #5
0
        public void MirrorTest4()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(8);

            BinaryTreeHelper.SetMirrorIteratively(node1);
            string completed = GetNodeString(node1);

            Assert.AreEqual(completed, "8");
        }
Example #6
0
        public void MirrorTest5()
        {
            BinaryTreeNode node1 = null;

            BinaryTreeHelper.SetMirrorIteratively(node1);
            string completed = GetNodeString(node1);

            Assert.AreEqual(completed, null);
        }
Example #7
0
        public void IsBalancedTest6()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(1);

            bool actual = BinaryTreeHelper.IsBalancedBinaryTree(node1);

            Assert.AreEqual(actual, true);

            ClearUpTreeNode(node1);
        }
Example #8
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);
            }
        }
Example #9
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);
        }
Example #11
0
        public void MirrorTest2()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(8);
            BinaryTreeNode node2 = new BinaryTreeNode(7);
            BinaryTreeNode node3 = new BinaryTreeNode(6);
            BinaryTreeNode node4 = new BinaryTreeNode(5);
            BinaryTreeNode node5 = new BinaryTreeNode(4);

            node1.leftChild = node2;
            node2.leftChild = node3;
            node3.leftChild = node4;
            node4.leftChild = node5;

            BinaryTreeHelper.SetMirrorIteratively(node1);
            string completed = GetNodeString(node1);

            Assert.AreEqual(completed, "87654");
        }
Example #12
0
        public void MirrorTest1()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(8);
            BinaryTreeNode node2 = new BinaryTreeNode(6);
            BinaryTreeNode node3 = new BinaryTreeNode(10);
            BinaryTreeNode node4 = new BinaryTreeNode(5);
            BinaryTreeNode node5 = new BinaryTreeNode(7);
            BinaryTreeNode node6 = new BinaryTreeNode(9);
            BinaryTreeNode node7 = new BinaryTreeNode(11);

            SetSubTreeNode(node1, node2, node3);
            SetSubTreeNode(node2, node4, node5);
            SetSubTreeNode(node3, node6, node7);

            BinaryTreeHelper.SetMirrorIteratively(node1);
            string completed = GetNodeString(node1);

            Assert.AreEqual(completed, "810611975");
        }
Example #13
0
        public void IsBalancedTest5()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(1);
            BinaryTreeNode node2 = new BinaryTreeNode(2);
            BinaryTreeNode node3 = new BinaryTreeNode(3);
            BinaryTreeNode node4 = new BinaryTreeNode(4);
            BinaryTreeNode node5 = new BinaryTreeNode(5);

            SetSubTreeNode(node1, null, node2);
            SetSubTreeNode(node2, null, node3);
            SetSubTreeNode(node3, null, node4);
            SetSubTreeNode(node4, null, node5);

            bool actual = BinaryTreeHelper.IsBalancedBinaryTree(node1);

            Assert.AreEqual(actual, false);

            ClearUpTreeNode(node1);
        }
Example #14
0
        public void IsBalancedTest1()
        {
            BinaryTreeNode node1 = new BinaryTreeNode(1);
            BinaryTreeNode node2 = new BinaryTreeNode(2);
            BinaryTreeNode node3 = new BinaryTreeNode(3);
            BinaryTreeNode node4 = new BinaryTreeNode(4);
            BinaryTreeNode node5 = new BinaryTreeNode(5);
            BinaryTreeNode node6 = new BinaryTreeNode(6);
            BinaryTreeNode node7 = new BinaryTreeNode(7);

            SetSubTreeNode(node1, node2, node3);
            SetSubTreeNode(node2, node4, node5);
            SetSubTreeNode(node3, node6, node7);

            bool actual = BinaryTreeHelper.IsBalancedBinaryTree(node1);

            Assert.AreEqual(actual, true);

            ClearUpTreeNode(node1);
        }
Example #15
0
        public void IsBalancedTest7()
        {
            bool actual = BinaryTreeHelper.IsBalancedBinaryTree(null);

            Assert.AreEqual(actual, true);
        }
Example #16
0
        public void GetTreeHeight_CheckHeight()
        {
            int height = BinaryTreeHelper.GetTreeHeight(_root);

            height.Should().Be(3);
        }
Example #17
0
        public static void Run()
        {
            BinaryTreeHelper binaryTreeHelper = new BinaryTreeHelper();

            // Generate a Binary Tree
            BinaryTree binaryTree = new BinaryTree();

            binaryTreeHelper.Insert(binaryTree, 1);
            binaryTreeHelper.Insert(binaryTree, 2);
            binaryTreeHelper.Insert(binaryTree, 3);
            binaryTreeHelper.Insert(binaryTree, 4);
            binaryTreeHelper.Insert(binaryTree, 5);
            binaryTreeHelper.Insert(binaryTree, 6);
            binaryTreeHelper.Insert(binaryTree, 7);
            binaryTreeHelper.Insert(binaryTree, 8);

            //This should output : 8 4 2 5 1 6 3 7
            binaryTreeHelper.RecursiveInorderTraversal(binaryTree.Root);
            Console.WriteLine();

            //This should output : 4
            int?successorValue = Algorithm.FindSuccessorSubOptimalByValue(binaryTree, 8);

            if (successorValue != null)
            {
                Console.WriteLine("Successor Value found: " + successorValue);
            }
            else
            {
                Console.WriteLine("No successor found!");
            }

            //This should output : 5
            successorValue = Algorithm.FindSuccessorSubOptimalByValue(binaryTree, 2);
            if (successorValue != null)
            {
                Console.WriteLine("Successor Value found: " + successorValue);
            }
            else
            {
                Console.WriteLine("No successor found!");
            }

            //This should output : No successor found!
            successorValue = Algorithm.FindSuccessorSubOptimalByValue(binaryTree, 7);
            if (successorValue != null)
            {
                Console.WriteLine("Successor Value found: " + successorValue);
            }
            else
            {
                Console.WriteLine("No successor found!");
            }

            // Construct a Custom Binary Tree
            BinaryTreeNode binaryTreeNode1 = new BinaryTreeNode(1);
            BinaryTreeNode binaryTreeNode2 = new BinaryTreeNode(2);
            BinaryTreeNode binaryTreeNode3 = new BinaryTreeNode(3);
            BinaryTreeNode binaryTreeNode4 = new BinaryTreeNode(4);
            BinaryTreeNode binaryTreeNode5 = new BinaryTreeNode(5);
            BinaryTreeNode binaryTreeNode6 = new BinaryTreeNode(6);

            BinaryTree newBinaryTree = new BinaryTree
            {
                Root = binaryTreeNode1
            };

            newBinaryTree.Root.Left  = binaryTreeNode2;
            newBinaryTree.Root.Right = binaryTreeNode3;
            binaryTreeNode2.Left     = binaryTreeNode4;
            binaryTreeNode2.Right    = binaryTreeNode5;
            binaryTreeNode2.Parent   = binaryTreeNode1;
            binaryTreeNode3.Parent   = binaryTreeNode1;
            binaryTreeNode4.Left     = binaryTreeNode6;
            binaryTreeNode4.Parent   = binaryTreeNode2;
            binaryTreeNode5.Parent   = binaryTreeNode2;
            binaryTreeNode6.Parent   = binaryTreeNode4;

            //This should output : 6 4 2 5 1 3
            binaryTreeHelper.RecursiveInorderTraversal(newBinaryTree.Root);
            Console.WriteLine();

            // This should output: Node with value as 1
            BinaryTreeNode successorNode = Algorithm.FindSuccessorSubOptimalByNode(newBinaryTree, binaryTreeNode5);

            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }

            // This should output: Node with value as 3
            successorNode = Algorithm.FindSuccessorSubOptimalByNode(newBinaryTree, binaryTreeNode1);
            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }

            // This should output: No Successor found!
            successorNode = Algorithm.FindSuccessorSubOptimalByNode(newBinaryTree, binaryTreeNode3);
            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }

            // This should output: Node with value as 3
            successorNode = Algorithm.FindSuccessorOptimalByNode(binaryTreeNode1);
            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }

            // This should output: Node with value as 1
            successorNode = Algorithm.FindSuccessorOptimalByNode(binaryTreeNode5);
            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }

            // This should output: No Successor found!
            successorNode = Algorithm.FindSuccessorOptimalByNode(binaryTreeNode3);
            if (successorNode != null)
            {
                Console.WriteLine("Successor Node found with Value: " + successorNode.Value);
            }
            else
            {
                Console.WriteLine("No Successor found!");
            }
        }