急!!!二叉树的创建与遍历,显示有三个错误,因为不熟练,所以求大神解答问题在哪儿!!
#include #include
#include
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild, *rchild;
}BiTNode,*BiTree;
//建立二叉链表
Status CreateBiTree(BiTree &T)
{
TElemType ch;
scanf("%c",&ch);
if (ch==' ')
T= NULL;
else
{
if (!(T = (BiTNode *)malloc(sizeof(BiTNode))))
return OVERFLOW;
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK; } // CreateBiTree
//先序递归遍历
void PreOrder(BiTree T)
{
if(T)
{
printf("%c",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//中序递归遍历
void InOrder(BiTree T)
{
if(T)
{
InOrder(T->lchild);
printf("%c",T->data);
InOrder(T->rchild);
}
}
//后序递归遍历
void PostOrder(BiTree T)
{
if(T)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%c",T->data);
}
}
//先序非递归遍历
status PreIOT(BiTree T,status (*visit)(ElemType e)){
InitStack(S);
p=T;
while (p||!StackEmpty(S)){
Pop(S,p);//访问根节点
if (!visit(p->data)) return ERROR;
else
{
Push(S,p);
p=p->lchild;
}
}
else{
Pop(S,p);
p=p->rchild;
}
}
return OK;
}//IOT
//中序非递归遍历
status InIOT(BiTree T,status (*visit)(ElemType e)){
InitStack(S);
p=T;
while (p||!StackEmpty(S)){
if (p)
{
Push(S,p);
p=p->lchild;
}
}
else{
Pop(S,p);
if (!visit(p->data)) return ERROR;
p=p->rchild;
}
}
return OK;
}//IOT
//后序非递归遍历
status PostIOT(BiTree T,status (*visit)(ElemType e)){
InitStack(S);
p=T;
while (p||!StackEmpty(S)){
if (p)
{
Push(S,p);
p=p->lchild;
}
}
else{
Pop(S,p);
p=p->rchild;
if (!visit(p->data)) return ERROR;
}
}
return OK;
}//IOT
//层序遍历
status LOT(BiTree T,status (*visit)(ElemType e)){
if (!T) return OK;
InitQueue(Q);
EnQueue(Q,T);//若二叉树非空,则根入队
while (!QueueEmpty(Q)){//循环结束条件是队列为空
DeQueue(Q,p);//队头出队,并访问
if (!visit(p->data)) return ERROR;
if (p->lchild) EnQueue(Q,p->lchild);//若当前结点有左子树,则其左子树的根入队
if (p->rchild) EnQueue(Q,p->rchild);//若当前结点有右子树,则其右子树的根入队
}
return OK;
}
void main()
{
BiTree T;
printf("\n 1.先序建立二叉链表:");
printf("\n 2.先序遍历二叉树:");
printf("\n 3.中序遍历二叉树:");
printf("\n 4.后序遍历二叉树:");
printf("\n 5.层序遍历二叉树:");
printf("\n 6.退出");
printf("请选择操作项目:");
scanf("%d",&k);
system("cls");
switch(k)
{
case 1:
printf("请输入要建立的信息(两个空格代表结点为空):\n");
CreateBiTree(T);
system("pause");
break;
case 2:
printf("请输入遍历方式(0代表递归遍历,1代表非递归遍历):");
scanf("%d",&flag);
switch(flag)
{
case 0:
printf("先序递归遍历结果为:\n");
PreOrder(T);
printf("\n");
system("pause");
break;
case 1:
printf("先序非递归遍历结果为:\n");
PreIOT(T,e);
printf("\n");
system("pause");
}
break;
case 3:
printf("请输入遍历方式(0代表递归遍历,1代表非递归遍历):");
scanf("%d",&flag);
switch(flag)
{
case 0:
printf("中序递归遍历结果为:\n");
InOrder(T);
printf("\n");
system("pause");
break;
case 1:
printf("中序非递归遍历结果为:\n");
InIOT(T,e);
printf("\n");
system("pause");
}
break;
case 4:
printf("请输入遍历方式(0代表递归遍历,1代表非递归遍历):");
scanf("%d",&flag);
switch(flag)
{
case 0:
printf("后序递归遍历结果为:\n");
PostOrder(T);
printf("\n");
system("pause");
break;
case 1:
printf("后序非递归遍历结果为:\n");
PostIOT(T,e);
printf("\n");
system("pause");
}
break;
case 5:
printf("层序遍历结果为:\n");
LOT(T,e);
printf("\n");
system("pause");
break;
case 6:break;
default:
printf("选择出错,请重新选择!\n");
system("pause");
}
system("cls");
}
错误:
error C2065: 'OVERFLOW' : undeclared identifier
error C2065: 'OK' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'PreIOT'
error C2501: 'status' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found