主题:[讨论]请大家看一眼我的二叉树程序
运行时老是出出现“应用程序错误”就是那个弹出的那个窗口,下面是我的程序:
#include <stdlib.h>
#include <stdio.h>
typedef struct Bitree{
char data;
struct Bitree * lchild , * rchild;
}BiTNode;
void CreateBitree(BiTNode *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 preorder(BiTNode *root)
{
if(root!=NULL){
printf("%c ",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(BiTNode *root)
{
if(root!=NULL){
inorder(root->lchild);
printf("%c ",root->data);
inorder(root->rchild);
}
}
void postorder(BiTNode *root)
{
if(root!=NULL){
postorder(root->lchild);
postorder(root->rchild);
printf("%c ",root->data);
}
}
int main(void)
{
BiTNode T;
BiTNode *p;
p=&T;
printf("***************************创建二叉树!****************************\n");
printf("Please input some data end of the character'@' as the end:\n");
CreateBitree(p);
printf("先序遍历的结果为:\n");
preorder(p);
printf("中序遍历的结果为:\n");
inorder(p);
printf("后序遍历的结果为:\n");
postorder(p);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
typedef struct Bitree{
char data;
struct Bitree * lchild , * rchild;
}BiTNode;
void CreateBitree(BiTNode *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 preorder(BiTNode *root)
{
if(root!=NULL){
printf("%c ",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(BiTNode *root)
{
if(root!=NULL){
inorder(root->lchild);
printf("%c ",root->data);
inorder(root->rchild);
}
}
void postorder(BiTNode *root)
{
if(root!=NULL){
postorder(root->lchild);
postorder(root->rchild);
printf("%c ",root->data);
}
}
int main(void)
{
BiTNode T;
BiTNode *p;
p=&T;
printf("***************************创建二叉树!****************************\n");
printf("Please input some data end of the character'@' as the end:\n");
CreateBitree(p);
printf("先序遍历的结果为:\n");
preorder(p);
printf("中序遍历的结果为:\n");
inorder(p);
printf("后序遍历的结果为:\n");
postorder(p);
return 0;
}