#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 二叉樹子系統(tǒng)"); printf("\n\t\t*****************************************"); printf("\n\t\t 1---------建二叉樹 "); 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é)點數(shù) "); printf("\n\t\t 7---------求樹深度 "); printf("\n\t\t 0---------返 回 "); printf("\n\t\t*****************************************"); printf("\n\t\t 請選擇菜單號 (0--7)"); scanf("%c",&ch2); getchar(); printf("\n"); switch(ch2) { case'1': printf("\n\t\t請按先序序列輸入二叉樹的結(jié)點:\n"); printf("\n\t\t說明:輸入結(jié)點(‘0’代表后繼結(jié)點為空)后按回車。\n"); printf("\n\t\t請輸入根結(jié)點:"); T=CreateTree(); printf("\n\t\t二叉樹成功建立!\n");break; case'2': printf("\n\t\t該二叉樹的先序遍歷序列為:"); Preorder(T);break; case'3': printf("\n\t\t該二叉樹的中序遍歷序列為:"); Inorder(T);break; case'4': printf("\n\t\t該二叉樹的后序遍歷序列為:"); Postorder(T);break; case'5': count=0;Leafnum(T); printf("\n\t\t該二叉樹有%d個葉子。\n",count);break; case'6': count=0;Nodenum(T); printf("\n\t\t該二叉樹總共有%d個結(jié)點。\n",count);break; case'7': printf("\n\t\t該樹的深度為:%d",TreeDepth(T)); break; case'0': ch1='n';break; default: printf("\n\t\t***請注意:輸入有誤!***"); } 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請輸入%c結(jié)點的左子結(jié)點:",t->data); t->lchild=CreateTree(); printf("\n\t\t請輸入%c結(jié)點的右子結(jié)點:",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; } }
上傳時間: 2020-06-11
上傳用戶:ccccy
一般軟件做出來后,都只能在一個環(huán)境下,運行(繁體或者簡體)如我們加入Delphi的繁簡轉(zhuǎn)換代碼,即可解決這個問題
標簽:
上傳時間: 2014-11-15
上傳用戶:wanqunsheng
embedded visual C++和基本的OpenGLES設(shè)定環(huán)境程式
標簽: embedded OpenGLES visual 程式
上傳時間: 2014-01-16
上傳用戶:wanqunsheng
rtems 於 Redhat 9.0 上的開發(fā)環(huán)境建立步驟
上傳時間: 2013-12-10
上傳用戶:亞亞娟娟123
DelphiPackage實用集錦 動態(tài)數(shù)據(jù)模組 設(shè)計模式重整技術(shù)運用實例 以DelphiPackage架構(gòu)多人開發(fā)應(yīng)用程式環(huán)境
標簽: DelphiPackage 集錦 動態(tài)數(shù)據(jù) 模組
上傳時間: 2014-11-22
上傳用戶:GHF
SEGY程式碼提供SEGY在不同作業(yè)環(huán)境下面的程式碼
上傳時間: 2013-12-08
上傳用戶:鳳臨西北
中國式報表,PowerBuilder8.0環(huán)境
標簽: PowerBuilder 8.0
上傳時間: 2014-02-24
上傳用戶:450976175
Sitronix 開發(fā)環(huán)境 Ver 10
上傳時間: 2013-12-08
上傳用戶:小草123
WINXP 環(huán)境下 抓取USB封包的程式 (使用此程式 對於USB封包格式要很清楚 不然會看不懂)
上傳時間: 2013-12-28
上傳用戶:源碼3
DSP整合開發(fā)環(huán)境CCS開發(fā)指南 給任何要開發(fā)DSP環(huán)境的初學者,一個很好的學習
上傳時間: 2015-11-19
上傳用戶:Divine
蟲蟲下載站版權(quán)所有 京ICP備2021023401號-1