




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
實(shí)驗6:二叉樹及其應(yīng)用一、實(shí)驗?zāi)康臉涫菙?shù)據(jù)結(jié)構(gòu)中應(yīng)用極為廣泛的非線性結(jié)構(gòu),本單元的實(shí)驗到達(dá)熟悉二叉樹的存儲結(jié)構(gòu)的特性,以及如何應(yīng)用樹結(jié)構(gòu)解決具體問題。二、問題描述首先,掌握二叉樹的各種存儲結(jié)構(gòu)和熟悉對二叉樹的根本操作。其次,以二叉樹表示算術(shù)表達(dá)式的根底上,設(shè)計一個十進(jìn)制的四那么運(yùn)算的計算器。--+/a*b-efCd如算術(shù)表達(dá)式:a+b*(c-d)-e/f三、實(shí)驗要求如果利用完全二叉樹的性質(zhì)和二叉鏈表結(jié)構(gòu)建立一棵二叉樹,分別計算統(tǒng)計葉子結(jié)點(diǎn)的個數(shù)。求二叉樹的深度。十進(jìn)制的四那么運(yùn)算的計算器可以接收用戶來自鍵盤的輸入。由輸入的表達(dá)式字符串動態(tài)生成算術(shù)表達(dá)式所對應(yīng)的二叉樹。自動完成求值運(yùn)算和輸出結(jié)果。四、實(shí)驗環(huán)境PC微機(jī)DOS操作系統(tǒng)或Windows操作系統(tǒng)TurboC程序集成環(huán)境或VisualC++程序集成環(huán)境五、實(shí)驗步驟1、根據(jù)二叉樹的各種存儲結(jié)構(gòu)建立二叉樹;2、設(shè)計求葉子結(jié)點(diǎn)個數(shù)算法和樹的深度算法;3、根據(jù)表達(dá)式建立相應(yīng)的二叉樹,生成表達(dá)式樹的模塊;4、根據(jù)表達(dá)式樹,求出表達(dá)式值,生成求值模塊;5、程序運(yùn)行效果,測試數(shù)據(jù)分析算法。設(shè)計程序代碼如下:#include<stdio.h>#include<malloc.h>#defineSTACK_INIT_SIZE100#defineSTACKINCREMENT10#defineERROR0#defineNUMBER1#defineSIMBLE2intOP[8]={'+','-','*','/','(',')','#',0};typedefunion{ intOperator;//操作符 floatOperand;//操作數(shù) }Int_Float;//表達(dá)式樹typedefstructBinaryTreeNode{ Int_FloatData; intIsOperator; structBinaryTreeNode*RChild; structBinaryTreeNode*LChild;}BiTreeNode,*lpBiTreeNode;//棧typedefstruct{ lpBiTreeNode*base; lpBiTreeNode*top; intstacksize;}SqStack;intInitStack(SqStack&s){ s.base=(lpBiTreeNode*)malloc(STACK_INIT_SIZE*sizeof(lpBiTreeNode)); if(!s.base) return0; s.top=s.base; s.stacksize=STACK_INIT_SIZE; return1;}intPush(SqStack&s,lpBiTreeNodee){ if(s.top-s.base>=s.stacksize) { s.base=(lpBiTreeNode*)realloc(s.base,(s.stacksize+STACKINCREMENT)*sizeof(lpBiTreeNode)); if(!s.base) return0; s.top=s.base+s.stacksize; s.stacksize+=STACKINCREMENT; } *s.top=e; s.top++; return1;}intPop(SqStack&s,lpBiTreeNode&e){ if(s.top==s.base) return0; s.top--; e=*s.top; return1;}lpBiTreeNodeGetTop(SqStacks){ lpBiTreeNodee; if(s.top==s.base) returnNULL; e=*(s.top-1); returne;}intIsEmpty(SqStacks){ if(s.top==s.base) return1; else return0;}intIn(intc,int*op)//判斷c是否在op中,op為以結(jié)尾的數(shù)組{ while(*op!=0) { if(c==*op) return1; op++; } return0;}intPrecede(inttheta1,inttheta2){ inti,j; intop_look[7][7]= { {'>','>','<','<','<','>','>'}, {'>','>','<','<','<','>','>'}, {'>','>','>','>','<','>','>'}, {'>','>','>','>','<','>','>'}, {'<','<','<','<','<','=',''}, {'>','>','>','>','','>','>'}, {'<','<','<','<','<','','='} }; switch(theta1) { case'+': i=0; break; case'-': i=1; break; case'*': i=2; break; case'/': i=3; break; case'(': i=4; break; case')': i=5; break; case'#': i=6; break; default: return0; } switch(theta2) { case'+': j=0; break; case'-': j=1; break; case'*': j=2; break; case'/': j=3; break; case'(': j=4; break; case')': j=5; break; case'#': j=6; break; default: return0; } returnop_look[i][j];}intisNum(intc){ if(c>='0'&&c<='9') return1; elsereturn0;}intGetInput(Int_Float*Result)//返回值:無,1數(shù)字,2符號{ staticintbuffer=0;//緩存存儲數(shù)字后的符號 intc; Int_Floatresult; intIsNumber=0;//是否為數(shù)字 intIsFloat=0;//是否為小數(shù) inti,t=1; result.Operand=0; if(In(buffer,OP))//緩存中有符號,返回 { result.Operator=buffer; buffer=0; Result->Operator=result.Operator; returnSIMBLE;//符號 }//去前導(dǎo)空格 c=getchar(); while(c==''&&c!=10) { c=getchar(); } while(c!=''&&c!=10)//不是空格和回車 { if(isNum(c))//是數(shù)字 { IsNumber=1; c=c-48;//字符到整型 if(IsFloat==0) result.Operand=result.Operand*10+c; else { result.Operand=result.Operand*10+c; IsFloat++; } } elseif(c=='.') { if(IsFloat!=0) //兩個小數(shù)點(diǎn) { Result->Operand=0; returnERROR; } else IsFloat=1; } elseif(In(c,OP)) { if(IsNumber)//數(shù)字后接符號 { if(IsFloat==1) { Result->Operand=0; returnERROR; } else { buffer=c; for(i=0;i<IsFloat-1;i++) t*=10; Result->Operand=result.Operand/(float)t; returnNUMBER;//數(shù)字 } } else { Result->Operator=c; returnSIMBLE;//符號 } } c=getchar(); } buffer=0; if(IsNumber) { if(IsFloat==1) { Result->Operand=0; returnERROR; } else { for(i=0;i<IsFloat-1;i++) t*=10; Result->Operand=result.Operand/(float)t; returnNUMBER; } } elseif(result.Operand==0) { Result->Operand=0; returnERROR; } else { Result->Operator=result.Operator; returnSIMBLE; }}lpBiTreeNodeCreateBiTree(){ SqStackOperand;//操作數(shù) SqStackOperator;//操作符 lpBiTreeNodepNode; lpBiTreeNodetheta,a,b; Int_Floatc; printf("輸入算式,以'#'結(jié)尾\n"); intr=GetInput(&c); InitStack(Operand); InitStack(Operator); pNode=(lpBiTreeNode)malloc(sizeof(BiTreeNode)); pNode->Data.Operator='#'; pNode->IsOperator=1; pNode->LChild=NULL; pNode->RChild=NULL; Push(Operator,pNode); while(!(r==SIMBLE&&c.Operator=='#')||GetTop(Operator)->Data.Operator!='#') { if(r==ERROR) returnNULL; if(r==NUMBER)//是數(shù)字 { pNode=(lpBiTreeNode)malloc(sizeof(BiTreeNode)); pNode->Data.Operand=c.Operand; pNode->IsOperator=0; pNode->LChild=NULL; pNode->RChild=NULL; Push(Operand,pNode); r=GetInput(&c); } elseif(r==SIMBLE)//是符號 { switch(Precede(GetTop(Operator)->Data.Operator,c.Operator)) { case'<'://棧頂元素優(yōu)先權(quán)低 pNode=(lpBiTreeNode)malloc(sizeof(BiTreeNode)); pNode->Data.Operator=c.Operator; pNode->IsOperator=1; pNode->LChild=NULL; pNode->RChild=NULL; Push(Operator,pNode); r=GetInput(&c); break; case'='://'(',')'相遇,脫括號 Pop(Operator,pNode); r=GetInput(&c); break; case'>'://棧頂元素優(yōu)先權(quán)高,退棧并將運(yùn)算結(jié)果入棧 Pop(Operator,theta); Pop(Operand,b); Pop(Operand,a); theta->LChild=a; theta->RChild=b; Push(Operand,theta); break; } } } returnGetTop(Operand);}boolcalculate(lpBiTreeNodeRoot,float*result){ floatresultLchild; floatresultRchild; if(Root!=NULL) { if(Root->LChild==NULL&&Root->RChild==NULL) { *result=Root->Data.Operand; returntrue; } elseif(Root->LChild==NULL||Root->RChild==NULL) returnfalse; else { switch(Root->Data.Operator) { case'+': if(calculate(Root->LChild,&resultLchild)==false) returnfalse; if(calculate(Root->RChild,&resultRchild)==false) returnfalse; *result=resultLchild+resultRchild; break; case'-': if(calculate(Root->LChild,&resultLchild)==false) returnfalse; if(calculate(Root->RChild,&resultRchild)==false) returnfalse; *result=resultLchild-resultRchild; break; case'*': if(calculate(Root->LChild,&resultLchild)==false) returnfalse; if(calculate(Root->RChild,&resultRchild)==false) returnfalse; *result=resultLchild*resultRchild; break; case'/': if(calculate(Root->LChild,&resultLchild)==false) returnfalse; if(calculate(Root->RChild,&resultRchild)==false) returnfalse; *result=resultLchild/resultRchild; break; } } } returntrue;}intgetLeafNum(lpBiTreeNodeRoot){ intLeafnumLchild; intLeafnumRchild; if(Root!=NULL) { if(Root->LChild==NULL&&Root->RChild==NULL) return1; else { LeafnumLchild=getLeafNum(Root->LChild); LeafnumRchild=getLeafNum(Root->RChild); returnLeafnumLchild+LeafnumRchild; } } return0;}intgetDepth(lpBiTreeNodeRoot){ intLeafDepthL; intLeafDepthR; if(Root!=NULL) { if(Root->LChild==NULL&&Root->RChild==NULL) return0; else { LeafDepthL=getDepth(Root->LChild); LeafDepthR=getDepth(Root->RChil
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 市場營銷在職工作證明(7篇)
- 月收入與獎金津貼明細(xì)證明書(6篇)
- 商業(yè)合作伙伴資信證明書(5篇)
- 市場需求導(dǎo)向下的農(nóng)民素質(zhì)提升路徑
- 世界歷史冷戰(zhàn)時期事件考察試題集
- 促進(jìn)教師專業(yè)發(fā)展提升美育教學(xué)質(zhì)量的策略
- 汽車零部件供應(yīng)協(xié)議
- 食品原料采購安全合同書
- 2025年藝術(shù)設(shè)計專業(yè)考試試題及答案回顧
- 2025年網(wǎng)絡(luò)信息安全與技術(shù)防范的實(shí)務(wù)能力考試試卷及答案
- 2022年廣東南方報業(yè)傳媒集團(tuán)有限公司招聘筆試試題及答案解析
- 口腔黏膜病圖示課件
- 2022年南京中華中等專業(yè)學(xué)校教師招聘筆試題庫及答案解析
- 國開期末考試《人力資源管理》機(jī)考試題及答案(第56套)
- 房地產(chǎn)項目規(guī)劃設(shè)計部工作流程圖
- 送教上門情況記錄表
- 隧道二襯施工專項方案
- 機(jī)械設(shè)備供貨安裝及售后服務(wù)方案
- 《深圳公交綜合車場設(shè)計標(biāo)準(zhǔn)》(征求意見稿)
- 雙脈沖測試法對英飛凌FF300R12ME4的測試和研究
- 棄渣場施工方案
評論
0/150
提交評論