队列实现层序遍历,以及改为堆栈先序遍历
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct BinTree
{
ElemType data;
struct BinTree* Left;
struct BinTree* Right;
}BinTree;
typedef struct Queue
{
ElemType data;
struct Queue* Frond;
struct Queue* rear;
}Queue;
void LevelOrderTraversal(BinTree* BT)//层序遍历,用队列
{
Queue* Q;
BinTree* T;
if(!BT)return ;
Q=CreatQueue(MaxSize);//创建并初始化队列
AddQ(Q,BT);
while(!IsEmpty(Q))
{
T=Delete(Q);
cout<<T->data<<" ";
if(T->Left) Add(Q,T->Left);//加入队列
if(T->Right) Add(Q,T->Right);
}
}
//可以用堆栈来实现二叉树的先序遍历
//即对调左右子树的加入顺序先Add(Q,T->Right),后Add(Q,T->Left),再;