/// <summary> /// Visit all nodes in the binary tree. /// </summary> /// <param name="visitor">Visitor</param> /// <param name="curNode">Current node</param> protected virtual void Visit(IVisitor visitor, MathParserTreeNode curNode) { if (curNode != null) { if (curNode.Left != null) Visit(visitor, curNode.Left); if (curNode.Right != null) Visit(visitor, curNode.Right); visitor.Action(curNode); } }
public void Action(MathParserTreeNode treeNode) { _count++; }
public void Action(MathParserTreeNode treeNode) { if (treeNode.Operation == Operation.NoOperation) return; _polish[_index++] = treeNode.Operation; }
/// <summary> /// Create binary tree object with specified root. /// </summary> /// <param name="root"></param> public MathParserBinaryTree(MathParserTreeNode root) { _root = root; }
public MathParserBinaryTree() { _root = new MathParserTreeNode(Nonterminal.NoValue); }
/// <summary> /// Perform semantic transform of the introduced expression. Check if the expression is valid and build BinaryTree needed for polish postfix expression. /// </summary> /// <exception cref="MathParserException">MathParserException</exception> protected virtual void SemanticTransform() { _continueParsing = true; _binaryTree = new MathParserBinaryTree(new MathParserTreeNode(Nonterminal.End)); this._sizeOfPolishPostfixExpression = 0; //initializing _indexOfChar = 0; if (_listOfDoubleInputs.Count != 0) _listOfDoubleInputs.Clear(); if (_listOfVariableInputs.Count != 0) _listOfVariableInputs.Clear(); //this._mathInput = this._mathInput.Trim() + "$"; //appeding the end-character this._binaryTree.Root.AddLeftChild(new MathParserTreeNode(Nonterminal.Expression)); //Expression$ Stack<MathParserTreeNode> stack = new Stack<MathParserTreeNode>(); stack.Push(this._binaryTree.Root); //$ _traverseNode = this._binaryTree.Root.Left; stack.Push(_traverseNode); //starting from Nonterminal.Expression while (_continueParsing) //main cycle of parsing { _traverseNode = stack.Pop(); switch (_traverseNode.Name) { case Nonterminal.Expression: this.ExpressionToTermW(stack); break; case Nonterminal.W: this.WtoXWorEps(stack); break; case Nonterminal.X: this.XtoMinusTermOrXtoPlusTerm(stack); break; case Nonterminal.Term: this.TermToPowerK(stack); break; case Nonterminal.K: this.KtoYKorKtoEps(stack); break; case Nonterminal.Y: this.YtoMultiplyPowerOrYtoDividePower(stack); break; case Nonterminal.Power: this.PowerToFactoV(stack); break; case Nonterminal.V: this.VtoZVorVtoEps(stack); break; case Nonterminal.Z: this.ZtoPowerFactor(stack); break; case Nonterminal.Factor: this.Factor(stack); break; case Nonterminal.ParenthesisClose: this.ParanthesisClose(stack); break; case Nonterminal.End: this.End(stack); break; case Nonterminal.AnyValue: break; } } }