中国矿业大学 测绘软件实习报告Word下载.docx
《中国矿业大学 测绘软件实习报告Word下载.docx》由会员分享,可在线阅读,更多相关《中国矿业大学 测绘软件实习报告Word下载.docx(38页珍藏版)》请在冰豆网上搜索。
//构造函数,根据输入前序序列由键盘输入
~C_LJH_BinTree();
//析构函数
voidPreOrder();
//前序遍历
voidInOrder();
//中序遍历
voidPostOrder();
//后序遍历
private:
Tdata;
C_LJH_BinTree<
T>
*lchild,*rchild;
boolNO_Die;
};
template<
C_LJH_BinTree<
:
C_LJH_BinTree()
NO_Die=false;
lchild=NULL;
rchild=NULL;
charch;
cin>
>
ch;
if(ch=='
#'
)
{
NO_Die=true;
//若为#,代表空节点
}
else
this->
data=ch;
//保存输入的节点
//左子树
C_LJH_BinTree*newChild0=newC_LJH_BinTree<
();
if(newChild0->
NO_Die)
deletenewChild0;
else
lchild=newChild0;
//右子树
C_LJH_BinTree*newChild1=newC_LJH_BinTree<
//直接创建子节点,
if(newChild1->
deletenewChild1;
rchild=newChild1;
}
//析构函数
~C_LJH_BinTree()
if(lchild)deletelchild;
//删除父节点之前,先删除子节点
if(rchild)deleterchild;
//前序遍历
voidC_LJH_BinTree<
PreOrder()
cout<
<
this->
data<
"
\t"
;
//先输出父节点,然后子节点按照父节点做
if(lchild!
=NULL)
this->
lchild->
PreOrder();
if(rchild!
rchild->
//中序遍历
InOrder()
if(lchild)lchild->
InOrder();
if(rchild)rchild->
//后序遍历
PostOrder()
PostOrder();
intmain()
请输入二叉树的前序遍历:
endl;
(以#作为分支结尾,例如:
AB##C##)"
C_LJH_BinTree<
char>
m_tree;
前序遍历为:
m_tree.PreOrder();
cout<
中序遍历为:
m_tree.InOrder();
后序遍历为:
m_tree.PostOrder();
return0;
实验结果:
实验体会:
通过本次试验,理解了二叉树类的构建、二叉树的建立及其遍历。
作为第一次实验,内容上实现实验所要求的目没有多大的难处,但其从数据结构出发,让我回忆起很多以前学过的知识,对我来说,收获不少。
实验二图的创建、遍历及其MST的构建
完成图的创建、遍历及最小数的构建,加深对图的认识以及对相关课本知识的认识。
1.图的创建。
2.基于深度优先的图的遍历算法的设计与实现。
3.基于广度优先的图的遍历算法的设计与实现。
4.基于Prim算法的最小生成树的构建。
5.基于Kruskal算法的最小生成树的构建。
structprimnode
{public:
charbegvex;
//开始结点
charendvex;
//结束结点
intlowcost;
//中间权值};
classLJH_Graphmtx//图的邻接矩阵定义
{public:
LJH_Graphmtx(intsz=DefaultVertices);
//构造函数
~LJH_Graphmtx()//析构函数
{delete[]VerticesList;
delete[]Edge;
}
boolGraphEmpty()//判断图是否为空
{if(numEdges==0)returntrue;
elsereturnfalse;
boolGraphFull()//判断图是否为满
{if(numVertices==maxVertices||numEdges==maxVertices*(maxVertices-1)/2)
returntrue;
returnfalse;
intNumberOfVertices()//返回当前顶点数
{returnnumVertices;
intNumberOfEdges()//返回当前边数
{returnnumEdges;
chargetValue(inti)//取顶点i的值,i不合理返回0
{returni>
=0&
&
i<
=numVertices?
VerticesList[i]:
NULL;
intgetWeight(intv1,intv2)//取边(v1,v2)上的权值
{returnv1!
=-1&
v2!
=-1?
Edge[v1][v2]:
0;
intgetFirstNeighbor(intv);
//取顶点v的第一个邻接顶点
intgetNextNeighbor(intv,intw);
//取v的邻接顶点w的下一邻接顶点
boolinsertVertex(charvertex);
//插入顶点vertex
boolinsertEdge(intv1,intv2,intweight);
//插入边(v1,v2),权为weight
boolremoveVertex(intv);
//删去顶点v和所有与它相关联的边
boolremoveEdge(intv1,intv2);
//在图中删去边(v1,v2)
intgetVertexPos(charvertex)//给出顶点vertex的位置,如果该顶点不在图内则返回-1
{for(inti=0;
numVertices;
i++)
if(VerticesList[i]==vertex)returni;
return-1;
intmini();
//求图中所有边的最小权值
boolinput();
//输入函数
booloutput();
//输出函数
voidkruskal();
//kruskal算法
voidprim();
//prim算法
protected:
intmaxVertices;
//图中最大顶点数
intnumEdges;
//图中当前边数
intnumVertices;
//图中当前顶点数
private:
char*VerticesList;
//顶点表
int**Edge;
//邻接矩阵
intvisit[50];
//便利时的辅助工具
primnodecloseedge[50];
//为实现prim函数的辅助结点};
LJH_Graphmtx:
LJH_Graphmtx(intsz)//构造函数
{maxVertices=sz;
numVertices=0;
numEdges=0;
inti,j;
VerticesList=newchar[maxVertices];
//创建顶点表数组
Edge=(int**)newint*[maxVertices];
//创建邻接矩阵数组
for(i=0;
maxVertices;
Edge[i]=newint[maxVertices];
for(i=0;
i++)//邻接矩阵初始化
for(j=0;
j<
j++)
Edge[i][j]=(i==j)?
0:
maxWeight;
intLJH_Graphmtx:
getFirstNeighbor(intv){if(v!
=-1)
if(Edge[v][i]>
0&
Edge[v][i]<
maxWeight)returni;
return-1;
getNextNeighbor(intv,intw)//给出顶点v的某邻接顶点w的下一个邻接顶点的位置,如果找不到,则函数返回-1
{if(v!
w!
{for(inti=w+1;
maxWeight)returni;
boolLJH_Graphmtx:
insertVertex(charvertex)//插入顶点vertex
{if(numVertices==maxVertices)returnfalse;
//顶点表满,不插入
VerticesList[numVertices++]=vertex;
returntrue;
insertEdge(intv1,intv2,intweight)//插入边(v1,v2),权为weig