
二叉树遍历程序,创建树的时候哪里错误
提示错误的语句是创建树中的t=(btnode*)malloc(sizeof(btnode));错误1errorC2065:“btnode”:未声明的标识符c:\users...
提示错误的语句是创建树中的t=(btnode*)malloc(sizeof(btnode));
错误 1 error C2065: “btnode”: 未声明的标识符 c:\users\administrator\documents\visual studio 2010\projects\s\s\ers.c 15 1 s
错误 2 error C2059: 语法错误:“)” c:\users\administrator\documents\visual studio 2010\projects\s\s\ers.c 15 1 s
#include<stdio.h>
#include<stdlib.h>
typedef struct btnode
{
int
data;
struct
btnode *Lchild,*Rchild;}*bitreptr;
bitreptr
chuangjianshu()
{
int a;
bitreptr t;
scanf_s("%d",&a);
if(a==0)
t=NULL;
else{
t=(btnode*)malloc(sizeof(btnode));//为节点开辟空间
t->data=a;
t->Lchild=chuangjianshu();
t->Rchild=chuangjianshu();}
return(t);}
void
xbianli(bitreptr t)
{if(t)
{printf("%d",t->data);
xbianli(t->Lchild);
xbianli(t->Rchild);}
}
void
zbianli(bitreptr t)
{
if(t)
{zbianli(t->Lchild);
printf("%d",&t);
zbianli(t->Rchild);}
}
void
hbianli(bitreptr t)
{
if(t)
{hbianli(t->Lchild);
hbianli(t->Rchild);
printf("%d",&t);
}}
void
main(void)
{
bitreptr b;
b=chuangjianshu();
printf("先序遍历:");
xbianli(b);
puts("\n");
printf("中序遍历:");
zbianli(b);
puts("\n");
printf("后序遍历:");
hbianli(b);
puts("\n");
} 展开
错误 1 error C2065: “btnode”: 未声明的标识符 c:\users\administrator\documents\visual studio 2010\projects\s\s\ers.c 15 1 s
错误 2 error C2059: 语法错误:“)” c:\users\administrator\documents\visual studio 2010\projects\s\s\ers.c 15 1 s
#include<stdio.h>
#include<stdlib.h>
typedef struct btnode
{
int
data;
struct
btnode *Lchild,*Rchild;}*bitreptr;
bitreptr
chuangjianshu()
{
int a;
bitreptr t;
scanf_s("%d",&a);
if(a==0)
t=NULL;
else{
t=(btnode*)malloc(sizeof(btnode));//为节点开辟空间
t->data=a;
t->Lchild=chuangjianshu();
t->Rchild=chuangjianshu();}
return(t);}
void
xbianli(bitreptr t)
{if(t)
{printf("%d",t->data);
xbianli(t->Lchild);
xbianli(t->Rchild);}
}
void
zbianli(bitreptr t)
{
if(t)
{zbianli(t->Lchild);
printf("%d",&t);
zbianli(t->Rchild);}
}
void
hbianli(bitreptr t)
{
if(t)
{hbianli(t->Lchild);
hbianli(t->Rchild);
printf("%d",&t);
}}
void
main(void)
{
bitreptr b;
b=chuangjianshu();
printf("先序遍历:");
xbianli(b);
puts("\n");
printf("中序遍历:");
zbianli(b);
puts("\n");
printf("后序遍历:");
hbianli(b);
puts("\n");
} 展开
1个回答
展开全部
你这个是递归生成二叉树吧?话说你的递归函数,怎么没有参数?
/**
* 层序生成二叉树
*/
BinTree *CreateBinTree() {
BinTree_Type Data;
BinTree *BT = NULL, *T = NULL;
Queue * Q = NULL;
Q = InitQueue(); /* 生成一个队列 */
scanf("%d", &Data); /* 建立第一个结点,即根节点 */
if (Data) { /* 分配结点单元,并将结点地址入队 */
BT = (BinTree *) malloc(sizeof(BinTree));
BT->Data = Data;
EnQueue(Q, BT);
} else {
return NULL; /* 若第一个数据就是0,则返回空树 */
}
while (!IsQueueEmpty(Q)) {
T = DeQueue(Q); /* 从队列中取出一结点地址 */
scanf("%d", &Data); /* 读入T的左孩子 */
if (Data) { /* 分配新结点,作为出对结点左孩子 */
T->Left = (BinTree *) malloc(sizeof(BinTree));
T->Left->Data = Data;
EnQueue(Q, T->Left); /* 新结点入队 */
} else {
T->Left = NULL;
}
scanf("%d", &Data); /* 读入T的右孩子 */
if (Data) { /* 分配新结点,作为出对结点右孩子 */
T->Right = (BinTree *) malloc(sizeof(BinTree));
T->Right->Data = Data;
EnQueue(Q, T->Right); /* 新结点入队 */
} else {
T->Right = NULL;
}
} /* 结束while */
return BT;
}
这是层序生成的二叉树的算法!如输入0代表结点为空。
同时你也可以直接百度搜索“C实现二叉树(模块化集成,遍历的递归与非递归实现)”,这是博客园的一个博文,里面有关二叉树的前中后层遍历的递归与非递归算法,比较全面。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询