问题描述 :
内容:(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)
注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。
(2)ADT的简单应用:使用该ADT设计并实现若干应用二叉树的算法设计。
应用:要求设计一个算法,将表达式二叉树转换成原始的中缀表达式(括号恢复)。二叉树的存储结构的建立参见二叉树应用1。
注意:假定输入的中缀表达式为合法的表达式。仅考虑有小括弧的场合。运算符包括+、-、*、/,运算数为整数(不局限于个位数)。
参考函数原型:
(1)表达式二叉树转换成中缀式 (外壳部分,用户函数)
//表达式二叉树转换成中缀式
template<class ElemType>
void BianryTree_Infix(BinaryTree<ElemType> &T, string &inffix); //inffix:转换获得的中缀表达式字符串
(2)表达式二叉树转换成中缀式 (递归部分,用户函数)
//表达式二叉树转换中缀表达式
template<class ElemType>
void BianryTree_Infix_Cursive(BinaryTreeNode<ElemType> *root, string &inffix);
辅助函数:
(1)判断是否为运算符(用户函数)
//判断是否为运算符
bool isoperator( char op ){
switch(op){
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
return true;
default:
return false;
}
}
(2)求运算符的优先级(用户函数)
//求运算符的优先级
int getOperPri(char op)
{
switch(op)
{
case '(':
return 1; break;
case '+':
case '-':
return 2; break;
case '*':
case '/':
return 3; break;
default:
return 0;
}
}
解题代码:
// tree.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
using namespace std;
vector<string> departString_string(string data)
{
vector<int> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
return part;
}
//————————————————
//版权声明:本文为CSDN博主「systemyff」的原创文章,遵循CC 4.0 BY - SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https ://blog.csdn.net/u014377763/article/details/113845555
template<class ElemType>
struct tree_point {
ElemType data;//数据
struct tree_point* l_child, * r_child;//左、右孩子指针
};
template<class ElemType>
class BinaryTree {
private:
vector<tree_point<ElemType>*> outlist;
tree_point&

这篇博客介绍了如何设计一个二叉树的抽象数据类型(ADT),包括前序、中序和后序遍历等操作,并提供了将表达式二叉树转换为中缀表达式的算法。同时,给出了判断运算符和计算运算符优先级的辅助函数。代码示例展示了具体的实现细节。
&spm=1001.2101.3001.5002&articleId=117432006&d=1&t=3&u=8cc80efcafb74d88b2d86e4419135a1c)
676

被折叠的 条评论
为什么被折叠?



