#ifndef __TREE_H
#define __TREE_H
#include <iostream>
#include <stack>
using namespace std;
struct BTree{
int data;
BTree* left;
BTree* right;
};
void Pre_Order(BTree* rt)
{
if(rt == NULL)
return ;
BTree* pNode = rt;
stack<BTree*> S;
while(!S.empty() || pNode != NULL)
{
while(pNode)
{
cout << pNode->data << " ";
S.push(pNode);
pNode = pNode->left;
}
if(!S.empty())
{
pNode = S.top();
S.pop();
pNode = pNode->right;
}
}
return ;
}
void In_Order(BTree* rt)
{
if(rt == NULL)
return ;
BTree* pNode = rt;
stack<BTree*> S;
while(!S.empty() || pNode != NULL)
{
while(pNode)
{
S.push(pNode);
pNode = pNode->left;
}
if(!S.empty())
{
pNode = S.top();
cout << pNode->data << " ";
S.pop();
pNode = pNode->right;
}
}
return ;
}
void Post_Order(BTree* rt)
{
if(rt == NULL)
return ;
BTree* pNode = rt;
BTree* has_visite = NULL;
stack<BTree*> S;
while(!S.empty() || pNode != NULL)
{
while(pNode)
{
S.push(pNode);
pNode = pNode->left;
}
if(!S.empty())
{
pNode = S.top();
if(pNode->right == has_visite || pNode->right == NULL)
{
cout << pNode->data << " ";
has_visite = pNode;
pNode = NULL;
S.pop();
}
else
pNode = pNode->right;
}
}
return ;
}
#endif