首页 > 解决方案 > 为多位数字构建表达式树

问题描述

我想为多位数字构建一个表达式树就像给定的表达式是(122 + 30),并且它的后序遍历给出“122 30 +”但是这段代码创建了表达式树并给出了一个数字的后序遍历仅像 (1+2) 这样的表达式

#include <bits/stdc++.h> 
using namespace std; 
  
// Tree Structure 
typedef struct node  
{ 
    char data; 
    struct node *left, *right; 
} * nptr; 
  
// Function to create new node 
nptr newNode(char c) 
{ 
    nptr n = new node; 
    n->data = c; 
    n->left = n->right = nullptr; 
    return n; 
} 
unsigned int getOperand(char ch) {
    
        if(ch >= '0' && ch <= '9'){
            return ch - '0';
        }
        return 0;
    }
// Function to build Expression Tree 
nptr build(string& s) 
{ 
  
    // Stack to hold nodes 
    stack<nptr> stN; 
  
    // Stack to hold chars 
    stack<char> stC; 
    nptr t, t1, t2; 
  
    // Prioritising the operators 
    int p[123] = { 0 }; 
    p['+'] = p['-'] = 1, p['/'] = p['*'] = 2, p['^'] = 3, 
    p[')'] = 0; 
  
    for (int i = 0; i < s.length(); i++)  
    { 
        if (s[i] == '(') { 
  
            // Push '(' in char stack 
            stC.push(s[i]); 
        } 
  
        // Push the operands in node stack 
        else if (getOperand(s[i]))  
        { 
            t = newNode(s[i]); 
            stN.push(t); 
        } 
        else if (p[s[i]] > 0)  
        { 
            // If an operator with lower or 
            // same associativity appears 
            while ( 
                !stC.empty() && stC.top() != '('
                && ((s[i] != '^' && p[stC.top()] >= p[s[i]]) 
                    || (s[i] == '^'
                        && p[stC.top()] > p[s[i]])))  
            { 
  
                // Get and remove the top element 
                // from the character stack 
                t = newNode(stC.top()); 
                stC.pop(); 
  
                // Get and remove the top element 
                // from the node stack 
                t1 = stN.top(); 
                stN.pop(); 
  
                // Get and remove the currently top 
                // element from the node stack 
                t2 = stN.top(); 
                stN.pop(); 
  
                // Update the tree 
                t->left = t2; 
                t->right = t1; 
  
                // Push the node to the node stack 
                stN.push(t); 
            } 
  
            // Push s[i] to char stack 
            stC.push(s[i]); 
        } 
        else if (s[i] == ')') { 
            while (!stC.empty() && stC.top() != '(')  
            { 
                t = newNode(stC.top()); 
                stC.pop(); 
                t1 = stN.top(); 
                stN.pop(); 
                t2 = stN.top(); 
                stN.pop(); 
                t->left = t2; 
                t->right = t1; 
                stN.push(t); 
            } 
            stC.pop(); 
        } 
    } 
    t = stN.top(); 
    return t; 
} 
  
// Function to print the post order 
// traversal of the tree 
void postorder(nptr root) 
{ 
    if (root)  
    { 
        postorder(root->left); 
        postorder(root->right); 
        cout << root->data; 
    } 
} 

int main() 
{ 
    string s ; 
    getline(cin,s);
    nptr root = build(s); 
    
    // Function call 
    postorder(root); 
  
    return 0; 
}

我应该做些什么改变才能处理多位表达式。

标签: c++treestack

解决方案


推荐阅读