c语言深度
Ⅰ c语言二叉树的深度指什么怎么求
从根节点到叶子结点一次经过的结点形成树的一条路径,最长路径的长度为树的深度。根节点的深度为1。
解体思路:
1.如果根节点为空,则深度为0,返回0,递归的出口。
2.如果根节点不为空,那么深度至少为1,然后我们求他们左右子树的深度,
3.比较左右子树深度值,返回较大的那一个
4.通过递归调用
#include<iostream>
#include<stdlib.h>
usingnamespacestd;
structBinaryTreeNode
{
intm_nValue;
BinaryTreeNode*m_pLeft;
BinaryTreeNode*m_pRight;
};
//创建二叉树结点
BinaryTreeNode*CreateBinaryTreeNode(intvalue)
{
BinaryTreeNode*pNode=newBinaryTreeNode();
pNode->m_nValue=value;
pNode->m_pLeft=NULL;
pNode->m_pRight=NULL;
returnpNode;
}
//连接二叉树结点
voidConnectTreeNodes(BinaryTreeNode*pParent,BinaryTreeNode*pLeft,BinaryTreeNode*pRight)
{
if(pParent!=NULL)
{
pParent->m_pLeft=pLeft;
pParent->m_pRight=pRight;
}
}
//求二叉树深度
intTreeDepth(BinaryTreeNode*pRoot)//计算二叉树深度
{
if(pRoot==NULL)//如果pRoot为NULL,则深度为0,这也是递归的返回条件
return0;
//如果pRoot不为NULL,那么深度至少为1,所以left和right=1
intleft=1;
intright=1;
left+=TreeDepth(pRoot->m_pLeft);//求出左子树的深度
right+=TreeDepth(pRoot->m_pRight);//求出右子树深度
returnleft>right?left:right;//返回深度较大的那一个
}
voidmain()
{
//1
///
//23
///\
//456
///
//7
//创建树结点
BinaryTreeNode*pNode1=CreateBinaryTreeNode(1);
BinaryTreeNode*pNode2=CreateBinaryTreeNode(2);
BinaryTreeNode*pNode3=CreateBinaryTreeNode(3);
BinaryTreeNode*pNode4=CreateBinaryTreeNode(4);
BinaryTreeNode*pNode5=CreateBinaryTreeNode(5);
BinaryTreeNode*pNode6=CreateBinaryTreeNode(6);
BinaryTreeNode*pNode7=CreateBinaryTreeNode(7);
//连接树结点
ConnectTreeNodes(pNode1,pNode2,pNode3);
ConnectTreeNodes(pNode2,pNode4,pNode5);
ConnectTreeNodes(pNode3,NULL,pNode6);
ConnectTreeNodes(pNode5,pNode7,NULL);
intdepth=TreeDepth(pNode1);
cout<<depth<<endl;
system("pause");
}
出处:http://www.cnblogs.com/xwdreamer
Ⅱ C语言中,二叉树的深度指怎样计算
二叉树中结点的最大层数称为二叉树的深度。计算:就是结点最大层数的个数,这还用计算,一看就知道。
Ⅲ C语言深度总结[全面认识main函数传递参数]
argc和argv是main函数的形式参数。这两个形式参数的类型是系统规定的。如果main函数要带参数,就是这两个类型的参数;否则main函数就没有参数。
坚持使用标准的意义在于:当你把程序从一个编译器移到另一个编译器时,照样能正常运行。
由于是 int main( ..) 那么当时 应当返回 int但是return 2.3 ;也能运行正确,这是因为编译器自动转换2.3为int,截断后为return 2;
如果写为 return "abc";那么会报错, error C2440: “return”: 无法从“const char [4]”转换为“int”。
变量名称argc和argv是常规的名称,当然也可以换成其他名称。那么,实际参数是如何传递给main函数的argc和argv的呢?我们知道,C程序在编译和链接后,都生成一个可执行文件。也可以在命令行下带参数执行,命令行执行的形式为:可执行文件名称 参数1 参数2 ... ... 参数n。可执行文件名称和参数、参数之间均使用空格隔开。
如果按照这种方法执行,命令行字符串将作为实际参数传递给main函数。具体为:
(1) 可执行文件名称和所有参数的个数之和传递给argc;
(2) 可执行文件名称(包括路径名称)作为一个字符串,首地址被赋给argv[0],参数1也作为一个字符串,首地址被赋给argv[1],... ...依次类推。
字符串arav[i](i=1,...argc-1)表式第 i 个程序参数,标准C 要求argv[argc]是个null指针,但在有些旧时编译器中却不是这样的,argv向量以及它所指向的字符串必须是可以修改的,并且他们的值在程序执行期间不能被编译器或操作系统所修改。如果编译器并不允许大小写混合的字符串 ,则存储在argv中的字符串必须采用小写形式。
1.给main函数传递参数只有一种方式,即main(int argc, char *argv[])。第一个参数必须int,第二个(如果有的话)必须是char**或char *argv[]。
2.argc代表传入参数的个数,argv是一个数组,每个元素都是一个char *。字符串arav[i](i=1,...argc-1)表式第 i 个程序参数,标准C 要求argv[argc]是个null指针。
3.main函数参数理论上支持“无数”个,且参数在进程内支持修改。
Ⅳ 求c语言图的深度优先遍历算法
#define MaxVerNum 100 /* 最大顶点数为*/
typedef enum {False,True} boolean;
#include "stdio.h"
#include "stdlib.h"
boolean visited[MaxVerNum];
typedef struct node /* 表结点*/
{
int adjvex;/* 邻接点域,一般是放顶点对应的序号或在表头向量中的下标*/
char Info; /*与边(或弧)相关的信息*/
struct node * next; /* 指向下一个邻接点的指针域*/
} EdgeNode;
typedef struct vnode /* 顶点结点*/
{
char vertex; /* 顶点域*/
EdgeNode * firstedge; /* 边表头指针*/
} VertexNode;
typedef struct
{
VertexNode adjlist[MaxVerNum]; /* 邻接表*/
int n,e; /* 顶点数和边数*/
} ALGraph; /* ALGraph是以邻接表方式存储的图类型*/
//建立一个无向图的邻接表存储的算法如下:
void CreateALGraph(ALGraph *G)/* 建立有向图的邻接表存储*/
{
int i,j,k;
int N,E;
EdgeNode *p;
printf("请输入顶点数和边数:");
scanf("%d %d",&G->n,&G->e);
printf("n=%d,e=%d\n\n",G->n,G->e);
getchar();
for(i=0;i<G->n;i++) /* 建立有n个顶点的顶点表*/
{
printf("请输入第%d个顶点字符信息(共%d个):",i+1,G->n);
scanf("%c",&(G->adjlist[i].vertex)); /* 读入顶点信息*/
getchar();
G->adjlist[i].firstedge=NULL; /* 顶点的边表头指针设为空*/
}
for(k=0;k<2*G->e;k++) /* 建立边表*/
{
printf("请输入边<Vi,Vj>对应的顶点序号(共%d个):",2*G->e);
scanf("%d %d",&i,&j);/* 读入边<Vi,Vj>的顶点对应序号*/
p=(EdgeNode *)malloc(sizeof(EdgeNode)); // 生成新边表结点p
p->adjvex=j; /* 邻接点序号为j */
p->next=G->adjlist[i].firstedge;/* 将结点p插入到顶点Vi的链表头部*/
G->adjlist[i].firstedge=p;
}
printf("\n图已成功创建!对应的邻接表如下:\n");
for(i=0;i<G->n;i++)
{
p=G->adjlist[i].firstedge;
printf("%c->",G->adjlist[i].vertex);
while(p!=NULL)
{
printf("[ %c ]",G->adjlist[p->adjvex].vertex);
p=p->next;
}
printf("\n");
}
printf("\n");
} /*CreateALGraph*/
int FirstAdjVertex(ALGraph *g,int v)//找图g中与顶点v相邻的第一个顶点
{
if(g->adjlist[v].firstedge!=NULL) return (g->adjlist[v].firstedge)->adjvex;
else return 0;
}
int NextAdjVertex(ALGraph *g ,int vi,int vj )//找图g中与vi相邻的,相对相邻顶点vj的下一个相邻顶点
{
EdgeNode *p;
p=g->adjlist[vi].firstedge;
while( p!=NULL && p->adjvex!=vj) p=p->next;
if(p!=NULL && p->next!=NULL) return p->next->adjvex;
else return 0;
}
void DFS(ALGraph *G,int v) /* 从第v个顶点出发深度优先遍历图G */
{
int w;
printf("%c ",G->adjlist[v].vertex);
visited[v]=True; /* 访问第v个顶点,并把访问标志置True */
for(w=FirstAdjVertex(G,v);w;w=NextAdjVertex(G,v,w))
if (!visited[w]) DFS(G,w); /* 对v尚未访问的邻接顶点w递归调用DFS */
}
void DFStraverse(ALGraph *G)
/*深度优先遍历以邻接表表示的图G,而以邻接矩阵表示时,算法完全相同*/
{ int i,v;
for(v=0;v<G->n;v++)
visited[v]=False;/*标志向量初始化*/
//for(i=0;i<G->n;i++)
if(!visited[0]) DFS(G,0);
}/*DFS*/
void main()
{
ALGraph G;
CreateALGraph(&G);
printf("该无向图的深度优先搜索序列为:");
DFStraverse(&G);
printf("\nSuccess!\n");
}
Ⅳ c语言深度优先搜索。代码
#include<stdlib.h>
#include<stdio.h>
structnode/*图顶点结构定义*/
{
intvertex;/*顶点数据信息*/
structnode*nextnode;/*指下一顶点的指标*/
};
typedefstructnode*graph;/*图形的结构新型态*/
structnodehead[9];/*图形顶点数组*/
intvisited[9];/*遍历标记数组*/
/********************根据已有的信息建立邻接表********************/
voidcreategraph(intnode[20][2],intnum)/*num指的是图的边数*/
{
graphnewnode;/*指向新节点的指针定义*/
graphptr;
intfrom;/*边的起点*/
intto;/*边的终点*/
inti;
for(i=0;i<num;i++)/*读取边线信息,插入邻接表*/
{
from=node[i][0];/*边线的起点*/
to=node[i][1];/*边线的终点*/
/*建立新顶点*/
newnode=(graph)malloc(sizeof(structnode));
newnode->vertex=to;/*建立顶点内容*/
newnode->nextnode=NULL;/*设定指标初值*/
ptr=&(head[from]);/*顶点位置*/
while(ptr->nextnode!=NULL)/*遍历至链表尾*/
ptr=ptr->nextnode;/*下一个顶点*/
ptr->nextnode=newnode;/*插入节点*/
}
}
/**********************图的深度优先搜寻法********************/
voiddfs(intcurrent)
{
graphptr;
visited[current]=1;/*记录已遍历过*/
printf("vertex[%d] ",current);/*输出遍历顶点值*/
ptr=head[current].nextnode;/*顶点位置*/
while(ptr!=NULL)/*遍历至链表尾*/
{
if(visited[ptr->vertex]==0)/*如过没遍历过*/
dfs(ptr->vertex);/*递回遍历呼叫*/
ptr=ptr->nextnode;/*下一个顶点*/
}
}
/******************************主程序******************************/
intmain()
{
graphptr;
intnode[20][2]={{1,2},{2,1},/*边线数组*/
{1,3},{3,1},
{1,4},{4,1},
{2,5},{5,2},
{2,6},{6,2},
{3,7},{7,3},
{4,7},{4,4},
{5,8},{8,5},
{6,7},{7,6},
{7,8},{8,7}};
inti;
//clrscr();
for(i=1;i<=8;i++)/*顶点数组初始化*/
{
head[i].vertex=i;/*设定顶点值*/
head[i].nextnode=NULL;/*指针为空*/
visited[i]=0;/*设定遍历初始标志*/
}
creategraph(node,20);/*建立邻接表*/
printf("Contentofthegragh'sADlistis: ");
for(i=1;i<=8;i++)
{
printf("vertex%d->",head[i].vertex);/*顶点值*/
ptr=head[i].nextnode;/*顶点位置*/
while(ptr!=NULL)/*遍历至链表尾*/
{
printf("%d",ptr->vertex);/*印出顶点内容*/
ptr=ptr->nextnode;/*下一个顶点*/
}
printf(" ");/*换行*/
}
printf(" Theendofthedfsare: ");
dfs(1);/*打印输出遍历过程*/
printf(" ");/*换行*/
puts("Pressanykeytoquit...");
//getch();
}
Ⅵ c语言 深度怎么算
二叉树的遍历,利用递归函数
int Depth(BiTree T){
int depthval,depthleft,depthright;
if(T == NULL) depthval = 0;
else{
depthleft = Depth(T -> lchild);
depthright = Depth(T -> rchild);
depthval = 1 + (depthleft > depthright ? depthleft : depthright);
}//else
return depthval;
}//Depth