求二叉树宽度算法
void width(Tree T) //求二叉树宽度
{
int w = 0, wmax = 1, last = -1;
Tree p;
queue q;
q.front = q.rear = -1;
if(T == NULL)
printf("该二叉树宽度为0!\n");
else
{
q.data[++q.rear] = T;
last = q.rear;
while(q.front < q.rear)
{
p = q.data[++q.front];
if(p->lchild != NULL)
q.data[++q.rear] = p->lchild;
if(p->rchild != NULL)
q.data[++q.rear] = p->rchild;
w++;
if(q.front == last)
{
if(w > wmax)
wmax = w;
w = 0;
last = q.rear;
}
}
printf("该二叉树宽度为%d!\n", wmax);
}
}