Esempio n. 1
0
        public void PreOrderTraversalTest()
        {
            StringBuilder resultStr = new StringBuilder();

            BinaryTreeTraversal.PreOrderTraversal(GetBinaryTree()).Select(x => resultStr.Append(x));

            String.Equals(new StringBuilder("12453"), resultStr);
        }
Esempio n. 2
0
        public void BuildFromInOrderAndPreOrder(int[] inOrder, int[] preOrder)
        {
            var tb = new BinTreeFromPreAndInOrder();

            var root          = tb.BuildTree(preOrder, inOrder);
            var treeTraverser = new BinaryTreeTraversal <int>();

            Assert.Equal(preOrder, treeTraverser.PreOrderTraversal(root));
            Assert.Equal(inOrder, treeTraverser.InOrderTraversal(root));
        }
Esempio n. 3
0
        public void Basic()
        {
            var node1 = new TreeNode(1);
            var node2 = new TreeNode(2);
            var node3 = new TreeNode(3);

            node1.right = node2;
            node2.left  = node3;

            var traverser    = new BinaryTreeTraversal <int>();
            var preOrderList = traverser.PreOrderTraversal(node1);

            Assert.Equal(new [] { 1, 2, 3 }, preOrderList);
        }
Esempio n. 4
0
        public void Left_chain()
        {
            var node1 = new TreeNode(1);
            var node2 = new TreeNode(2);
            var node3 = new TreeNode(3);

            node2.left = node1;
            node3.left = node2;

            var traverser    = new BinaryTreeTraversal <int>();
            var preOrderList = traverser.PreOrderTraversal(node3);

            Assert.Equal(new[] { 3, 2, 1 }, preOrderList);
        }
Esempio n. 5
0
        public void Failed_case()
        {
            var node1 = new TreeNode(1);
            var node2 = new TreeNode(2);
            var node3 = new TreeNode(3);
            var node4 = new TreeNode(4);

            node1.left  = node4;
            node1.right = node3;
            node4.left  = node2;

            var traverser    = new BinaryTreeTraversal <int>();
            var preOrderList = traverser.PreOrderTraversal(node1);

            Assert.Equal(new[] { 1, 4, 2, 3 }, preOrderList);
        }