#include<iostream>
#include<stack>
#include<string>
using namespace std;
struct TreeNode
{
char key;
TreeNode* left;
TreeNode* right;
};
stack<char> opt;
stack<TreeNode*> subexp;
stack<char> bracket;
//检查表达式中括号是否匹配
bool TestBrackets(string exp,int len)
{
for(int i=0;i<len;i++)
{
char ch=exp[i];
if(ch=='(')
{
bracket.push(ch);
}else
{
if(ch==')')
{
if(!bracket.empty())
{
bracket.pop();
}else
{
return false;
}
}
}
}
if(bracket.empty())
{
return true;
}else
{
return false;
}
}
//将中缀表达式转换为表达式二叉树
TreeNode* InfixExpressionToBinaryTree(string exp,int len)
{
if(!TestBrackets(exp,len))
{
cout<<"the brackets can not be matched"<<endl;
return NULL;
}
for(int i=0;i<len;i++)
{
char ch=exp[i];
if(ch>='0'&&ch<='9')//检查当前字符是否是操作数,如果是,将操作数直接转换为树叶子节点(左右子树均为NULL)
{
TreeNode* p=new TreeNode;
p->key=ch;
p->left=NULL;
p->right=NULL;
subexp.push(p);
}
if(ch=='(')//如果是‘(’,则直接入栈
{
opt.push(ch);
}
if(ch=='+'||ch=='-')//如果是‘+’或者‘-’,新生成一个树节点,同时将栈subexp中栈顶以及栈顶向下一个元素作为新节点左右子树
{
if(opt.size()==0)
{
opt.push(ch);
}else
{
if(opt.top()=='(')
{
opt.push(ch);
}else
{
while((!opt.empty())&&opt.top()!='(')
{
char op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.push(ch);
}
}
}
if(ch=='*'||ch=='/')
{
if(opt.size()==0)
{
opt.push(ch);
}else
{
if(opt.top()=='(')
{
opt.push(ch);
}else
{
while((!opt.empty())&&opt.top()!='('&&opt.top()!='+'&&opt.top()!='-')
{
char op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.push(ch);
}
}
}
if(ch==')')
{
while((!opt.empty())&&opt.top()!='(')
{
char op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
opt.pop();
}
}
while(!opt.empty())
{
char op=opt.top();
opt.pop();
TreeNode* p1=subexp.top();
subexp.pop();
TreeNode* p2=subexp.top();
subexp.pop();
TreeNode* newNode=new TreeNode;
newNode->key=op;
newNode->left=p2;
newNode->right=p1;
subexp.push(newNode);
}
return subexp.top();
}
//表达式二叉树前序遍历
void PreVisitor(TreeNode* ROOT)
{
cout<<ROOT->key;
if(ROOT->left!=NULL)
{
PreVisitor(ROOT->left);
}
if(ROOT->right!=NULL)
{
PreVisitor(ROOT->right);
}
}
//表达式二叉树中序遍历,同时加括号确定运算优先级
void InfixVisitor(TreeNode* ROOT)
{
if(ROOT->left!=NULL)
{
cout<<"(";
InfixVisitor(ROOT->left);
}
cout<<ROOT->key;
if(ROOT->right!=NULL)
{
InfixVisitor(ROOT->right);
cout<<")";
}
}
//表达式二叉树后序遍历
void PostVisitor(TreeNode* ROOT)
{
if(ROOT->left!=NULL)
{
PostVisitor(ROOT->left);
}
if(ROOT->right!=NULL)
{
PostVisitor(ROOT->right);
}
cout<<ROOT->key;
}
int main()
{
string exp="1+2/3+(5*(7+8/3))+1-6/(5+3)+3+7*8/(6-2)";
int len=exp.length();
TreeNode* ROOT=InfixExpressionToBinaryTree(exp,len);
if(ROOT!=NULL)
{
PreVisitor(ROOT);
cout<<endl;
InfixVisitor(ROOT);
cout<<endl;
PostVisitor(ROOT);
cout<<endl;
}
while(!subexp.empty())
{
subexp.pop();
}
char ch;
cin>>ch;
exit(ch);
return 0;
}
中缀表达式直接生成表达式二叉树
最新推荐文章于 2020-12-08 16:46:26 发布