猿问

想要构造二叉树函数及遍历,以下操作该怎么修改吗?

//主要是验证下采用递推的算法对二叉树进行遍历:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//二叉树的表示:
#define error -1
#define ok 1

typedef struct BiTreeNode
{
//结点元素的数值:
char data;
struct BiTreeNode *lchild,*rchild;

}Bnode,*Btree;
//初始化一个二叉树,
int creatree(Btree T);
//对二叉树树结点的操作函数:
int print(char e);
//遍历二叉树结点:先序遍历法
int preoder(Btree T,int(*vist)(char));
//遍历二叉树结点:中序遍历法
int inorder(Btree T,int(*vist)(char));
//遍历二叉树结点:后序遍历法
int postorder(Btree T,int(*vist)(char));
//主函数:
int main(int argc,char *argv[])
{
Btree R;int i,j;R=(Btree)malloc(sizeof(Bnode));
i=creatree(R);
printf("if i=1;表示初始化成功:%d\n",i);
j=preoder(R,print);
printf("if j=1;表示初始化成功:%\n",j);
printf("\n");
inorder(R,print);
printf("\n");
postorder(R,print);
return 0;
}
//初始化一个二叉树:
int creatree(Btree T)
{
//按先后顺序输入二叉树结点值,空格表示空树:
char ch;
printf("输入字符\n");
scanf("%c",&ch);
if(ch==' ')
T=NULL;
else
{

if(!(T=(Bnode*)malloc(sizeof(Bnode))))
return error;
T->data=ch;
creatree(T->lchild);
creatree(T->rchild);

}
return ok;
}
//
int print(char e)
{
printf("%c\n",e);
return ok;
}
//
int preoder(Btree T,int(*vist)(char))
{
if(T)
{
vist(T->data);
preoder(T->lchild,vist);
preoder(T->rchild,vist);
return ok;
}
else
return error;
}
//
int inorder(Btree T,int(*vist)(char))
{
if(T)
{
inorder(T->lchild,vist);
vist(T->data);
inorder(T->rchild,vist);
return ok;
}
else
return error;

}
int postorder(Btree T,int(*vist)(char))
{
if(T)
{
postorder(T->lchild,vist);
postorder(T->rchild,vist);
vist(T->data);
return ok;
}
else
return error;
}
怎么这个函数调试不出来,,在vc调试下,,卡在这里:
vist(T->data);

慕无忌1623718
浏览 161回答 2
2回答

手掌心

程序写多了,以下那些是多余的,定义函数,你下面并没有具体程序。//初始化一个二叉树,int creatree(Btree T);//对二叉树树结点的操作函数:int print(char e);//遍历二叉树结点:先序遍历法int preoder(Btree T,int(*vist)(char));//遍历二叉树结点:中序遍历法int inorder(Btree T,int(*vist)(char));//遍历二叉树结点:后序遍历法int postorder(Btree T,int(*vist)(char));而且注释部分是用 /* */ 来实现的,要不然系统认为你的注释也是程序的部分。

四季花海

//初始化一个二叉树,int creatree(Btree T);//对二叉树树结点的操作函数:int print(char e);//遍历二叉树结点:先序遍历法int preoder(Btree T,int(*vist)(char));//遍历二叉树结点:中序遍历法int inorder(Btree T,int(*vist)(char));//遍历二叉树结点:后序遍历法int postorder(Btree T,int(*vist)(char));
随时随地看视频慕课网APP
我要回答