主题:[讨论]大家看看我的建立树和层次遍历的代码运行有3个错误怎么改
#include<Stdio.h>
#include<Conio.h>
#include<Malloc.h>
typedef struct bitree
{char data;
struct bitree *left;
struct bitree *right;
}BITREENODE,*PBITREENODE;
int n;
PBITREENODE BiTree=NULL;
/* 声名函数原型 */
void create_btree4(int *A,int size)
void levelorder(PBITREENODE pnode)
void levelorder(PBITREENODE pnode)
{
PBITREENODE ptr,Tree[n];
int i,front=-1,rear=0;
for(i=0;i<n;i++)
Tree[i]=NULL;
ptr=poode;
while(ptr!=NULL)
{
printf("%d",ptr->data);
if(ptr->left!=NULL)
Tree[rear++]=ptr->left;
if(ptr->right!=NULL
Tree[rear++]=ptr->right;
if(front!=NULL)
ptr=Tree[++front];
}
}
void create_btree4(int *A,int size)
{
int i; PBITREENODE ptr1,ptr2;
for(i=0;i<size;i++)
{
if(BiTree=NULL)
{
BiTree=(PBITREENODE)malloc(sizeof(BITREENODE))
if(BiTree==NULL)
{printf(malloc fail);break;}
BiTree->data=A[i];BiTree->left=NULL;BiTree->right=NULL;
continue;
}
else ptr=BiTree;
while(1)
{
if(A[1]>ptr->data)
{
if(ptr->right==NULL)
{
ptr2=(PBITREENODE)malloc(sizeof(BITREENODE));
if(ptr2==NULL)
{printf(malloc fail);break;}
ptr2->data=A[i];ptr2->left=NULL;ptr2->right=NULL;ptr->=ptr2;break;
}
else ptr=ptr->right;
}
else
{
if(ptr->left==NULL)
{
ptr2=(PBITREENODE)malloc(sizeof(BITREENODE));
if(ptr2==NULL)
{printf(malloc fail);break;}
ptr2->data=A[i];ptr2->left=NULL;ptr2->right=NULL;ptr->=ptr2;break;
}
else ptr=ptr->left;
}
}
}
}
main()
{
int i; int A[7]={5,3,7,2,9,4,6};
printf("input the data:");
for(i=0;i<7;i++)
printf("%d",A[i]);
create_btree4(A,7);
PBITREENODE Tree[6];
Tree[0]=BiTree;
Tree[1]=Tree[0]->left;
Tree[2]=Tree[0]->right;
Tree[3]=Tree[1]->left;
Tree[4]=Tree[1]->right;
Tree[5]=Tree[2]->left;
Tree[6]=Tree[2]->right;
printf("\n Binary Tree:\n");
printf(" %d\n",Tree[0]->data);
printf(" |\n");
printf(" --+--\n");
printf(" | |\n");
printf(" %d %d\n",Tree[1]->data,Tree[2]->data);
printf(" | |\n");
printf(" ---+-- --+--\n ");
printf(" | | | |\n");
printf(" %d %d %d %d\n",Tree[3]->data,Tree[4]->data,Tree[5]->data,Tree[6]->data);
printf("\n\n");
printf("\n Tree levelorder traversal:");
levelorder(PBITREENODE Tree);
}
#include<Conio.h>
#include<Malloc.h>
typedef struct bitree
{char data;
struct bitree *left;
struct bitree *right;
}BITREENODE,*PBITREENODE;
int n;
PBITREENODE BiTree=NULL;
/* 声名函数原型 */
void create_btree4(int *A,int size)
void levelorder(PBITREENODE pnode)
void levelorder(PBITREENODE pnode)
{
PBITREENODE ptr,Tree[n];
int i,front=-1,rear=0;
for(i=0;i<n;i++)
Tree[i]=NULL;
ptr=poode;
while(ptr!=NULL)
{
printf("%d",ptr->data);
if(ptr->left!=NULL)
Tree[rear++]=ptr->left;
if(ptr->right!=NULL
Tree[rear++]=ptr->right;
if(front!=NULL)
ptr=Tree[++front];
}
}
void create_btree4(int *A,int size)
{
int i; PBITREENODE ptr1,ptr2;
for(i=0;i<size;i++)
{
if(BiTree=NULL)
{
BiTree=(PBITREENODE)malloc(sizeof(BITREENODE))
if(BiTree==NULL)
{printf(malloc fail);break;}
BiTree->data=A[i];BiTree->left=NULL;BiTree->right=NULL;
continue;
}
else ptr=BiTree;
while(1)
{
if(A[1]>ptr->data)
{
if(ptr->right==NULL)
{
ptr2=(PBITREENODE)malloc(sizeof(BITREENODE));
if(ptr2==NULL)
{printf(malloc fail);break;}
ptr2->data=A[i];ptr2->left=NULL;ptr2->right=NULL;ptr->=ptr2;break;
}
else ptr=ptr->right;
}
else
{
if(ptr->left==NULL)
{
ptr2=(PBITREENODE)malloc(sizeof(BITREENODE));
if(ptr2==NULL)
{printf(malloc fail);break;}
ptr2->data=A[i];ptr2->left=NULL;ptr2->right=NULL;ptr->=ptr2;break;
}
else ptr=ptr->left;
}
}
}
}
main()
{
int i; int A[7]={5,3,7,2,9,4,6};
printf("input the data:");
for(i=0;i<7;i++)
printf("%d",A[i]);
create_btree4(A,7);
PBITREENODE Tree[6];
Tree[0]=BiTree;
Tree[1]=Tree[0]->left;
Tree[2]=Tree[0]->right;
Tree[3]=Tree[1]->left;
Tree[4]=Tree[1]->right;
Tree[5]=Tree[2]->left;
Tree[6]=Tree[2]->right;
printf("\n Binary Tree:\n");
printf(" %d\n",Tree[0]->data);
printf(" |\n");
printf(" --+--\n");
printf(" | |\n");
printf(" %d %d\n",Tree[1]->data,Tree[2]->data);
printf(" | |\n");
printf(" ---+-- --+--\n ");
printf(" | | | |\n");
printf(" %d %d %d %d\n",Tree[3]->data,Tree[4]->data,Tree[5]->data,Tree[6]->data);
printf("\n\n");
printf("\n Tree levelorder traversal:");
levelorder(PBITREENODE Tree);
}