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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

// TheRealBinaryTreeOfMine.cpp : 定义控制台应用程序的入口点。

//

#include"stdafx.h"

#include

#include

#include

#include

#include

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 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);

相关文档
最新文档