typedef struct TreeNode* BinTree;
struct TreeNode
{
int Key;
BinTree Left;
BinTree Right;
};
int Width(BinTree T)
{
BinTree p;
Queue Q;
int Last, temp_width, max_width;
temp_width = max_width = 0;
Q = CreateQueue(MaxElements);
Last = Queue_rear(Q);//初始化last的大小
if (T == NULL) return 0;
else {
Enqueue(T, Q);
while (!IsEmpty(Q)) {
p = Front_Dequeue(Q);//取队首元素并将对手元素出队
temp_width++;//用来计数
if (p->Left != NULL) Enqueue(p->Left, Q);
if (p->Right != NULL) Enqueue(p->Right, Q);
if (Queue_front(Q) > Last) //当前层次以扩展完,当前层次的左右孩子以入队
Last = Queue_rear(Q);//更新last的大小
if (temp_width > max_width) max_width = temp_width;//选取最大的数值
temp_width = 0;//当前层次统计完成,计数清零
} /* end-if */
} /* end-while */
return max_width;
} /* end-else */
}