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

2021-01-20 17:54

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

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 << "(";


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

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

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

马上注册会员

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