[item - solving algebraic expression with binary tree]

The binary tree is used to represent the algebraic expression. Each branch node of the tree represents an operator, and each leaf node represents an operand (to simplify, only +, -, *, /, without parenthesis, and the operand is only a digit character). This project only considers that the input conforms to the above rules). Please design algorithm , (1) according to the form "1 + 2 * 3 − 4 / 5"

”For the expression represented by the string of, construct the corresponding binary tree (as shown in the figure). When calculating the value of the expression with the idea of post order traversal, it can reflect the rule of multiplication and division before addition and subtraction; (2) for the binary tree constructed, calculate the value of the expression.

btree.h in the [reference solution] procedure, see Binary tree algorithm library.

#include <stdio.h> #include <string.h> #include <malloc.h> #include "btree.h" //Using the string between s[i] and s[j], construct the representation of binary tree BTNode *CRTree(char s[],int i,int j) { BTNode *p; int k,plus=0,posi; if (i==j) //i is the same as j, which means that there is only one character and a leaf node is constructed { p=(BTNode *)malloc(sizeof(BTNode)); //Allocate storage space p->data=s[i]; //The value is s[i] p->lchild=NULL; p->rchild=NULL; return p; } //The following is the case of i!=j for (k=i; k<=j; k++) if (s[k]=='+' || s[k]=='-') { plus++; posi=k; //Last + or - position } if (plus==0) //There is no + or - case (because if there is +, -, plus + + must be executed before) for (k=i; k<=j; k++) if (s[k]=='*' || s[k]=='/') { plus++; posi=k; } //The above processing takes into account the priority of putting +, - on the higher level of binary tree //In the future, we use the idea of post order traversal //Multiplication and division at the lower level take precedence //Thus, the algorithm of "multiplication and division before addition and subtraction" is embodied //Create a branch node with the detected operator as the node value if (plus!=0) { p=(BTNode *)malloc(sizeof(BTNode)); p->data=s[posi]; //The node value is s[posi] p->lchild=CRTree(s,i,posi-1); //The left subtree i s composed of s[i] to s[posi-1] p->rchild=CRTree(s,posi+1,j); //Right subtree is composed of s[poso+1] to s[j] return p; } else //Returns NULL if there is no operator return NULL; } double Comp(BTNode *b) { double v1,v2; if (b==NULL) return 0; if (b->lchild==NULL && b->rchild==NULL) //Leaf node, which should be a numeric character (illegal expression is not considered in this project) return b->data-'0'; //The leaf node directly returns the node value. The number saved in the node is in the form of characters, so you need to -'0 ' v1=Comp(b->lchild); //Calculate left subtree first v2=Comp(b->rchild); //Recalculate right subtree switch(b->data) //The results of left and right subtrees are calculated again, using the idea of post order traversal { case '+': return v1+v2; case '-': return v1-v2; case '*': return v1*v2; case '/': if (v2!=0) return v1/v2; else abort(); } } int main() { BTNode *b; char s[MaxSize]="1+2*3-4/5"; printf("Algebraic expression%s\n",s); b=CRTree(s,0,strlen(s)-1); printf("Corresponding binary tree:"); DispBTNode(b); printf("\n Value of expression:%g\n",Comp(b)); DestroyBTNode(b); return 0; }