public void TestIsSumTree()
        {
            Assert.AreEqual(false, BinaryTreeOperations.IsSumTree(bst.Root));

            bst      = new BST();
            bst.Root = new Node {
                Data = 26
            };
            bst.Root.left = new Node {
                Data = 10
            };
            bst.Root.Right = new Node {
                Data = 3
            };

            bst.Root.left.left = new Node {
                Data = 4
            };
            bst.Root.left.Right = new Node {
                Data = 6
            };

            bst.Root.Right.Right = new Node {
                Data = 3
            };

            Assert.AreEqual(true, BinaryTreeOperations.IsSumTree(bst.Root));
        }
        public void CompareMethodTestLastCase()
        {
            BTN  second = new BTN();
            bool result = BinaryTreeOperations.Compare(null, second);

            Assert.IsFalse(result);
        }
Example #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            TreeTraversal.Run();
            CheckIfBinaryTreeBST.Run();
            BinaryTreeOperations.Run();
            TransformToBST.Run();
            LowestCommonAncestor.Run();
            BSTOperations.Run();

            Console.WriteLine();
            GraphTraversal graph = new GraphTraversal();

            graph.Run();

            Console.WriteLine();
            Console.WriteLine("Dijkstra Algo:");
            Dijkstra dj = new Dijkstra();

            dj.Run();

            SortingAlogrithm.Run();
            Console.WriteLine();
            Console.WriteLine("Heap : ");
            HeapProblem.Run();
            Console.ReadLine();
        }
Example #4
0
        public void TestPrintPostOrderFromInAndPre()
        {
            int[] inorder  = new int[] { 4, 2, 5, 1, 3, 6 };
            int[] pre      = new int[] { 1, 2, 4, 5, 3, 6 };
            int   preIndex = 0;

            BinaryTreeOperations.PrintPostOrderFromInAndPre(inorder, pre, 0, inorder.Length - 1, ref preIndex);
        }
Example #5
0
        public void TestBinaryTreeConstructionFromInAndPre()
        {
            int[] inorder  = new int[] { 4, 2, 5, 1, 3, 6 };
            int[] pre      = new int[] { 1, 2, 4, 5, 3, 6 };
            int   preIndex = 0;

            Node root = BinaryTreeOperations.ConstructFromInAndPre(inorder, pre, 0, inorder.Length - 1, ref preIndex);
        }
        public void CompareMethodTestThirdCase()
        {
            BTN first  = new BTN();
            BTN second = new BTN();

            this.createThirdCaseObjects(first, second);
            bool result = BinaryTreeOperations.Compare(first, second);

            Assert.IsFalse(result);
        }
Example #7
0
        public void TestGetDiameterOfBinaryTree1()
        {
            // Arrange

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.GetDiameterOfBinaryTree(root);

            // Assert
            Assert.AreEqual(4, result, "Wrong Value");
        }
Example #8
0
        public void TestInorderSuccessor1()
        {
            // Arrange

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.InorderSuccessor(root, root.Right.Left);

            // Assert
            Assert.AreEqual(6, result.Value, "Wrong Value");
        }
Example #9
0
        public void TestIsValidBinarySearchTree1()
        {
            // Arrange
            // Was just contemplating whether we need to call the method explicitly after we have marked it with TestInitialize Attribute
            // Turns Out :- No :-)

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.IsValidBinarySearchTree(root);

            // Assert
            Assert.AreEqual(true, result, "Wrong Value");
        }
Example #10
0
        public void TestIsValidBinarySearchTree2()
        {
            // Arrange
            var root = new TreeNode(1);

            root.Left       = new TreeNode(2);
            root.Left.Right = new TreeNode(5);
            root.Right      = new TreeNode(3);

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.IsValidBinarySearchTree(root);

            // Assert
            Assert.AreEqual(false, result, "Wrong Value");
        }
Example #11
0
        public void TestGetDiameterOfBinaryTree2()
        {
            // Arrange
            var root = new TreeNode(1);

            root.Left       = new TreeNode(2);
            root.Left.Right = new TreeNode(5);
            root.Right      = new TreeNode(3);

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.GetDiameterOfBinaryTree(root);

            // Assert
            Assert.AreEqual(3, result, "Wrong Value");
        }
Example #12
0
        public void TestBinaryTreePaths1()
        {
            // Arrange
            // Was just contemplating whether we need to call the method explicitly after we have marked it with TestInitialize Attribute
            // Turns Out :- No :-)

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var results = treeOperations.BinaryTreePaths(root);

            // Assert
            Assert.AreEqual(4, results.Count, "Wrong Value");
            Assert.AreEqual("4->2->1", results[0], "Wrong Value");
            Assert.AreEqual("4->2->3", results[1], "Wrong Value");
            Assert.AreEqual("4->6->5", results[2], "Wrong Value");
            Assert.AreEqual("4->6->7", results[3], "Wrong Value");
        }
Example #13
0
        public void TestBinaryTreeInOrderTraversal()
        {
            // Arrange
            // Was just contemplating whether we need to call the method explicitly after we have marked it with TestInitialize Attribute
            // Turns Out :- No :-)

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.InOrderTraversal(root);

            // Assert
            Assert.AreEqual(7, result.Count, "Wrong Value");
            for (int i = 0; i < result.Count; i++)
            {
                Assert.AreEqual(i + 1, result[i], "Wrong Value");
            }
        }
Example #14
0
        public void TestBinaryTreePaths2()
        {
            // Arrange
            var root = new TreeNode(1);

            root.Left       = new TreeNode(2);
            root.Left.Right = new TreeNode(5);
            root.Right      = new TreeNode(3);

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var results = treeOperations.BinaryTreePaths(root);

            // Assert
            Assert.AreEqual(2, results.Count, "Wrong Value");
            Assert.AreEqual("1->2->5", results[0], "Wrong Value");
            Assert.AreEqual("1->3", results[1], "Wrong Value");
        }
Example #15
0
        public void TestBinaryTreePreOrderTraversal()
        {
            // Arrange
            // Was just contemplating whether we need to call the method explicitly after we have marked it with TestInitialize Attribute
            // Turns Out :- No :-)

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.PreOrderTraversal(root);

            // Assert
            Assert.AreEqual(7, result.Count, "Wrong Value");
            Assert.AreEqual(4, result[0], "Wrong Value");
            Assert.AreEqual(2, result[1], "Wrong Value");
            Assert.AreEqual(1, result[2], "Wrong Value");
            Assert.AreEqual(3, result[3], "Wrong Value");
            Assert.AreEqual(6, result[4], "Wrong Value");
            Assert.AreEqual(5, result[5], "Wrong Value");
            Assert.AreEqual(7, result[6], "Wrong Value");
        }
Example #16
0
        public void TestBinarySearchTreeToDoublyCircularList()
        {
            // Arrange
            // Was just contemplating whether we need to call the method explicitly after we have marked it with TestInitialize Attribute
            // Turns Out :- No :-)

            // Act
            BinaryTreeOperations treeOperations = new BinaryTreeOperations();
            var result = treeOperations.BinarySearchTreeTreeToDoublyCircularList(root);

            // Assert
            Assert.AreEqual(1, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(2, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(3, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(4, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(5, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(6, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(7, result.Value, "Wrong Value");
            result = result.Right;
            Assert.AreEqual(1, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(7, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(6, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(5, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(4, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(3, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(2, result.Value, "Wrong Value");
            result = result.Left;
            Assert.AreEqual(1, result.Value, "Wrong Value");
        }
        public void TestDiameterN2()
        {
            int d = BinaryTreeOperations.Diameter(bst.Root);

            Assert.AreEqual(d, 6);
        }
Example #18
0
        public void TestGetHeight()
        {
            int h = BinaryTreeOperations.GetHeight(bst.Root);

            Assert.AreEqual(4, h);
        }
 public void TestMirror()
 {
     BinaryTreeOperations.Mirror(bst.Root);
 }