public void BuildTree_PrintTree_ShouldWorkCorrectly()
        {
            // Arrange
            var tree =
                new Tree <int>(7,
                               new Tree <int>(19,
                                              new Tree <int>(1),
                                              new Tree <int>(12),
                                              new Tree <int>(31)),
                               new Tree <int>(21),
                               new Tree <int>(14,
                                              new Tree <int>(23),
                                              new Tree <int>(6)));

            // Act
            var outputStream = new MemoryStream();

            using (var outputWriter = new StreamWriter(outputStream))
            {
                Console.SetOut(outputWriter);
                tree.Print();
            }
            var output = Encoding.UTF8.GetString(outputStream.ToArray());

            // Assert
            var expectedOutput = "7\n  19\n    1\n    12\n    31\n  21\n  14\n    23\n    6\n";

            output = output.Replace("\r\n", "\n");
            Assert.AreEqual(expectedOutput, output);
        }
Exemple #2
0
            public void PrintTest()
            {
                _tree.Insert(10);
                _tree.Insert(5);
                _tree.Insert(20);
                _tree.Insert(15);
                _tree.Insert(25);
                _tree.Insert(3);
                _tree.Insert(9);

                _tree.Print();
            }
        public static void Main()
        {
            var tree =
                new Tree <int>(7,
                               new Tree <int>(19,
                                              new Tree <int>(1),
                                              new Tree <int>(12),
                                              new Tree <int>(31)),
                               new Tree <int>(21),
                               new Tree <int>(14,
                                              new Tree <int>(23),
                                              new Tree <int>(6)));

            Console.WriteLine("Tree (indented):");
            tree.Print();

            Console.WriteLine(string.Join(' ', tree.OrderDFS()));
            Console.WriteLine(string.Join(' ', tree.OrderBFS()));

            Console.Write("Tree nodes:");
            tree.Each(c => Console.Write(" " + c));
            Console.WriteLine();

            Console.WriteLine();

            var binaryTree =
                new BinaryTree <string>("*",
                                        new BinaryTree <string>("+",
                                                                new BinaryTree <string>("3"),
                                                                new BinaryTree <string>("2")),
                                        new BinaryTree <string>("-",
                                                                new BinaryTree <string>("9"),
                                                                new BinaryTree <string>("6")));

            Console.WriteLine("Binary tree (indented, pre-order):");
            binaryTree.PrintIndentedPreOrder();

            Console.Write("Binary tree nodes (in-order):");
            binaryTree.EachInOrder(c => Console.Write(" " + c));
            Console.WriteLine();

            Console.Write("Binary tree nodes (post-order):");
            binaryTree.EachPostOrder(c => Console.Write(" " + c));
            Console.WriteLine();
        }
        static void Main()
        {
            var tree =
                new Tree<int>(7,
                    new Tree<int>(19,
                        new Tree<int>(1),
                        new Tree<int>(12),
                        new Tree<int>(31)),
                    new Tree<int>(21),
                    new Tree<int>(14,
                        new Tree<int>(23),
                        new Tree<int>(6)));

            Console.WriteLine("Tree (indented):");
            tree.Print();

            Console.Write("Tree nodes:");
            tree.Each(c => Console.Write(" " + c));
            Console.WriteLine();

            Console.WriteLine();

            var binaryTree =
                new BinaryTree<string>("*",
                    new BinaryTree<string>("+",
                        new BinaryTree<string>("3"),
                        new BinaryTree<string>("2")),
                    new BinaryTree<string>("-",
                        new BinaryTree<string>("9"),
                        new BinaryTree<string>("6")));

            Console.WriteLine("Binary tree (indented, pre-order):");
            binaryTree.PrintIndentedPreOrder();

            Console.Write("Binary tree nodes (in-order):");
            binaryTree.EachInOrder(c => Console.Write(" " + c));
            Console.WriteLine();

            Console.Write("Binary tree nodes (post-order):");
            binaryTree.EachPostOrder(c => Console.Write(" " + c));
            Console.WriteLine();
        }