二叉树的表达式求值

问题描述:

  输入一个表达式(表达式中的数均为小于10的正整数),利用二叉树来表示该表达数,创建表达式树,然后利用二叉树的遍历操作求表达式的值。

输入要求:

  多组数据,每组一行,以‘=’结尾。当输入只有一个‘=’时, 输入结束。

输出要求:

  每组数据输出一行为表达式的值。

样例:

  输入样例:

  1+2-3*4+(1+2)*3=

  =

  输出样例:

  0

  思路:分别用num 队列来存数,op队列来存运算符。然后取一个运算符为父节点,取两个数为子结点。将数叠加后就组成了一颗表达式树,然后后序遍历求值即可。

  

#include
#include
#include

using namespace std;
typedef struct Node* BinTree;
typedef BinTree BT;
// 1+2-3*4+(1+2)*3=
string s;
queue<char> num;
queue<char> op;
struct Node{
    char Data;
    BT Left;
    BT Right;
    int ans;
};

int fact(char c) {
    if (c >= '0' && c <= '9') return 1;
    else return 2;
}
BT createNode(char c){
    BT p = new Node;
    p->Data  = c;
    p->Left = p->Right = NULL;
    if (fact(c) == 1)
        p->ans = c - '0';
    else
        p->ans = 0;
    return p;
}
BT createTree() {
    for (int i = 0; i < s.size() - 1; i++) {
        if(fact(s[i]) == 1) num.push(s[i]);
        else op.push(s[i]);
    }
    BT Head = NULL;
    int flag = 0;     //标记有括号时的情况 
    int sflag = 0;      //处理开始时为括号的情况 
    if(s[0] == '(') sflag = 1;
    while(!op.empty()) {
        char  option;
        option = op.front(); op.pop();
        if (option != '(' && option != ')') {
              BT T = createNode(option);
            if (option == '+' || option == '-') {
                if (flag == 0) {
                    if (Head == NULL) {
                        T->Left = createNode(num.front());
                        num.pop();
                        T->Right = createNode(num.front());
                        num.pop(); 
                    }
                    else {
                        T->Left = Head;
                        T->Right = createNode(num.front());
                        num.pop();
                    }
                    Head = T;
                }
                else {
                    if (Head == NULL) {
                        T->Left = createNode(num.front());
                        num.pop();
                        T->Right = createNode(num.front());
                        num.pop(); 
                        Head = T; 
                    }
                    else {
                        T->Left = Head->Right ;
                        Head->Right = T;
                        T->Right = createNode(num.front());
                        num.pop(); 
                    }
                }
            }
            else if(option == '*' || option == '/') {
                if (flag == 0) {
                    if(Head == NULL) {
                        T->Left = createNode(num.front());
                        num.pop();
                        T->Right = createNode(num.front());
                        num.pop();
                        Head = T; 
                       }
                    else {
                        if(sflag == 1 || Head->Data == '*' || Head->Data == '/') {
                            T->Left = Head;
                            Head = T;
                            T->Right = createNode(num.front());
                            num.pop();
                            sflag =0;
                        }
                        else {
                            T->Left = Head->Right ;
                            Head->Right = T;
                            T->Right = createNode(num.front());
                            num.pop();
                        }
                    }
                }
                if (flag == 1) {
                    if(Head == NULL) {
                        T->Left = createNode(num.front());
                        num.pop();
                        T->Right = createNode(num.front());
                        num.pop();
                        Head = T; 
                       }
                       else {
                           T->Left = Head->Right;
                          Head->Right= T;
                          T->Right = createNode(num.front());
                        num.pop();     
                    }
                }
                
            } 
        }
        else if (option == '('){
            flag = 1;
            //continue;
        } 
        else if (option == ')'){
            flag = 0;
            //continue;
        }
    }
    return Head;
} 
void InorderTraversal_1(BT L){
    if(L){
        InorderTraversal_1(L->Left );
        printf("%d ",L->ans );
        InorderTraversal_1(L->Right );
    }
}
void solve(BT L){
    if(L){
        solve(L->Left );
        solve(L->Right );
        char option = L->Data ;
        if (option == '+') L->ans = L->Left->ans + L->Right->ans ;
        if (option == '-') L->ans = L->Left->ans - L->Right->ans ;
        if (option == '*') L->ans = L->Left->ans * L->Right->ans ;
        if (option == '/') L->ans = L->Left->ans / L->Right->ans ;
        //if(option < '0' || option > '9')
        //    printf("%d %c %d = %d\n", L->Left->ans, option, L->Right->ans, L->ans );
        
    }
}
void InorderTraversal_2(BT L){
    BT T=L;
    stack s;
    while(T||!s.empty()){
        while(T){
            s.push(T);
            T=T->Left ;
        }
        T=s.top();
        s.pop();
        printf("%c ",T->Data );
        T=T->Right ;
    }
}
int main() {
    while(cin >> s && s[0] != '='){
        BT H = createTree();
        //InorderTraversal_2(H);
        //cout << endl;
        
        solve(H);
        //InorderTraversal_1(H);
        //cout << endl;
        cout << H->ans << endl;
    } 
}

  可能我写的过于复杂,有同学做的比较好

  。。。。。。。。。。。

转载于:https://www.cnblogs.com/astonc/p/10783658.html

你可能感兴趣的:(二叉树的表达式求值)