/**********
【题目】试编写算法,求一棵以孩子兄弟链表表示的树的度。
孩子兄弟链表类型定义:
typedef struct CSTNode {
TElemType data;
struct CSTNode *firstChild, *nextSibling;
} CSTNode, *CSTree;
**********/
int Degree(CSTree T) /* 求树T的度 */
{
if(T==NULL) return 0;
int i,j,max,tmp;
CSTree Q[100];//临时存放各结点
i=j=0;
max=-1;
if(T)
{
max=0;
Q[j++]=T->firstChild;
while(i<j)//按层序遍历
{
tmp=0;
while(Q[i])
{
tmp++;
//存储有孩子的结点
if(Q[i]->firstChild) Q[j++]=Q[i]->firstChild;
Q[i]=Q[i]->nextSibling;//统计本层结点数
}
if(tmp>max) max=tmp;
i++;
}
}
return max;
}
网友评论