include
using namespace std;
define DataType char
struct BiNode
{
DataType data;
BiNode *lchild, *rchild;
int flag;
};
BiNode *Q[100]; //层序遍历队列
const int StackSize = 100;
define Data BiNode
class SeqStack //顺序栈
{
public:
Data *a[StackSize];
int top;
SeqStack(){ top = -1; }
~SeqStack(){}
void Push(Data *x);
Data* Pop();
Data* GetTop(){ if (top != -1) return a[top]; }
bool Empty(){ if (top == -1) return true; else return false; } //top==-1?return true:return false;
};
void BiTree::PreOrder(BiNode *bt) //前序非递归遍历函数调用
{
SeqStack s;
while (bt != NULL || s.top != -1)
{
while (bt != NULL)
{
cout << bt->data << ’ ‘;
s.Push(bt);
bt = bt->lchild;
}
if (s.top != -1)
{
bt = s.Pop(); //注意bt此时为空不能直接将节点的值付给bt,而要将栈改成压指针
bt = bt->rchild;
}
}