不知道为什么我用循环队列他就不给我过.....
我不是怕他数据太多越界嘛,虽说给的实例才9个结点
注释掉的是我想用的步长递增方式,看了别人大佬的我改成了++才给我过
void InorderTraversal( BinTree BT )
{
if(BT)
{
InorderTraversal(BT->Left);
printf(" %c",BT->Data);
InorderTraversal(BT->Right);
}
}
void PreorderTraversal( BinTree BT )
{
if(BT)
{
printf(" %c",BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
void PostorderTraversal( BinTree BT )
{
if(BT)
{
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c",BT->Data);
}
}
void LevelorderTraversal( BinTree BT )
{
const int MAXSIZE=20;
BinTree Queue[MAXSIZE],tmp;
int front = 0 , rear = 0; //从队头出队,队尾出队,rear是队尾的下一个
if(BT) Queue[rear++] = BT; //该根结点入队
while(front != rear)
{
tmp = Queue[front++]; //队头元素出队
//front = (front+1)%MAXSIZE;
printf(" %c",tmp->Data); //访问根结点
if(tmp->Left) Queue[rear++] = tmp->Left; //若存在左孩子,左孩子入队(左子树的根结点)
//rear = (rear+1)%MAXSIZE;
if(tmp->Right) Queue[rear++] = tmp->Right; //若存在右孩子,右孩子入队(右子树的根结点)
//rear = (rear+1)%MAXSIZE;
}
}