中缀表达式直接生成表达式二叉树

#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;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值