The data of an expression binary tree node may be an operand or an operator, can be stored using a union, and a variable indicates whether the stored operand or operator can be tokentype with the same enumeration type as in the Stack method:
1typedefenum2 {3 BEGIN,4 Number ,5 OPERATOR,6 Left_brac,7 Right_brac8 } tokentype;9 Ten classToken One { A Public: - Tokentype _type; - Union the { - Charop; - Doublenum; - } _data; +};
The calculator class of the binary tree method is the Binarytree class of the token class, which is the public inheritance from the node data data type:
1 classCalculator: PublicBinarytree<token>2 {3 Public:4 voidParseExpression (stringExpressionThrow(string);5 Doublecalculate ();6 7 Private:8stack<Char>_stkoperators;9Stack<binarytreenode<token> *>_stknodes;Tenstack<Double>_stknumbers; One A Static intPriorityCharop); - Static DoubleCalculateDoubleD1,CharOpDoubleD2)Throw(string); - the voidPostorder (binarytreenode<token> *node); - voidCalculatestack ()Throw(string); - voidDealwithnumber (Char*&ptoken)Throw(string); - voidDealwithoperator (Char*&ptoken)Throw(string); + voidDealwithleftbrac (Char*&ptoken)Throw(string); - voidDealwithrightbrac (Char*&ptoken)Throw(string); +};
Method ParseExpression () is used to convert an expression to a two-fork tree, which requires two stacks, one for storing operators, and one for storing pointers to sub-trees; the stack used to store floating-point types is only used when evaluating values.
The conversion process is basically the same as that of the stack method, and when the operand is encountered, a new node is generated and its pointer is pressed into the node pointer stack, and when the operator is encountered, the precedence of the top operator of the current operator and Operation Fu is compared, and if the operation Fu top operator has a higher precedence, it generates a new node. and a two node pointer is popped from the node pointer stack, as the Saozi right subtree of the new node, and finally the pointer of the new node is pressed into the node pointer stack, the current operator is pressed into the operator stack, otherwise only the current operator is pressed into the operator stack. Finally, the above procedure is repeated until the operator stack is empty, and the stack top element of the node pointer stack is a pointer to the tree root node. The conversion process for the expression "1+2*3" and "1*2+3" is as follows:
Using code to describe the process of manipulating a node pointer stack:
1 voidCalculator::calculatestack ()Throw(string)2 {3binarytreenode<token> * node =NewBinarytreenode<token>();4 assert (node);5Node->_data._type =OPERATOR;6Node->_data._data.op =_stkoperators.top ();7 _stkoperators.pop ();8ASSERT (!_stknodes.empty ());9Node->_rightchild =_stknodes.top ();Ten _stknodes.pop (); OneASSERT (!_stknodes.empty ()); ANode->_leftchild =_stknodes.top (); - _stknodes.pop (); - _stknodes.push (node); the}
Based on the mathematical rules and the process of finally emptying the operator stack, the ParseExpression () method is implemented as follows:
1 voidCalculator::p arseexpression (stringExpressionThrow(string)2 {3 destory ();4 while(!_stknodes.empty ())5 {6 _stknodes.pop ();7 }8 while(!_stkoperators.empty ())9 {Ten _stkoperators.pop (); One } ATokentype Lasttoken =BEGIN; - - Char* PToken = &expression[0]; the while(*PToken) - { - Switch(Lasttoken) - { + CaseBEGIN: - if(*ptoken = ='(') + { A //An expression begin with a left bracket at Dealwithleftbrac (pToken); -Lasttoken =Left_brac; - } - Else - { - //or a number in Dealwithnumber (pToken); -Lasttoken =Number ; to } + Break; - CaseNumber : the //After a number * if(*ptoken = =')') $ {Panax Notoginseng //It May is a right bracket - Dealwithrightbrac (pToken); theLasttoken =Right_brac; + } A Else the { + //It May is an operator - Dealwithoperator (pToken); $Lasttoken =OPERATOR; $ } - Break; - CaseOPERATOR: the CaseLeft_brac: - //After an operator or a left bracketWuyi if(*ptoken = ='(') the { - //It May is a left bracket Wu Dealwithleftbrac (pToken); -Lasttoken =Left_brac; About } $ Else - { - //It May is a number - Dealwithnumber (pToken); ALasttoken =Number ; + } the Break; - CaseRight_brac: $ //After a right bracket the if(*ptoken = =')') the { the //It May is another right bracket the Dealwithrightbrac (pToken); -Lasttoken =Right_brac; in } the Else the { About //It May is an perator the Dealwithoperator (pToken); theLasttoken =OPERATOR; the } + Break; - } the }Bayi the while(!_stkoperators.empty ()) the { - if(_stkoperators.top () = ='(') - { the Throw string("Bad token ' ('"); the } the Calculatestack (); the } - theASSERT (!_stknodes.empty ()); the_root =_stknodes.top (); the}
The method implementation is basically the same as the public Method calculator () which evaluates the Stack method. The Destory () method that starts the call inherits from the Binarytree class, freeing up the occupied two-fork tree node space, which prevents program memory overflow.
Expression evaluation (binary tree Method/c++ language description) (ii)