慕無忌1623718
2022-03-18 11:11:37
//主要是驗證下采用遞推的算法對二叉樹進行遍歷:#include<stdio.h>#include<stdlib.h>#include<malloc.h>//二叉樹的表示:#define error -1#define ok 1typedef 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;}elsereturn error;}//int inorder(Btree T,int(*vist)(char)){if(T){inorder(T->lchild,vist);vist(T->data);inorder(T->rchild,vist);return ok;}elsereturn error;}int postorder(Btree T,int(*vist)(char)){if(T){postorder(T->lchild,vist);postorder(T->rchild,vist);vist(T->data);return ok;}elsereturn error;}怎么這個函數調試不出來,,在vc調試下,,卡在這里:vist(T->data);
2 回答

手掌心
TA貢獻1942條經驗 獲得超3個贊
程序寫多了,以下那些是多余的,定義函數,你下面并沒有具體程序。
//初始化一個二叉樹,
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));
而且注釋部分是用 /* */ 來實現的,要不然系統認為你的注釋也是程序的部分。

四季花海
TA貢獻1811條經驗 獲得超5個贊
//初始化一個二叉樹,
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));
添加回答
舉報
0/150
提交
取消