源代碼:
#include
#include
#include
#define TElemtype char
#define Status int
#define OK 1
#define ERROR 0
typedef struct BiTNode
{
TElemtype data;
struct BiTNode lchild,*rchild;
}BiTNode;
typedef BiTNode BiTree;
Status CreateBiTree(BiTree & T)
{
TElemtype temp;
scanf("%c",&temp);
if(temp=='0')
{
T=NULL;
return OK;
}
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
{
printf("NO MEMORY!\n");
return ERROR;
}
T->data=temp;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
Status PreOrderTraverse(BiTree T)
{
if(T)
{
printf("%c\n",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
else return ERROR;
return OK;
}
/*Status InOrderTraverse(BiTree *T)
{
}*/
int main()
{
printf("Begin to input character:\n");
BiTree T;
CreateBiTree(T);
printf("OK\n");
PreOrderTraverse(T);
getch();
return 0;
}
總是只能不停地輸入,無法結束
麻煩各位看一下,謝謝!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define TElemtype char
#define Status int
#define OK 1
#define ERROR 0
typedef struct BiTNode
{
TElemtype data;
struct BiTNode *lchild,*rchild;
}BiTNode;
typedef BiTNode* BiTree;
Status CreateBiTree(BiTree & T)
{
TElemtype temp;
scanf("%c",&temp);
if(temp=='0')
{
T=NULL;
return OK;
}
else
{
T=(BiTree)malloc(sizeof(BiTNode));
if(!T)
{
printf("NO MEMORY!\n");
return ERROR;
}
T->data=temp;
T->lchild=NULL; //添加
T->rchild=NULL; //添加
if (CreateBiTree(T->lchild) == OK) return OK; //修改
if (CreateBiTree(T->rchild) == OK) return OK; //修改
}
return OK;
}
Status PreOrderTraverse(BiTree T)
{
if(T)
{
printf("%c\n",T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
else return ERROR;
return OK;
}
/*Status InOrderTraverse(BiTree *T)
{
}*/
int main()
{
printf("Begin to input character:\n");
BiTree T;
CreateBiTree(T);
printf("OK\n");
PreOrderTraverse(T);
getch();
return 0;
}