#include <iostream>
#include "queue"
using namespace std;
class BinaryTreeNode
{
public:
char data;
BinaryTreeNode * lc;
BinaryTreeNode * rc;
BinaryTreeNode(char C){ data = C ;lc = rc = NULL;}
};
class BinaryTree
{
private:
BinaryTreeNode * root;
public:
BinaryTree(){root = NULL;}
BinaryTreeNode * Create(BinaryTreeNode * p,char * m,char * n,int len );
BinaryTreeNode * GiveRoot(){return root;}
void Show();
};
BinaryTreeNode * BinaryTree::Create(BinaryTreeNode * p,char * m,char * n,int len )
{
char * x;
char * y;
int k=0;
if (len>0)
{
p=new BinaryTreeNode(*m);
while (*(n+k)!=*m)
{
k++;
}
x=m+1;
y=n;
if (k!=0)
{
p->rc=Create(p->rc,x,y,k);
}
x=m+k+1;
y=n+k+1;
if ((len-k-1)!=0)
{
p->lc=Create(p->lc,x,y,len-k-1);
}
}
root=p;
return p;
}
void BinaryTree::Show()
{
queue<BinaryTreeNode *> q;
BinaryTreeNode * p=root;
if (p)
{
q.push(p);
}
while (!q.empty())
{
p=q.front();
cout<<p->data<<" ";
q.pop();
if (p->lc)
{
q.push(p->lc);
}
if (p->rc)
{
q.push(p->rc);
}
}
cout<<endl;
}
char * convert(char * S,int len)
{
char * s = new char[len+1];
int i = 0;
while (i<len)
{
s[i] = S[len-i-1];
i++;
}
s[len] = '\0';
return s;
}
int length(char * s)
{
int i=0;
while (*(s+i)!='\0')
{
i++;
}
return i;
}
int main()
{
char * M = "FGDBHECA";
char * N = "BFDGACEH";
char * m = convert(M,length(M));
char * n = convert(N,length(N));
BinaryTree b;
cout<<"后序序列为:"<<M<<endl;
cout<<"中序序列为:"<<N<<endl;
b.Create(b.GiveRoot(),m,n,length(m));
b.Show();
return 0;
}