//主要是验证下采用递推的算法对二叉树进行遍历:
#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);
手掌心
四季花海