主题:二叉树的遍历
#i nclude "stdio.h"
#define maxsize 128
typedef char datatype;
typedef struct node
{datatype data;
struct node *lchild,*rchild;
} bitree;
bitree *Q[maxsize];
bitree *CREATREE();
PREORDER(bitree *t);
INORDER(bitree *t);
POSTORDER(bitree *t);
main()
{
bitree *BTroot;
printf("\n");
BTroot=CREATREE();
printf("\npreorder travelsal of binary tree:\n");
PREORDER(BTroot);
printf("\ninorder travelsal of binary tree:\n");
INORDER(BTroot);
printf("\npostorder travelsal of binary tree:\n");
POSTORDER(BTroot);
}
bitree *CREATREE()
{char ch;
int front,rear;
bitree *root,*s;
root=NULL;
front=1;rear=0;
ch=getchar();getchar();
while (ch!='#')
{s=NULL;
if(ch!='@')
{
s=malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1) root=s;
else
{if (s && Q[front])
if(rear%2==0)
Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1) front++;
}
ch=getchar();getchar();
}
return(root);
}
INORDER(t)
bitree *t;
{
if(t)
{
INORDER(t->lchild);
printf("\t%c\n,",t->data);
INORDER(t->rchild);
}
}
PREORDER(t)
bitree *t;
{
if(t)
{
printf("%c,",t->data);
PREORDER(t->lchild);
PREORDER(t->rchild);
}
}
POSTORDER(t)
bitree *t;
{
if(t)
{
POSTORDER(t->lchild);
POSTORDER(t->rchild);
printf("%c,",t->data);
}
}
有一个错误
谁帮我调试一下
谢谢!!!
#define maxsize 128
typedef char datatype;
typedef struct node
{datatype data;
struct node *lchild,*rchild;
} bitree;
bitree *Q[maxsize];
bitree *CREATREE();
PREORDER(bitree *t);
INORDER(bitree *t);
POSTORDER(bitree *t);
main()
{
bitree *BTroot;
printf("\n");
BTroot=CREATREE();
printf("\npreorder travelsal of binary tree:\n");
PREORDER(BTroot);
printf("\ninorder travelsal of binary tree:\n");
INORDER(BTroot);
printf("\npostorder travelsal of binary tree:\n");
POSTORDER(BTroot);
}
bitree *CREATREE()
{char ch;
int front,rear;
bitree *root,*s;
root=NULL;
front=1;rear=0;
ch=getchar();getchar();
while (ch!='#')
{s=NULL;
if(ch!='@')
{
s=malloc(sizeof(bitree));
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1) root=s;
else
{if (s && Q[front])
if(rear%2==0)
Q[front]->lchild=s;
else
Q[front]->rchild=s;
if(rear%2==1) front++;
}
ch=getchar();getchar();
}
return(root);
}
INORDER(t)
bitree *t;
{
if(t)
{
INORDER(t->lchild);
printf("\t%c\n,",t->data);
INORDER(t->rchild);
}
}
PREORDER(t)
bitree *t;
{
if(t)
{
printf("%c,",t->data);
PREORDER(t->lchild);
PREORDER(t->rchild);
}
}
POSTORDER(t)
bitree *t;
{
if(t)
{
POSTORDER(t->lchild);
POSTORDER(t->rchild);
printf("%c,",t->data);
}
}
有一个错误
谁帮我调试一下
谢谢!!!