#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct bitNode
{
char value;
bitNode*lchild;
bitNode*rchild;
}bitNode,*bitTree;
void createTreeInPreOrder(bitTree& T)//create a bitTree by preOrder;
{
char ch;
scanf("%c",&ch);
if ('#'==ch)//indicate that this is an empty node;
{
return;
}
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
T=new bitNode;
T->value=ch;
T->lchild=NULL;//the pointer of sub-tree should be initialized;
T->rchild=NULL;
createTreeInPreOrder(T->lchild);
createTreeInPreOrder(T->rchild);
}
}
void preOrderTraverse(bitTree& T)//traverse bitTree in preOrder;
{
stack<bitTree> s;
bitTree p=T;
while(!s.empty()||p)
{
if (p!=NULL)
{
cout<<p->value;
s.push(p);
p=p->lchild;
}
else
{
p=s.top();
s.pop();
p=p->rchild;
}
}
cout<<endl;
}
void inOrderTraverse(bitTree& T)//traverse bitree by inorder;
{
stack<bitTree> s;
bitTree p=T;
while(!s.empty()||p)
{
if (p)
{
s.push(p);//the difference between in and pre:the cout is in else;
p=p->lchild;
}
else
{
p=s.top();
s.pop();
cout<<p->value;
p=p->rchild;
}
}
cout<<endl;
}
void postOrderTraverse(bitTree & T)//traverse the tree by postOrder,and use the tag to mark if the right sub-tree has been visited;
{
stack<bitTree> s;
int tag[20];//use tag to mark return from where,left(1) or right(2);
bitTree p=T;
while(p||!s.empty())
{
while(p)
{
s.push(p);
tag[s.size()]=0;
p=p->lchild;
}
while(!s.empty()&&tag[s.size()]==1)// if the right sub-tree has been visited, it should be poped and output;
{
p=s.top();
s.pop();
cout<<p->value;
}
if (!s.empty())
{
tag[s.size()]=1;//visit the right sub-tree and change the mark;
p=s.top();
p=p->rchild;
}
else
break;
}
cout<<endl;
}
int main()
{
bitTree T;
createTreeInPreOrder(T);
preOrderTraverse(T);
inOrderTraverse(T);
postOrderTraverse(T);
return 0;
}
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct bitNode
{
char value;
bitNode*lchild;
bitNode*rchild;
}bitNode,*bitTree;
void createTreeInPreOrder(bitTree& T)//create a bitTree by preOrder;
{
char ch;
scanf("%c",&ch);
if ('#'==ch)//indicate that this is an empty node;
{
return;
}
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
{
T=new bitNode;
T->value=ch;
T->lchild=NULL;//the pointer of sub-tree should be initialized;
T->rchild=NULL;
createTreeInPreOrder(T->lchild);
createTreeInPreOrder(T->rchild);
}
}
void preOrderTraverse(bitTree& T)//traverse bitTree in preOrder;
{
stack<bitTree> s;
bitTree p=T;
while(!s.empty()||p)
{
if (p!=NULL)
{
cout<<p->value;
s.push(p);
p=p->lchild;
}
else
{
p=s.top();
s.pop();
p=p->rchild;
}
}
cout<<endl;
}
void inOrderTraverse(bitTree& T)//traverse bitree by inorder;
{
stack<bitTree> s;
bitTree p=T;
while(!s.empty()||p)
{
if (p)
{
s.push(p);//the difference between in and pre:the cout is in else;
p=p->lchild;
}
else
{
p=s.top();
s.pop();
cout<<p->value;
p=p->rchild;
}
}
cout<<endl;
}
void postOrderTraverse(bitTree & T)//traverse the tree by postOrder,and use the tag to mark if the right sub-tree has been visited;
{
stack<bitTree> s;
int tag[20];//use tag to mark return from where,left(1) or right(2);
bitTree p=T;
while(p||!s.empty())
{
while(p)
{
s.push(p);
tag[s.size()]=0;
p=p->lchild;
}
while(!s.empty()&&tag[s.size()]==1)// if the right sub-tree has been visited, it should be poped and output;
{
p=s.top();
s.pop();
cout<<p->value;
}
if (!s.empty())
{
tag[s.size()]=1;//visit the right sub-tree and change the mark;
p=s.top();
p=p->rchild;
}
else
break;
}
cout<<endl;
}
int main()
{
bitTree T;
createTreeInPreOrder(T);
preOrderTraverse(T);
inOrderTraverse(T);
postOrderTraverse(T);
return 0;
}