亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請大神幫忙看看以下的程序,并進行指點哈,謝謝了

請大神幫忙看看以下的程序,并進行指點哈,謝謝了

MM們 2023-04-02 21:17:43
要求:按先序序列輸入一些字母,建立一個二叉樹,然后按中序輸出個結點的字母,再求出樹的高度和葉子結點數并打印葉子結點。如輸入:abc##de#g##f###中序輸出為cbegdfa#include<stdio.h>#include<malloc.h>typedef int Status;typedef char TElemType;typedef struct BiTNode{TElemType data;struct BiTNode *lchild,*rchild;}BiTNode;//結點定義//BiTree root;//根結點定義//構造二叉樹Status BiTree CreateBiTree(BiTree &T){//按先序輸入個接點的值,空格字符#表示空樹//構造二叉鏈表表示的二叉樹Tchar ch;printf("輸入接點字符");scanf("%c",&ch);if(ch=='#')T=NULL;else{if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))return ERROR;T->data=ch;//生成根接點CreateBiTree(T->lchild); //構造左子樹CreateBiTree(T->rchild); //構造右子樹}return T;}//CreateBinTree//中序訪問并輸出各接點字符Status INORDER(BiTree *T){if(T){INORDER(T->lchild); //中序遍歷左子樹printf("\t%c\n",T->data); //訪問接點*tINORDER(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 Leaf_Count(T->lchild)+Leaf_Count(T->rchild);//左子樹的葉子數加上右子樹的葉子數}//LeafCount_BiTreevoid main(){BiTree root;/*根結點指針定義*/ CreateBiTree(&root);/*傳入根結點指針的地址在函數中指向二叉樹根*/ INORDER(root);depth(BiTree T)LeafCount_BiTree(Bitree T)}
查看完整描述

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
*/


查看完整回答
反對 回復 2023-04-05
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

1、BiTree類型沒有定義
2、CreateBiTree的返回類型寫了兩個(Status和BiTree)
3、CreateBiTree的參數定義的是BiTree的引用類型,但main里面傳的是BiTree的指針類型。
4、既然CreateBiTree返回的是Status類型,你最后一句return T;就不對了。
5、LeafCount_BiTree里兩個遞歸調用把函數名字寫錯了。

查看完整回答
反對 回復 2023-04-05
  • 2 回答
  • 0 關注
  • 214 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號