数据结构拓展习题:二叉树的繁茂度
题目:一棵二叉树T的繁茂度定义为各层结点数的最大值(也称二叉树的宽度)和二叉树的高度的乘积。试设计算法,求给定二叉树T的繁茂度。






int Depth(BiTree T)//计算树的深度
{
int m = 0;
int n = 0;
if (T == NULL)
return 0;
else
{
m = Depth(T->lchild); //计算左子树的深度
n = Depth(T->rchild); //计算右子树的深度
if (m > n)
return m + 1;
else
return n + 1;
}
}
int Width(BiTree T) //计算树的宽度
{
if (T == NULL)//如果是空树则宽度为0
return 0;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q, T);
BiTNode* p;
int width = 1;
int m;
while (!QueueEmpty(Q))
{
m = QueueLength(Q);//m为队列长度
if (width < m)//width取最长的队列
width = m;
for (int i = 0; i < m; i++)//将树的下一层所有结点入队
{
DeQueue(Q, p);
if (p->lchild)
EnQueue(Q, p->lchild);
if (p->rchild)
EnQueue(Q, p->rchild);
}
}
return width;
}
int Lushness(BiTree T) //计算树的繁茂度
{
return Depth(T) * Width(T);
}