#include <bits/stdc++.h> using namespace std; typedef string ElemType; typedef struct BitNode { ElemType data; struct BitNode *lchild,*rchild; } BitNode,*BiTree; //char sc[10]= {"+-*/"}; void CreateBiTree(BiTree &t) { string ch; cin >> ch; if(ch[0] == '#') t = NULL; else { t = new BitNode; t->data = ch; CreateBiTree(t->lchild); CreateBiTree(t->rchild); } } void InOrderTraverse(BiTree t) { if(t) { string op = t->data; if(op[0]<'0' || op[0]>'9') cout << '('; InOrderTraverse(t->lchild); cout << t->data ; InOrderTraverse(t->rchild); if(op[0]<'0' || op[0]>'9') cout << ')'; } /***********************************/ } int main() { BiTree tree = new BitNode; string ch; while(cin >> ch) { tree->data = ch; CreateBiTree(tree->lchild); CreateBiTree(tree->rchild); InOrderTraverse(tree); printf("\n"); } return 0; }用c语言实现
时间: 2024-03-17 18:46:46 浏览: 82
这段代码是C++语言实现的,不是C语言。如果想要在C语言中实现,可以参考下面的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char ElemType;
typedef struct BitNode {
ElemType data;
struct BitNode *lchild, *rchild;
} BitNode, *BiTree;
void CreateBiTree(BiTree *t)
{
char ch;
scanf("%c", &ch);
if (ch == '#') {
*t = NULL;
} else {
*t = (BitNode *)malloc(sizeof(BitNode));
(*t)->data = ch;
CreateBiTree(&((*t)->lchild));
CreateBiTree(&((*t)->rchild));
}
}
void InOrderTraverse(BiTree t)
{
if (t) {
char op = t->data;
if (op < '0' || op > '9')
printf("(");
InOrderTraverse(t->lchild);
printf("%c", t->data);
InOrderTraverse(t->rchild);
if (op < '0' || op > '9')
printf(")");
}
}
int main()
{
BiTree tree;
while (scanf("%c", &(tree->data)) != EOF) {
CreateBiTree(&(tree->lchild));
CreateBiTree(&(tree->rchild));
InOrderTraverse(tree);
printf("\n");
}
return 0;
}
```
阅读全文