Exemple #1
0
        public ExpressionTree(string postfix)
        {
            var stack = new Stack <ExpressionTreeNode>();

            for (var i = 0; i < postfix.Length; i++)
            {
                var node = new ExpressionTreeNode()
                {
                    Value = postfix[i]
                };

                if (IsOperator(postfix[i]))
                {
                    var leftNode  = stack.Count > 0 ? stack.Pop() : null;
                    var rightNode = stack.Count > 0 ? stack.Pop() : null;

                    node.Right = leftNode;
                    node.Left  = rightNode;

                    stack.Push(node);
                }
                else
                {
                    stack.Push(node);
                }
            }

            Head = stack.Pop();
        }
Exemple #2
0
        public void PrintInorder(ExpressionTreeNode node)
        {
            if (node == null)
            {
                return;
            }

            PrintInorder(node.Left);
            Console.Write(node.Value + " ");
            PrintInorder(node.Right);
        }