主题:新手求高手帮忙!
这是我用C语言编的数据结构程序:目的是通过结构数组来表示二叉树
在编译时说我create_btree 中有非法运算
错误 tree02.c 15: 非法的结构运算在 create_btree 函数中
下面是源程序:请高手帮忙啊!!谢谢了!
#include<stdlib.h>
struct tree
{
int left;
int data;
int right;
};
typedef struct tree treenode;
treenode b_tree[15];
void create_btree(int *b_tree,int *nodelist,int len)
{
int i;
int level;
int position;
b_tree[0].data=nodelist[0];/*以第一个节点为根节点*/
for(i=1;i<len;i++)
{
b_tree[i].data=nodelist[i];
level=0;
position=0;
while(position==0)
{ /*判断是左子树或是右子树*/
if(nodelist[i]>b_tree[level].data)
/*右树是否还有下一阶层*/
if(b_tree[level].right!=-1)
level=b_tree[level].right;
else
position=-1;
else
if(b_tree[level].left!=-1)
level=b_tree[level].left;
else
position=1;
}
if(position==1)
b_tree[level].left=i;
else
b_tree[level].right=i;
}
}
void main()
{
int i,index;
int data;
int nodelist[16];
printf("\nplease input the elements of binary tree(exit for 0):\n");
index=1;
scanf("%d",&data);
while(data!=0)
{
nodelist[index]=data;
index=index+1;
scanf("%d",&data);
}
for(i=0;i<15;i++)
{
b_tree[i].data=0;
b_tree[i].left=-1;
b_tree[i].right=-1;
}
create_btree(nodelist,index)
{
printf("\nthe binary tree content:\n");
printf(" left data right\n");
printf("=======================\n");
for(i=1;i<15;i++)
{
printf("%2d:[%d] [%d] [%d] \n",i,
b_tree[i].left,b_tree[i].data,b_tree[i].right);
}
}
getch();
}
在编译时说我create_btree 中有非法运算
错误 tree02.c 15: 非法的结构运算在 create_btree 函数中
下面是源程序:请高手帮忙啊!!谢谢了!
#include<stdlib.h>
struct tree
{
int left;
int data;
int right;
};
typedef struct tree treenode;
treenode b_tree[15];
void create_btree(int *b_tree,int *nodelist,int len)
{
int i;
int level;
int position;
b_tree[0].data=nodelist[0];/*以第一个节点为根节点*/
for(i=1;i<len;i++)
{
b_tree[i].data=nodelist[i];
level=0;
position=0;
while(position==0)
{ /*判断是左子树或是右子树*/
if(nodelist[i]>b_tree[level].data)
/*右树是否还有下一阶层*/
if(b_tree[level].right!=-1)
level=b_tree[level].right;
else
position=-1;
else
if(b_tree[level].left!=-1)
level=b_tree[level].left;
else
position=1;
}
if(position==1)
b_tree[level].left=i;
else
b_tree[level].right=i;
}
}
void main()
{
int i,index;
int data;
int nodelist[16];
printf("\nplease input the elements of binary tree(exit for 0):\n");
index=1;
scanf("%d",&data);
while(data!=0)
{
nodelist[index]=data;
index=index+1;
scanf("%d",&data);
}
for(i=0;i<15;i++)
{
b_tree[i].data=0;
b_tree[i].left=-1;
b_tree[i].right=-1;
}
create_btree(nodelist,index)
{
printf("\nthe binary tree content:\n");
printf(" left data right\n");
printf("=======================\n");
for(i=1;i<15;i++)
{
printf("%2d:[%d] [%d] [%d] \n",i,
b_tree[i].left,b_tree[i].data,b_tree[i].right);
}
}
getch();
}