数据结构图遍历的演示Word下载.docx
- 文档编号:21222431
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:14
- 大小:180.24KB
数据结构图遍历的演示Word下载.docx
《数据结构图遍历的演示Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构图遍历的演示Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
structVertexNode//顶点表结点
stringvertex;
//数据域
ArcNode*firstArc;
structTNode//树结点
stringdata;
structTNode*fristchild,*nextchild;
2.邻接表类设计:
classGraphTraverse
public:
VertexNodeVexList[MaxSize];
//顶点表数组
intvertexNumberber;
//图的顶点数
intarcNumberber;
//图的边数
boolHasCreated;
//图是否创建
voidReadFile();
//从文件读取数据,并建立该图
voidDisplayGraph();
//以邻接表显示图
TNode*DFSForest(int);
//建立深度优先生成树
voidDFSTree(int,TNode*);
//深度优先遍历图
TNode*BFSForest(int);
//建立广度优先生成树
voidBFSTree(int,TNode*);
//广度优先遍历图
voidPrintTree(TNode*,int);
//按照凹入表方式打印树
三、详细设计
1.主要操作函数的实现:
(1)建立深度优先生成树函数:
TNode*GraphTraverse:
:
DFSForest(intv)
inti,j;
TNode*p,*q,*DT;
j=v;
for(i=0;
i<
vertexNumberber;
i++)
{
Visited[i]=0;
}
if(Visited[(i+j)%vertexNumberber]==0)
{
p=newTNode;
p->
data=VexList[(i+j)%vertexNumberber].vertex;
fristchild=NULL;
nextchild=NULL;
DT=p;
q=p;
DFSTree(((i+j)%vertexNumberber),p);
}
returnDT;
}
(2)深度优先遍历图函数:
voidGraphTraverse:
DFSTree(intv,TNode*DT)
intj=0;
inti=0;
TNode*p,*q;
intfirst=1;
Visited[v]=1;
for(ArcNode*m=VexList[v].firstArc;
m;
m=m->
next)
j=m->
vexIndex;
if(Visited[j]==0)
data=VexList[j].vertex;
if(first)
{
DT->
fristchild=p;
first=0;
}
else
q->
nextchild=p;
DFSTree(j,q);
(3)建立广度优先生成树函数:
BFSForest(intv)
TNode*p,*q,*BT;
BT=NULL;
BT=p;
BFSTree(((i+j)%vertexNumberber),p);
returnBT;
(4)广度优先遍历图函数:
BFSTree(intv,TNode*BT)
intfront=-1;
intrear=-1;
inta,b;
a=b=0;
TNode*m,*n,*r,*cur[MaxSize];
r=BT;
ArcNode*p;
Query[++rear]=v;
while(front!
=rear)
first=1;
v=Query[++front];
for(p=VexList[v].firstArc;
p;
p=p->
j=p->
if(Visited[j]==0)
m=newTNode;
m->
Visited[j]=1;
Query[++rear]=j;
if(first)
{
r->
fristchild=m;
first=0;
}
else
n->
nextchild=m;
cur[a++]=m;
n=m;
r=cur[b++];
(5)打印函数:
按照凹入表方式打印树。
PrintTree(TNode*T,inti)
intj;
TNode*p;
cout<
<
ends;
for(j=1;
j<
=i;
j++)
cout<
ends<
T->
data<
endl;
for(p=T->
fristchild;
nextchild)
PrintTree(p,i+1);
2.主函数的实现:
voidmain()
strings1;
intflag;
TNode*DT,*BT;
GraphTraversegraphnet;
stringbegin;
inti;
flag=atoi(s1.c_str());
while(flag>
5||flag<
1)
"
输入错误,请重新输入!
endl<
请输入操作序号:
;
cin>
>
s1;
flag=atoi(s1.c_str());
while(flag!
=5)
switch(flag)
case1:
graphnet.ReadFile();
break;
case2:
graphnet.DisplayGraph();
case3:
cout<
请输入遍历起点:
cin>
begin;
for(i=0;
graphnet.vertexNumberber;
if(graphnet.VexList[i].vertex==begin)
break;
if(i==graphnet.vertexNumberber)
cout<
输入的起点不存在!
break;
DT=graphnet.DFSForest(i);
深度优先遍历结果如下(凹入表):
graphnet.PrintTree(DT,0);
case4:
BT=graphnet.BFSForest(i);
广度优先遍历结果如下(凹入表):
graphnet.PrintTree(BT,0);
while(flag>
flag=atoi(s1.c_str());
四、用户手册
1.本程序的执行文件为:
GraphTraverse.exe
2.进入程序的用户界面,并根据程序提示,输入文件名及其路径,读取文件中的数据:
3.显示当前图:
4.输入遍历起点,进行深度优先遍历(或者广度优先遍历):
五、测试结果
程序的主要测试结果如下:
1.读取文件,并显示当前图:
2.输入遍历起点,进行深度优先遍历:
3.输入遍历起点,进行广度优先遍历:
六、附录
源程序文件清单:
GraphTraverse.h//邻接表类以及主要数据结构的声明
GraphTraverse.cpp//邻接表类成员函数的实现
GraphMain.cpp//主程序
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据 结构图 遍历 演示