數(shù)據結構_圖遍歷的演示_第1頁
數(shù)據結構_圖遍歷的演示_第2頁
數(shù)據結構_圖遍歷的演示_第3頁
數(shù)據結構_圖遍歷的演示_第4頁
數(shù)據結構_圖遍歷的演示_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實習報告題目:圖遍歷的演示編譯環(huán)境:Microsoft Visual Studio 2010功能實現(xiàn):以鄰接表為存儲結構,演示在連通無向圖上訪問全部節(jié)點的操作;實現(xiàn)連通無向圖的深度優(yōu)先遍歷和廣度優(yōu)先遍歷;建立深度優(yōu)先生成樹和廣度優(yōu)先生成樹,按凹入表或樹形打印生成樹。一、 需求分析1. 以鄰接表為存儲結構,演示在連通無向圖上訪問全部節(jié)點的操作。該無向圖為一個交通網絡,共25個節(jié)點,30條邊,遍歷時需要以用戶指定的節(jié)點為起點,建立深度優(yōu)先生成樹和廣度優(yōu)先生成樹,再按凹入表或樹形打印生成樹。2. 程序的測試數(shù)據:graph.txt文件所表示的無向交通圖。二、 概要設計1. 主要數(shù)據結構設計:stru

2、ct ArcNode/邊表結點int vexIndex;/鄰接點域,即鄰接點在頂點表中的下標ArcNode* next;struct VertexNode/頂點表結點string vertex;/數(shù)據域ArcNode* firstArc;struct TNode/樹結點string data;struct TNode *fristchild, *nextchild;2. 鄰接表類設計:class GraphTraversepublic:VertexNode VexListMaxSize;/頂點表數(shù)組int vertexNumberber;/圖的頂點數(shù)int arcNumberber;/圖的邊數(shù)

3、bool HasCreated;/圖是否創(chuàng)建void ReadFile();/從文件讀取數(shù)據,并建立該圖void DisplayGraph();/以鄰接表顯示圖TNode* DFSForest(int);/建立深度優(yōu)先生成樹void DFSTree(int, TNode*);/深度優(yōu)先遍歷圖TNode* BFSForest(int);/建立廣度優(yōu)先生成樹void BFSTree(int, TNode*);/廣度優(yōu)先遍歷圖void PrintTree(TNode*, int);/按照凹入表方式打印樹;三、 詳細設計1. 主要操作函數(shù)的實現(xiàn):(1) 建立深度優(yōu)先生成樹函數(shù):TNode* Graph

4、Traverse:DFSForest(int v)int i,j;TNode *p,*q,*DT;j=v;for(i=0;i<vertexNumberber;i+)Visitedi=0;for(i=0;i<vertexNumberber;i+)if(Visited(i+j)%vertexNumberber=0)p=new TNode;p->data=VexList(i+j)%vertexNumberber.vertex;p->fristchild=NULL;p->nextchild=NULL;DT=p;q=p;DFSTree(i+j)%vertexNumberbe

5、r),p);return DT;(2) 深度優(yōu)先遍歷圖函數(shù):void GraphTraverse:DFSTree(int v, TNode* DT)int j=0;int i=0;TNode *p,*q;int first=1;Visitedv=1;for(ArcNode *m=VexListv.firstArc;m;m=m->next)j=m->vexIndex;if(Visitedj=0)p=new TNode;p->data=VexListj.vertex;p->fristchild=NULL;p->nextchild=NULL;if(first)DT-&g

6、t;fristchild=p;first=0;elseq->nextchild=p;q=p;DFSTree(j,q);(3) 建立廣度優(yōu)先生成樹函數(shù):TNode* GraphTraverse:BFSForest(int v)int i,j;TNode *p,*q,*BT;BT=NULL;j=v;for(i=0;i<vertexNumberber;i+)Visitedi=0;for(i=0;i<vertexNumberber;i+)if(Visited(i+j)%vertexNumberber=0)p=new TNode;p->data=VexList(i+j)%vert

7、exNumberber.vertex;p->fristchild=NULL;p->nextchild=NULL;BT=p;q=p;BFSTree(i+j)%vertexNumberber),p);return BT;(4) 廣度優(yōu)先遍歷圖函數(shù):void GraphTraverse:BFSTree(int v,TNode*BT)int front=-1;int rear=-1;int j=0;int a,b;int first=1;a=b=0;TNode *m, *n, *r, *curMaxSize;r=BT;ArcNode *p;Visitedv=1;Query+rear=v;w

8、hile(front!=rear)first=1;v=Query+front;for(p=VexListv.firstArc;p;p=p->next)j=p->vexIndex;if(Visitedj=0)m=new TNode;m->data=VexListj.vertex;m->fristchild=NULL;m->nextchild=NULL;Visitedj=1;Query+rear=j;if(first)r->fristchild=m;first=0;elsen->nextchild=m;cura+=m;n=m;r=curb+;(5) 打印函

9、數(shù):按照凹入表方式打印樹。void GraphTraverse:PrintTree(TNode *T,int i)int j;TNode *p;cout<<ends;for(j=1;j<=i;j+)cout<<ends<<ends;cout<<T->data<<endl;for(p=T->fristchild;p;p=p->nextchild)PrintTree(p,i+1);2. 主函數(shù)的實現(xiàn):void main()string s1;int flag;TNode *DT,*BT;GraphTraverse

10、graphnet;string begin;int i;flag=atoi(s1.c_str(); while(flag>5|flag<1)cout<<"輸入錯誤,請重新輸入!"<<endl;cout<<endl<<"請輸入操作序號:"cin>>s1;flag=atoi(s1.c_str(); while(flag!=5)switch(flag)case 1:graphnet.ReadFile();break;case 2:graphnet.DisplayGraph();break;

11、case 3:cout<<"請輸入遍歷起點:"cin>>begin;for(i=0;i<graphnet.vertexNumberber;i+)if(graphnet.VexListi.vertex=begin)break;if(i=graphnet.vertexNumberber)cout<<"輸入的起點不存在!"<<endl;break;elseDT=graphnet.DFSForest(i);cout<<"深度優(yōu)先遍歷結果如下(凹入表):"<<endl

12、;graphnet.PrintTree(DT,0);break;case 4:cout<<"請輸入遍歷起點:"cin>>begin;for(i=0;i<graphnet.vertexNumberber;i+)if(graphnet.VexListi.vertex=begin)break;if(i=graphnet.vertexNumberber)cout<<"輸入的起點不存在!"<<endl;break;elseBT=graphnet.BFSForest(i);cout<<"廣度

13、優(yōu)先遍歷結果如下(凹入表):"<<endl;graphnet.PrintTree(BT,0);break;flag=atoi(s1.c_str(); while(flag>5|flag<1)cout<<"輸入錯誤,請重新輸入!"<<endl;cout<<endl<<"請輸入操作序號:"cin>>s1;flag=atoi(s1.c_str(); 四、 用戶手冊1. 本程序的執(zhí)行文件為:GraphTraverse.exe2. 進入程序的用戶界面,并根據程序提示,輸入文件名及其路徑,讀取文件

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論