表达式树的创建和输出#include <bits/stdc++.h> using namespace std; string op; typedef char TElemType; typedef struct BiNode { TElemType data; struct BiNode lchild,rchild; } BiNode,BiTree; void CreateBiTree(BiTree &T) //建立二叉树 { cin >> op; int i = 0,s = 0,len = 0; if(op[0]=='#') T = NULL; else { if(op[0]=='+') s = -1; else if(op[0]=='-') s = -2; else if(op[0]=='') s = -3; else if(op[0]=='/') s = -4; else { len=op.size(); while(i<len) { s = s10+(op[i]-'0'); i++; } } T = new BiNode; T->data = s; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } } void Copy(BiTree &T) { if(T) { if(T->data >0) cout << T->data; else { cout << "("; Copy(T->lchild); if(T->data==-1) cout << "+"; else if(T->data==-2) cout << "-"; else if(T->data==-3) cout << ""; else if(T->data==-4) cout << "/"; Copy(T->rchild); cout << ")"; } } } int main() { BiTree T; while(cin >> op) { int i=0,s=0,len=0; if(op[0]=='+') s = -1; else if(op[0]=='-') s = -2; else if(op[0]=='') s = -3; else if(op[0]=='/') s = -4; else { len = op.size(); while(i < len) { s = s10+(op[i]-'0'); i++; } } T = new BiNode; T->data = s; CreateBiTree(T->lchild); CreateBiTree(T->rchild); Copy(T); cout << endl; } return 0; }的错误
时间: 2024-03-23 20:41:27 浏览: 110
在这份代码中,结构体 `BiNode` 中的 `lchild` 和 `rchild` 成员应该是指向 `BiNode` 类型的指针,而不是直接定义为 `BiNode` 类型。因此,结构体应该改为:
```c++
typedef struct BiNode {
TElemType data;
struct BiNode *lchild, *rchild; // 改为指针类型
} BiNode, *BiTree;
```
此外,代码中的四个判断操作符的字符常量不正确,应该分别为 `'+'`、`'-'`、`'*'` 和 `'/'`。因此,建立二叉树的部分代码应该改为:
```c++
if (op[0] == '+') s = -1;
else if (op[0] == '-') s = -2;
else if (op[0] == '*') s = -3;
else if (op[0] == '/') s = -4;
```
另外,函数 `Copy()` 中应该先递归调用 `Copy()` 处理左子树,再处理右子树,而不是先处理右子树。因此,`Copy()` 函数中的代码应该改为:
```c++
if (T) {
if (T->data > 0) cout << T->data;
else {
cout << "(";
Copy(T->lchild);
if (T->data == -1) cout << "+";
else if (T->data == -2) cout << "-";
else if (T->data == -3) cout << "*";
else if (T->data == -4) cout << "/";
Copy(T->rchild); // 先处理左子树,再处理右子树
cout << ")";
}
}
```
最后,由于 `CreateBiTree()` 函数中的 `T` 形参是指针类型,因此调用该函数时需要传入指向指针的指针。因此,建立二叉树的代码应该改为:
```c++
T = new BiNode;
T->data = s;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
```
修改后的完整代码如下:
```c++
#include <bits/stdc++.h>
using namespace std;
string op;
typedef char TElemType;
typedef struct BiNode {
TElemType data;
struct BiNode *lchild, *rchild;
} BiNode, *BiTree;
void CreateBiTree(BiTree &T) {
cin >> op;
int i = 0, s = 0, len = 0;
if (op[0] == '#') T = NULL;
else {
if (op[0] == '+') s = -1;
else if (op[0] == '-') s = -2;
else if (op[0] == '*') s = -3;
else if (op[0] == '/') s = -4;
else {
len = op.size();
while (i < len) {
s = s * 10 + (op[i] - '0');
i++;
}
}
T = new BiNode;
T->data = s;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void Copy(BiTree &T) {
if (T) {
if (T->data > 0) cout << T->data;
else {
cout << "(";
Copy(T->lchild);
if (T->data == -1) cout << "+";
else if (T->data == -2) cout << "-";
else if (T->data == -3) cout << "*";
else if (T->data == -4) cout << "/";
Copy(T->rchild);
cout << ")";
}
}
}
int main() {
BiTree T;
while (cin >> op) {
int i = 0, s = 0, len = 0;
if (op[0] == '+') s = -1;
else if (op[0] == '-') s = -2;
else if (op[0] == '*') s = -3;
else if (op[0] == '/') s = -4;
else {
len = op.size();
while (i < len) {
s = s * 10 + (op[i] - '0');
i++;
}
}
T = new BiNode;
T->data = s;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
Copy(T);
cout << endl;
}
return 0;
}
```
阅读全文