

#include "stdafx.h"
#include "binaryTree.h"
#include<list>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
Status PreOrderTraverse(BiTree T, Status (*Visit)(TElemType e))
{
if(IsEmpty(T))
return Ok;
/*
Visit(T->data);
PreOrderTraverse(T->lchild, Visit);
PreOrderTraverse(T->rchild, Visit);
*/
stack<BiTNode *> stack;
BiTNode* t;
stack.push(T);
while(!stack.empty())
{
t = stack.top();
stack.pop();
if(t->rchild != null)
stack.push(t->rchild);
Visit(t->data);
if(t->lchild != null)
stack.push(t->lchild);
}
return Ok;
}
Status InOrderTraverse(BiTree T, Status (*Visit)(TElemType e))
{
if(IsEmpty(T))
return Ok;
stack<BiTNode *> stack;
BiTNode* t;
stack.push(T);
t = T->lchild;
while(!stack.empty())
{
while(t != null)
{
stack.push(t);
t = t->lchild;
}
t = stack.top();
Visit(t->data);
stack.pop();
t = t->rchild;
}
return Ok;
}
Status PostOrderTraverse(BiTree T, Status (*Visit)(TElemType e))
{
if(IsEmpty(T))
return Ok;
stack<BiTNode *> stack;
BiTNode* t;
BiTNode* pre = null;
t = T;
while(!stack.empty() || t != null)
{
while(t != null)
{
stack.push(t);
t = t->lchild;
}
t = stack.top();
if(t->rchild == null || t->rchild == pre )
{
Visit(t->data);
stack.pop();
pre = t;
t = null;
}
else
{
t = t->rchild;
}
}
return Ok;
}
Status LevelTraverse(BiTree T, Status (*Visit)(TElemType e))
{
queue<BiTNode *> l;
BiTNode* t;
l.push(T);
while(!l.empty())
{
t = l.front();
if(t->lchild != null)
l.push(t->lchild);
if(t->rchild != null)
l.push(t->rchild);
Visit(t->data);
l.pop();
}
return Ok;
}