前言
狠狠地记录一次艰难的数据结构作业Orz,属实是死去的记忆又开始攻击我了。。。
EduOJ再你妈的见,劳资再次正式宣布你被打入冷宫!
一、题目部分
二、代码部分
一共包括一个二叉树的结构体(BinaryTreeNode),三个子函数(isNum()、PreVisit(BinaryTreeNode* root,int cnt)、PostBiTree())和一个主函数(main() ),重点是控制打印格式的第二个子函数,真的是太!搞!心!态!了
打印格式控制函数
后缀表达式先序打印出一棵二叉树:
void PreVisit(BinaryTreeNode* root,int cnt) {
if (root != NULL) {
for (int i = 0; i < cnt; i++) {
cout << " ";
}
cout << root->info;
if (root->left != NULL || root->right != NULL) {
cout << "[";
cout << endl;
PreVisit(root->left,cnt+1);
cout << endl;
if (root->right != NULL) {
for (int i = 0; i <= cnt; i++) {
cout << " ";
}
cout << "," << endl;;
PreVisit(root->right,cnt+1);
cout << endl;
}
for (int i = 0; i < cnt; i++) {
cout << " ";
}
cout << "]";
}
}
}
空格和右括号加逗号是这部分打印的难点,如果我自己一个人真的是想破脑袋都没想出来,还好问了亲爱的内酱,提出了递归这个想法(递归没学明白太难想到了T_T)。虽然说用递归有一定概率可以解决这个问题问题,但还是尝试了好久,终于功夫不负有心人,它终于按照格式要求打印出来了(太不容易了~)
————
PS:写着写着感觉这个格式有点像广义表(但老师不教广义表只好一知半解地瞎琢磨T_T)
先序遍历构造二叉树
通过栈的方式将输入的后缀表达式按先序遍历的方式构造一棵二叉树,再用PreVisit()将这颗二叉树打印出来。
void PostBiTree() {
int index = 0;
string ch,tmpch,aa,bb;
string arr[100];
stack<BinaryTreeNode*>q;
BinaryTreeNode* result = NULL;
while (cin >> ch) { //输入后缀表达式
if (ch == "=")break;
arr[index++] = ch;
}
//构造二叉树
for (int i = 0; i < index; i++) {
BinaryTreeNode* temp = new BinaryTreeNode();
if (arr[i] == "+" || arr[i] == "-" || arr[i] == "*" || arr[i] == "/") {
if(arr[i]=="+") temp->info ="ADD";
else if (arr[i] == "-") temp->info = "SUB";
else if (arr[i] == "*") temp->info = "MUL";
else if (arr[i] == "/") temp->info = "DIV";
else{ cout << "error!" << endl; }
temp->right= q.top();
q.pop();
temp->left= q.top();
q.pop();
q.push(temp);
}
else {
tmpch = "CONST[" + arr[i] + "]";
temp->info = tmpch;
temp->left = NULL;
temp->right = NULL;
q.push(temp);
}
result = temp;
}
PreVisit(result,0); //二叉树打印函数
cout << endl;
}
完整代码及测试样例
(本代码存在一定的出错概率,还请多多指教!)
#include<iostream>
#include<string>
#include<stack>
#define MAXSIZE 50
using namespace std;
typedef string Elemtype;
struct BinaryTreeNode {
string info;
struct BinaryTreeNode * left;
struct BinaryTreeNode * right;
}BTNode;
bool isNum(string s) {
if (s == "ADD[" || s == "SUB[" || s == "MUL[" || s == "DIV[") {
return false;
}
return true;
}
void PreVisit(BinaryTreeNode* root,int cnt) {
if (root != NULL) {
for (int i = 0; i < cnt; i++) {
cout << " ";
}
cout << root->info;
if (root->left != NULL || root->right != NULL) {
cout << "[";
cout << endl;
PreVisit(root->left,cnt+1);
cout << endl;
if (root->right != NULL) {
for (int i = 0; i <= cnt; i++) {
cout << " ";
}
cout << "," << endl;;
PreVisit(root->right,cnt+1);
cout << endl;
}
for (int i = 0; i < cnt; i++) {
cout << " ";
}
cout << "]";
}
}
}
//25 12 13 + * 4 2 - 123 456 + * + =
void PostBiTree() {
int index = 0;
string ch,tmpch,aa,bb;
string arr[100];
stack<BinaryTreeNode*>q;
BinaryTreeNode* result = NULL;
while (cin >> ch) {
if (ch == "=")break;
arr[index++] = ch;
}
for (int i = 0; i < index; i++) {
BinaryTreeNode* temp = new BinaryTreeNode();
if (arr[i] == "+" || arr[i] == "-" || arr[i] == "*" || arr[i] == "/") {
if(arr[i]=="+") temp->info ="ADD";
else if (arr[i] == "-") temp->info = "SUB";
else if (arr[i] == "*") temp->info = "MUL";
else if (arr[i] == "/") temp->info = "DIV";
else{ cout << "error!" << endl; }
temp->right= q.top();
q.pop();
temp->left= q.top();
q.pop();
q.push(temp);
}
else {
tmpch = "CONST[" + arr[i] + "]";
temp->info = tmpch;
temp->left = NULL;
temp->right = NULL;
q.push(temp);
}
result = temp;
}
PreVisit(result,0);
cout << endl;
}
int main() {
PostBiTree();
return 0;
}
待打印的后缀表达式:
25 12 13 + * 4 2 - 123 456 + * + =
测试样例:
三、运行结果
虽然不是很明白但终于搞出来了Orz,还是得好好理解一番。。。其他的测试样例也通过了,这里就不展示了。