2 回答

TA貢獻1873條經驗 獲得超9個贊
#include<stdio.h>
#include<malloc.h>
#include"stdlib.h"
typedef int Status;
typedef char TElemType;
typedef struct BiTNode{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;//結點定義
//BiTree root;//根結點定義
//構造二叉樹
BiTree CreateBiTree(BiTree T)
{
//按先序輸入個接點的值,空格字符 表示空樹
//構造二叉鏈表表示的二叉樹T
char ch;
scanf("%c",&ch);
if(ch!=EOF&&ch!='\n')
{if(ch==' ') T=NULL;
else
{
T=(BiTree)malloc(sizeof(struct BiTNode));
if(!T) exit(0);
T->data=ch; //生成根接點
T->lchild=CreateBiTree(T->lchild);//構造左子樹
T->rchild=CreateBiTree(T->rchild);//構造右子樹
} }
return(T);
}
//中序訪問并輸出各接點字符
void INORDER(BiTree T){
if(T)
{INORDER(T->lchild); //中序遍歷左子樹
printf("%c\t",T->data); //訪問接點*t
INORDER(T->rchild); //中序遍歷右子樹
}
}
//計算樹的高度
int depth(BiTree T){
int dep1,dep2;
if(T==NULL)return(0);
else
{dep1=depth(T->lchild);
dep2=depth(T->lchild);
if(dep1>dep2)return(dep1+1);
else return(dep2+1);}
}
int LeafCount_BiTree(BiTree T)//求二叉樹中葉子結點的數目
{
if(!T) return 0; //空樹沒有葉子
else if(!T->lchild&&!T->rchild) return 1; //葉子結點
else return LeafCount_BiTree(T->lchild)+LeafCount_BiTree(T->rchild);//左子樹的葉子數加上右子樹的葉子數
}//LeafCount_BiTree
void main(){
struct BiTNode root,*p;/*根結點指針定義*/
printf("\nplease input the treestring you want create :\n");
p=CreateBiTree(&root);/*傳入根結點指針的地址在函數中指向二叉樹根*/
INORDER(p);
depth(p);
LeafCount_BiTree(p);
}
測試通過:
運行結果為:// 這里輸入時按照你的abc##de#g##f### ,注意空格的輸入
/*
please input the treestring you want create :
abc de g f
c b e g d f a
*/

TA貢獻1817條經驗 獲得超14個贊
1、BiTree類型沒有定義
2、CreateBiTree的返回類型寫了兩個(Status和BiTree)
3、CreateBiTree的參數定義的是BiTree的引用類型,但main里面傳的是BiTree的指針類型。
4、既然CreateBiTree返回的是Status類型,你最后一句return T;就不對了。
5、LeafCount_BiTree里兩個遞歸調用把函數名字寫錯了。
添加回答
舉報