无法从后缀表达式创建解析树
Cannot create a parse tree from a postfix expression
我试图从后缀表达式创建一个解析树。但是它给了我分割错误。
下面是我的代码:#include <iostream>
#include <stack>
#include <string>
#include <set>
#include <vector>
#include <cstdio>
#include <queue>
#include <list>
using namespace std;
struct my_tree{
struct my_tree* left;
char a;
struct my_tree* right;
};
typedef struct my_tree TREE;
bool is_binary_op(char a){
if(a == '|' || a == '.') return true;
else return false;
}
bool is_unary_op(char a){
if(a == '*') return true;
else return false;
}
int main() {
string postfix = "ab|*a.b.";
stack<TREE*> parse_tree;
for(unsigned i=0; i<postfix.length(); i++){
if(is_binary_op(postfix[i])){
TREE* n;
TREE* right = parse_tree.top();
parse_tree.pop();
TREE* left = parse_tree.top();
parse_tree.pop();
n->left = left;
n->a = postfix[i];
n->right = right;
parse_tree.push(n);
} else if(is_unary_op(postfix[i])){
TREE* n;
TREE* left = parse_tree.top();
parse_tree.pop();
n->left = left;
n->a = postfix[i];
n->right = NULL;
parse_tree.push(n);
} else{
TREE* n;
n->left = NULL;
n->a = postfix[i];
n->right = NULL;
parse_tree.push(n);
}
}
return 0;
}
修改所有
TREE *n;
到
TREE *n = new TREE;
,因为它们似乎都是树上的新节点。您需要通过operator new
分配实际实例
相关文章:
- 使用成员在类中创建 lambda 表达式
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 创建结构体向量,表达式:向量下标超出范围
- 使用 lambda 表达式创建线程时,如何为每个线程提供自己的 lambda 表达式副本
- 使用makeword函数创建错误e0109-表观呼叫的括号前表达式必须具有(指针到 - )函数类型
- 使用参数包和元组创建一个简单的表达式类
- 如何创建适用于 lambda 表达式的排序函数
- 从折叠表达式创建带有分度的字符串
- 异常表达式创建的异常对象的类型
- "std::string + char"表达式会创建另一个 std::string 吗?
- 是否可以创建一个字符串数组,如果是的话,为什么我会遇到此错误:错误:预期表达式
- #include < regex.h> 用于创建正确正则表达式的问题
- 在不使用lambda的情况下,我如何整齐地创建一个表达式的函数
- 什么表达式创建x值
- 使用自动关键字创建的类型的表达式模板中存在分段错误
- 从嵌套循环创建数学表达式
- 如何从嵌套的 lambda 表达式函子类创建
- 如何从队列中的运算符和操作数创建数学表达式
- 创建单个正则表达式而不是多个正则表达式
- 使用数字和字符操作符创建表达式