表达式构建二叉树(中缀,前缀,后缀)
《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。
// TheRealBinaryTreeOfMine.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<string>
#include<stack>
#include<queue>
#include<iostream>
#include<math.h>
using namespace std;
class BinaryNode{
public:
char date;
BinaryNode * leftchild;
BinaryNode * rightchild;
BinaryNode(){
date = 0;
leftchild = NULL;
rightchild = NULL;
}
BinaryNode(char ch){
date = ch;
leftchild = NULL;
rightchild = NULL;
}
};
int first_or_last(char a, char b); //判断两个运算符的优先级;
bool isOP(char ch); //判断该字符是否属于操作符; BinaryNode* Build_BinaryTree(BinaryNode* &BT, string str);//根据中缀表达式构建一颗二叉树; void Last(BinaryNode *bt); //后序遍历二叉树(递归);
void First(BinaryNode *bt); //先序遍历二叉树(递归);
void Inn(BinaryNode *bt); //中序遍历二叉树(递归);
string Exp_turn(string s); //后缀表达式转换成中缀表达式;
string Exp_turn_and_turn(string s); //前缀表达式转换为中缀表达式;
int choice(char c, char d); //判断表达式属于什么类型的表达式(中缀,后缀,前缀);
void Print(BinaryNode *bt); //打印二叉树(层序遍历,层层打印二叉树);
int first_or_last(char a, char b){
if (a == '/' || a == '*'){
if (b == '+' || b == '-')
return 1;
if (b == '/' || b == '*')
return 0;
else
return 0;
}
if (a == '+' || a == '-'){
if (b == '+' || b == '-')
return 0;
if (b == '/' || b == '*')
return 0;
else
return 0;
}
else
《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。
return 0;
}
bool isOP(char ch)
{
string OP = { '+', '-', '*', '/', '(', ')' };
for (int i = 0; i < 6; i++){
if (ch == OP[i])
return true;
}
return false;
}
BinaryNode* Build_BinaryTree(BinaryNode* &BT, string str){
queue <BinaryNode*> aQ;
BinaryNode * root = BT;
if (str[0] == '('){
root = new BinaryNode(str[2]);
BT = new BinaryNode(str[1]);
BinaryNode *R = root;
aQ.push(BT);
int j = 3;
while (str[j] != '\0')
{
if (!isOP(str[j]))
{
BT = new BinaryNode(str[j]);
aQ.push(BT);
j++;
}
else
{
if (str[j] == ')' || str[j] == '(')
{
}
else
{
BT = new BinaryNode(str[j]);
root->leftchild = aQ.front();
//cout << root->leftchild->date;
root->rightchild = BT;
//cout << root->rightchild->date;
aQ.pop();
root = BT;
}
j++;
}
}
root->leftchild = aQ.front();
//cout << root->leftchild->date;
aQ.pop();
root->rightchild = aQ.front();
//cout << root->rightchild->date;
aQ.pop();
return R;
}
else{
root = new BinaryNode(str[1]);
BT = new BinaryNode(str[0]);
BinaryNode *R = root;
aQ.push(BT);
《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。
int j = 2;
while (str[j] != '\0')
{
if (!isOP(str[j]))
{
BT = new BinaryNode(str[j]);
aQ.push(BT);
j++;
}
else
{
if (str[j] == '(' || str[j] == ')')
{
}
else
{
BT = new BinaryNode(str[j]);
root->leftchild = aQ.front();
//cout << root->leftchild->date;
root->rightchild = BT;
//cout << root->rightchild->date;
aQ.pop();
root = BT;
}
j++;
}
}
root->leftchild = aQ.front();
//cout << root->leftchild->date;
aQ.pop();
root->rightchild = aQ.front();
//cout << root->rightchild->date;
aQ.pop();
return R;
}
}//中缀表达式构建了二叉树函数(消除了‘(’和‘)’)
void Last(BinaryNode *bt){
if (bt){
Last(bt->leftchild);
Last(bt->rightchild);
cout << bt->date;
}
}//后序遍历函数
void First(BinaryNode *bt){
if (bt){
cout << bt->date;
First(bt->leftchild);
First(bt->rightchild);
}
}//先序遍历函数
void Inn(BinaryNode *bt){
if (bt){
Inn(bt->leftchild);
cout << bt->date;
if (bt->rightchild)
{
if (isOP(bt->rightchild->date) && first_or_last(bt->date, bt->rightchild->date) == 1){ cout << "(";
《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。
Inn(bt->rightchild);
cout << ")"; //考虑root与root->leftchild操作符的优先级,如果root的优先级较大则需要添加小括号;
}
else
Inn(bt->rightchild);
}
}
}
string Exp_turn(string s){
queue <char> bQ;
stack <char> bS;
int k = 0;
while (s[k] != '\0'){
if (!isOP(s[k])){
bQ.push(s[k]);
}
else
{
bS.push(s[k]);
}
k++;
}
char ss[30];
int g = 0;
while (!bQ.empty())
{
ss[g] = bQ.front();
bQ.pop();
g++;
if (!bS.em …… 此处隐藏:3005字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [资格考试]石油钻采专业设备项目可行性研究报告编
- [资格考试]2012-2013学年度第二学期麻风病防治知
- [资格考试]道路勘测设计 绪论
- [资格考试]控烟戒烟知识培训资料
- [资格考试]建设工程安全生产管理(三类人员安全员
- [资格考试]photoshop制作茶叶包装盒步骤平面效果
- [资格考试]授课进度计划表封面(09-10下施工)
- [资格考试]麦肯锡卓越工作方法读后感
- [资格考试]2007年广西区农村信用社招聘考试试题
- [资格考试]软件实施工程师笔试题
- [资格考试]2014年初三数学复习专练第一章 数与式(
- [资格考试]中国糯玉米汁饮料市场发展概况及投资战
- [资格考试]塑钢门窗安装((专项方案)15)
- [资格考试]初中数学答题卡模板2
- [资格考试]2015-2020年中国效率手册行业市场调查
- [资格考试]华北电力大学学习实践活动领导小组办公
- [资格考试]溃疡性结肠炎研究的新进展
- [资格考试]人教版高中语文1—5册(必修)背诵篇目名
- [资格考试]ISO9001-2018质量管理体系最新版标准
- [资格考试]论文之希尔顿酒店集团进入中国的战略研
- 全国中小学生转学申请表
- 《奇迹暖暖》17-支2文学少女小满(9)公
- 2019-2020学年八年级地理下册 第六章
- 2005年高考试题——英语(天津卷)
- 无纺布耐磨测试方法及标准
- 建筑工程施工劳动力安排计划
- (目录)中国中央空调行业市场深度调研分
- 中国期货价格期限结构模型实证分析
- AutoCAD 2016基础教程第2章 AutoCAD基
- 2014-2015学年西城初三期末数学试题及
- 机械加工工艺基础(完整版)
- 归因理论在管理中的应用[1]0
- 突破瓶颈 实现医院可持续发展
- 2014年南京师范大学商学院决策学招生目
- 现浇箱梁支架预压报告
- Excel_2010函数图表入门与实战
- 人教版新课标初中数学 13.1 轴对称 (
- Visual Basic 6.0程序设计教程电子教案
- 2010北京助理工程师考试复习《建筑施工
- 国外5大医疗互联网模式分析




