Exemplo n.º 1
0
        public  void GetExpressionTreeData(BinaryTreeNode btn)
        {
            if (btn.LeftChild.IsLeaf)
            {
                i += 1;
                // insert
                lstValue.Add(btn.LeftChild.Value);
                lstIsLeaf.Add(true);
                lstIsRoot.Add(false);
                lstLeftNode.Add(null);
                lstRightNode.Add(null);
            }
            else
            {
                GetExpressionTreeData(btn.LeftChild);
            }

            if (btn.RightChild.IsLeaf)
            {
                i += 1;
                // insert
                lstValue.Add(btn.RightChild.Value);
                lstIsLeaf.Add(true);
                lstIsRoot.Add(false);
                lstLeftNode.Add(null);
                lstRightNode.Add(null);
            }
            else
            {
                GetExpressionTreeData(btn.RightChild);
            }

            // insert btn
            i += 1;
            lstValue.Add(btn.Value);
            lstIsLeaf.Add(false);
            if (i != TreeNodeCount)
            {
                lstIsRoot.Add(false);
            }
            else
            {
                lstIsRoot.Add(true);
            }
            
            lstLeftNode.Add(btn.LeftChild.Value);
            lstRightNode.Add(btn.RightChild.Value);
        }
Exemplo n.º 2
0
        public  BinaryTreeNode Infix2ExpressionTree(string infixExpression)
        {
            lstValue = new List<string>();
        lstIsLeaf = new List<Boolean>();
       lstIsRoot = new List<Boolean>();
        lstLeftNode = new List<string>();
        lstRightNode = new List<string>();

            //Lis prefix = new List();
            Stack<BinaryTreeNode> operatorStack = new Stack<BinaryTreeNode>();
            Stack<BinaryTreeNode> nodeStack = new Stack<BinaryTreeNode>();

            FormatExpression(ref infixExpression);

            IEnumerable enumer = infixExpression.Split(' ');
            obtn = new BinaryTreeNode();
            
            foreach (string s in enumer)
            {
                if (IsOperand(s))
                    nodeStack.Push(new BinaryTreeNode(s));
                else if (s == "(")
                    operatorStack.Push(new BinaryTreeNode(s));
                else if (s == ")")
                {
                    while (operatorStack.Peek().Value != "(")
                        CreateSubTree(operatorStack, nodeStack);
                    operatorStack.Pop();
                }
                else if (IsOperator(s))
                {
                    while (operatorStack.Count > 0 && GetPriority(operatorStack.Peek().Value) >= GetPriority(s))
                        CreateSubTree(operatorStack, nodeStack);

                    operatorStack.Push(new BinaryTreeNode(s));
                }

            }

            while (operatorStack.Count > 0)
                CreateSubTree(operatorStack, nodeStack);

            obtn = nodeStack.Peek();
            //TreeNodeCount = infixExpression.Split(' ').Except(new string[2]{"(",")"}.AsEnumerable()).Count();
            TreeNodeCount = infixExpression.Split(' ').Count();
            return obtn;
        }