我想用二叉树的前序和中序两种遍历序列建立一棵二叉树,并用后序法输出,但编了程序后无法实现,调试时是在从数组里将元素赋给data值时出错,但不明白为什么错,请大家帮我看看吧,谢谢~
#include <iostream>
using namespace std;
class BiNode
{
public:
char data;
BiNode* lchild,* rchild;
};
int locate(char A[],int x);
static int i=0;
char X[10]={'A','B','H','F','D','E','C','K','G','#'};
char Y[9]={'H','B','D','F','A','E','K','C','G'};
void create(BiNode *root);
void PostOrder(BiNode *root);
void main()
{
BiNode *root;//=new BiNode;
// int i=0;
create(root);
PostOrder(root);
}
int locate(char A[],int x)
{
int i=0;
while(x!=A[i])i++;
return i;
}
void create(BiNode *root)
{
root=new BiNode;
root->data =X[i];
if(X[i]=='#') return;
else
{
// if(locate(Y[], X[i+1])<locate(Y[], X[i]))
int n=locate(Y,X[i+1])<locate(Y,X[i]);
if(n)
{
create(root->lchild);
i++;
}
else
{
create(root->rchild);
i++;
}
}
}
void PostOrder(BiNode *root)
{
if(root==NULL)return;
else
{
PostOrder(root->lchild );
PostOrder(root->rchild );
cout<<root->data ;
}
}