??
二叉樹(shù)子系統(tǒng) - 免費(fèi)下載
源碼資源
文件大小:2 K
?? 資源詳細(xì)信息
??
上傳時(shí)間
?? 溫馨提示:本資源由用戶(hù) ccccy 上傳分享,僅供學(xué)習(xí)交流使用。如有侵權(quán),請(qǐng)聯(lián)系我們刪除。
?? 資源簡(jiǎn)介
#include<stdio.h>
#define TREEMAX 100
typedef struct BT
{
char data;
BT *lchild;
BT *rchild;
}BT;
BT *CreateTree();
void Preorder(BT *T);
void Postorder(BT *T);
void Inorder(BT *T);
void Leafnum(BT *T);
void Nodenum(BT *T);
int TreeDepth(BT *T);
int count=0;
void main()
{
BT *T=NULL;
char ch1,ch2,a;
ch1='y';
while(ch1=='y'||ch1=='y')
{
printf("\n");
printf("\n\t\t 二叉樹(shù)子系統(tǒng)");
printf("\n\t\t*****************************************");
printf("\n\t\t 1---------建二叉樹(shù) ");
printf("\n\t\t 2---------先序遍歷 ");
printf("\n\t\t 3---------中序遍歷 ");
printf("\n\t\t 4---------后序遍歷 ");
printf("\n\t\t 5---------求葉子數(shù) ");
printf("\n\t\t 6---------求結(jié)點(diǎn)數(shù) ");
printf("\n\t\t 7---------求樹(shù)深度 ");
printf("\n\t\t 0---------返 回 ");
printf("\n\t\t*****************************************");
printf("\n\t\t 請(qǐng)選擇菜單號(hào) (0--7)");
scanf("%c",&ch2);
getchar();
printf("\n");
switch(ch2)
{
case'1':
printf("\n\t\t請(qǐng)按先序序列輸入二叉樹(shù)的結(jié)點(diǎn):\n");
printf("\n\t\t說(shuō)明:輸入結(jié)點(diǎn)(‘0’代表后繼結(jié)點(diǎn)為空)后按回車(chē)。\n");
printf("\n\t\t請(qǐng)輸入根結(jié)點(diǎn):");
T=CreateTree();
printf("\n\t\t二叉樹(shù)成功建立!\n");break;
case'2':
printf("\n\t\t該二叉樹(shù)的先序遍歷序列為:");
Preorder(T);break;
case'3':
printf("\n\t\t該二叉樹(shù)的中序遍歷序列為:");
Inorder(T);break;
case'4':
printf("\n\t\t該二叉樹(shù)的后序遍歷序列為:");
Postorder(T);break;
case'5':
count=0;Leafnum(T);
printf("\n\t\t該二叉樹(shù)有%d個(gè)葉子。\n",count);break;
case'6':
count=0;Nodenum(T);
printf("\n\t\t該二叉樹(shù)總共有%d個(gè)結(jié)點(diǎn)。\n",count);break;
case'7':
printf("\n\t\t該樹(shù)的深度為:%d",TreeDepth(T));
break;
case'0':
ch1='n';break;
default:
printf("\n\t\t***請(qǐng)注意:輸入有誤!***");
}
if(ch2!='0')
{
printf("\n\n\t\t按【Enter】鍵繼續(xù),按任意鍵返回主菜單!\n");
a=getchar();
if(a!='\xA')
{
getchar();
ch1='n';
}
}
}
}
BT *CreateTree()
{
BT *t;
char x;
scanf("%c",&x);
getchar();
if(x=='0')
t=NULL;
else
{
t=new BT;
t->data=x;
printf("\n\t\t請(qǐng)輸入%c結(jié)點(diǎn)的左子結(jié)點(diǎn):",t->data);
t->lchild=CreateTree();
printf("\n\t\t請(qǐng)輸入%c結(jié)點(diǎn)的右子結(jié)點(diǎn):",t->data);
t->rchild=CreateTree();
}
return t;
}
void Preorder(BT *T)
{
if(T)
{
printf("%3c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Inorder(BT *T)
{
if(T)
{
Inorder(T->lchild);
printf("%3c",T->data);
Inorder(T->rchild);
}
}
void Postorder(BT *T)
{
if(T)
{
Postorder(T->lchild);
Postorder(T->rchild);
printf("%3c",T->data);
}
}
void Leafnum(BT *T)
{
if(T)
{
if(T->lchild==NULL&&T->rchild==NULL)
count++;
Leafnum(T->lchild);
Leafnum(T->rchild);
}
}
void Nodenum(BT *T)
{
if(T)
{
count++;
Nodenum(T->lchild);
Nodenum(T->rchild);
}
}
int TreeDepth(BT *T)
{
int ldep,rdep;
if(T==NULL)
return 0;
else
{
ldep=TreeDepth(T->lchild);
rdep=TreeDepth(T->rchild);
if(ldep>rdep)
return ldep+1;
else
return rdep+1;
}
}
??
立即下載此資源
?? 提示:下載后請(qǐng)用壓縮軟件解壓,推薦使用 WinRAR 或 7-Zip
?? 資源說(shuō)明
?? 下載說(shuō)明
- 下載需消耗 2積分
- 24小時(shí)內(nèi)重復(fù)下載不扣分
- 支持?jǐn)帱c(diǎn)續(xù)傳
- 資源永久有效
?? 使用說(shuō)明
- 下載后用解壓軟件解壓
- 推薦 WinRAR 或 7-Zip
- 如有密碼請(qǐng)查看說(shuō)明
- 解壓后即可使用
?? 積分獲取
- 上傳資源獲得積分
- 每日簽到免費(fèi)領(lǐng)取
- 邀請(qǐng)好友注冊(cè)獎(jiǎng)勵(lì)
- 查看詳情 →
??? 相關(guān)標(biāo)簽
點(diǎn)擊標(biāo)簽查看更多相關(guān)資源: