孩子兄弟链表
描述:对以孩子-兄弟链表表示的树编写计算树的深度的算法
1、源代码
#include <stdio.h>
#include<stdlib.h>
typedef struct CSNode{
int data;
struct CSNode *firstchild,*nextsibling;
}CSNode;
//层次遍历构建二叉树
void CreatTree(CSNode** T){
int ch;
scanf("%d",&ch);
if(ch==-1){
*T=NULL;
return;
}else{
(*T)=(CSNode*)malloc(sizeof(CSNode));
(*T)->data=ch;
printf("输入%d的左孩子",ch);
CreatTree((&(*T)->firstchild));
printf("输入%d的兄弟",ch);
CreatTree(&(*T)->nextsibling);
}
return;
}
int depth(CSNode *T){
CSNode *p;
if(!T) return 0;
if((*T).firstchild==NULL) return 1;
int dep,max=0;
p=(*T).firstchild;
for(;p!= NULL;p= p->nextsibling)
{
dep = depth(p);
if(dep > max)
{
max = dep;
}
}
return max+1;
}
int main()
{
CSNode *T;
printf("输入根节点:");
CreatTree(&T);
printf("深度为:%d",depth(T));
}
2、解释
求深度采用递归调用,如果树为空,则返回0,如果左孩子为空,则返回1,
定义一个最大值变量用于保留当前的最大值,如果后面兄弟结点的dep大于max,则将max置位dep
层层下降,最后返回max+1
3、输出结果