Exemplo n.º 1
0
        private static void Main()
        {
            var f = new BinaryTree <char>('F');
            var b = new BinaryTree <char>('B');
            var g = new BinaryTree <char>('G');
            var a = new BinaryTree <char>('A');
            var d = new BinaryTree <char>('D');
            var i = new BinaryTree <char>('I');
            var c = new BinaryTree <char>('C');
            var e = new BinaryTree <char>('E');
            var h = new BinaryTree <char>('H');

            f.AddLeft(b);
            f.AddRight(g);
            b.AddLeft(a);
            b.AddRight(d);
            g.AddRight(i);
            i.AddLeft(h);
            d.AddLeft(c);
            d.AddRight(e);

            Console.WriteLine("PreOrder tree traversal: ");
            DisplayTree.PreOrderTraversal(f);

            Console.WriteLine();
            Console.WriteLine("InOrder tree traversal: ");
            DisplayTree.InOrderTraversal(f);

            Console.WriteLine();
            Console.WriteLine("OutOrder tree traversal: ");
            DisplayTree.OutOrderTraversal(f);

            Console.WriteLine();
            Console.WriteLine("PostOrder tree traversal: ");
            DisplayTree.PostOrderTraversal(f);

            Console.WriteLine();
            Console.WriteLine("LevelOrder tree traversal: ");
            DisplayTree.BreadthFirstTreeTraversal(f);
        }
Exemplo n.º 2
0
        private static void Main()
        {
            const string infix = "3+2*4/5";

            Console.WriteLine($"Initial infix: {infix}");
            // 324*5/+
            var postfix = ShuntingYardAlgorithm.PostfixString(infix);

            Console.WriteLine($"Postfix by Shunting-yard: {postfix}");
            var arithmeticTree = ExpressionTree.BuildArithmeticTree(postfix);

            Console.WriteLine("Infix by In-Order traversal: ");
            DisplayTree.InOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Postfix notation by Post-Order traversal: ");
            DisplayTree.PostOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Prefix notation by Pre-Order traversal: ");
            DisplayTree.PreOrderTraversal(arithmeticTree);
            Console.WriteLine();
            Console.WriteLine("Infix: ");
            ExpressionTree.PrintInfixExpression(arithmeticTree);
        }