- 论坛徽章:
- 0
|
这是一个创建二叉树并中序遍历的程序,可是却是错的,
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef int Status;
typedef int TElemType;
typedef struct btnode
{ TElemType data;
struct btnode *lchild,*rchild;
}btnode,*bitree;
void Create(bitree T)
{
int ch;
scanf("%d",&ch);
if(ch==0)
T=NULL;
else{
T=(bitree)malloc(sizeof(btnode));
T->data=ch;
Create(T->lchild);
Create( T->rchild);
}
}
int InOrder(bitree T)
{
if(T)
{
InOrder( T->lchild);
printf("%d ",T->data);
InOrder( T->rchild);
return 1;
} return 0;
}
main()
{bitree bhead;
Create(bhead);
InOrder(bhead);
getch();
}
而这样改的话就会对了
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef int Status;
typedef int TElemType;
typedef struct btnode
{ TElemType data;
struct btnode *lchild,*rchild;
}btnode,*bitree;
bitree Create()
{ bitree T;
int ch;
scanf("%d",&ch);
if(ch==0)
T=NULL;
else{
T=(bitree)malloc(sizeof(btnode));
T->data=ch;
T->lchild=Create();
T->rchild=Create();
} return T;
}
int InOrder(bitree T)
{
if(T)
{
InOrder( T->lchild);
printf("%d ",T->data);
InOrder( T->rchild);
return 1;
} return 0;
}
main()
{bitree bhead;
bhead=Create();
InOrder(bhead);
getch();
} |
|