#include<cstdlib>
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef struct BiTNode
{
string data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void createBiTree(BiTree &r) //按照前序遍历实现创建树 1 2 # 4 6 # # # 3 # 5 # #
{
string temp;
cin>>temp;
if(temp.size() ==1 && temp[0]=='#')
{
r=NULL;
}
else
{
r = new BiTNode;
r->data = temp;
createBiTree(r->lchild);
createBiTree(r->rchild);
}
void PreOrder(BiTree T)
{
if(T!=NULL)
{
cout<<T->data;
PreOrder(T->lchild);
PreOrder(T->rchild);
}
else
return;
}
void InOrder(BiTree T)
{
if(T!=NULL)
{
InOrder(T->lchild);
cout<<T->data;
InOrder(T->rchild);
}
else
return;
}
void PostOrder(BiTree T)
{
if(T!=NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
cout<<T->data ;
}
else
return ;
}
//非递归实现方式树的三种遍历
void PreOrder2(BiTree T)
{
stack<BiTree> st;
BiTree p = T;
while(p || !st.empty())
{
if(p!=NULL)
{
cout<<p->data;
st.push(p);
p=p->lchild ;
}
else
{
p=st.top();
st.pop();
p=p->rchild ;
}
}
}
void InOrder2(BiTree T)
{
stack<BiTree> st;
BiTree p = T;
while(p || !st.empty())
{
if(p)
{
st.push(p);
p=p->lchild;
}
else
{
p=st.top();
st.pop();
cout<<p->data ;
p=p->rchild ;
}
}
}
void PostOrder2(BiTree T) //较难一点
{
stack<BiTree> st;
int Tag[2000];
memset(Tag,0,sizeof(Tag));
int topp=-1;
BiTree p = T;
while(p || !st.empty())
{
while(p!=NULL)
{
st.push(p);
topp++;
Tag[topp]=0;
p=p->lchild ;
}
while(!st.empty() && Tag[topp]==1)
{
p=st.top();
st.pop();
topp--;
cout<<p->data ;
}
if(!st.empty())
{
Tag[topp]=1;
p=st.top();
p=p->rchild;
}
else
break;
}
}
//层次遍历
void LeverOrder(BiTree T)
{
queue<BiTree> que;
BiTree p=T;
if(T==NULL)
return;
que.push(p);
while(!que.empty())
{
p=que.front();
que.pop();
cout<<p->data;
if(p->lchild)
{
que.push(p->lchild );
}
if(p->rchild )
{
que.push(p->rchild );
}
}
}
int main()
{
BiTree T;
createBiTree(T); //1 2 # 4 6 # # # 3 # 5 # #
PreOrder(T);
cout<<endl;
InOrder(T);
cout<<endl;
PostOrder(T);
cout<<endl;
PreOrder2(T);
cout<<endl;
InOrder2(T);
cout<<endl;
PostOrder2(T);
cout<<endl;
LeverOrder(T);
cout<<endl;
return 0;
}
#include<string>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<ctime>
#include<cstdio>
#include<stack>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef struct BiTNode
{
string data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void createBiTree(BiTree &r) //按照前序遍历实现创建树 1 2 # 4 6 # # # 3 # 5 # #
{
string temp;
cin>>temp;
if(temp.size() ==1 && temp[0]=='#')
{
r=NULL;
}
else
{
r = new BiTNode;
r->data = temp;
createBiTree(r->lchild);
createBiTree(r->rchild);
}
}
void PreOrder(BiTree T)
{
if(T!=NULL)
{
cout<<T->data;
PreOrder(T->lchild);
PreOrder(T->rchild);
}
else
return;
}
void InOrder(BiTree T)
{
if(T!=NULL)
{
InOrder(T->lchild);
cout<<T->data;
InOrder(T->rchild);
}
else
return;
}
void PostOrder(BiTree T)
{
if(T!=NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
cout<<T->data ;
}
else
return ;
}
//非递归实现方式树的三种遍历
void PreOrder2(BiTree T)
{
stack<BiTree> st;
BiTree p = T;
while(p || !st.empty())
{
if(p!=NULL)
{
cout<<p->data;
st.push(p);
p=p->lchild ;
}
else
{
p=st.top();
st.pop();
p=p->rchild ;
}
}
}
void InOrder2(BiTree T)
{
stack<BiTree> st;
BiTree p = T;
while(p || !st.empty())
{
if(p)
{
st.push(p);
p=p->lchild;
}
else
{
p=st.top();
st.pop();
cout<<p->data ;
p=p->rchild ;
}
}
}
void PostOrder2(BiTree T) //较难一点
{
stack<BiTree> st;
int Tag[2000];
memset(Tag,0,sizeof(Tag));
int topp=-1;
BiTree p = T;
while(p || !st.empty())
{
while(p!=NULL)
{
st.push(p);
topp++;
Tag[topp]=0;
p=p->lchild ;
}
while(!st.empty() && Tag[topp]==1)
{
p=st.top();
st.pop();
topp--;
cout<<p->data ;
}
if(!st.empty())
{
Tag[topp]=1;
p=st.top();
p=p->rchild;
}
else
break;
}
}
//层次遍历
void LeverOrder(BiTree T)
{
queue<BiTree> que;
BiTree p=T;
if(T==NULL)
return;
que.push(p);
while(!que.empty())
{
p=que.front();
que.pop();
cout<<p->data;
if(p->lchild)
{
que.push(p->lchild );
}
if(p->rchild )
{
que.push(p->rchild );
}
}
}
int main()
{
BiTree T;
createBiTree(T); //1 2 # 4 6 # # # 3 # 5 # #
PreOrder(T);
cout<<endl;
InOrder(T);
cout<<endl;
PostOrder(T);
cout<<endl;
PreOrder2(T);
cout<<endl;
InOrder2(T);
cout<<endl;
PostOrder2(T);
cout<<endl;
LeverOrder(T);
cout<<endl;
return 0;
}