表达式构建二叉树(中缀,前缀,后缀)

2021-01-20 17:54

《数据结构与算法》上的一道作业题答案,本程序设计二叉树的构建,遍历以及打印等函数,能够实现自动分辨所输入表达式属于什么类型。

// 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


表达式构建二叉树(中缀,前缀,后缀).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:3.4学习的基本理论之学习曲线的实验

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: