构造二叉树
template <class T>
BinaryTree<T>::BinaryTree(T postlist[], T inlist[], int n) //以先根和中根序列构造二叉树
{ //n指定序列长度
root = create(postlist, inlist, n-1, 0, n);
}
template <class T>
BinaryNode<T>* BinaryTree<T>::create(T postlist[], T inlist[], int postEnd,int inStart, int n)
{ //以先根和中根序列创建一棵子树,子树根结点是prelist[i],返回根结点指针
BinaryNode<T> *p=NULL;
if (n>0)
{
T elem=postlist[postEnd]; //根结点值
p = new BinaryNode<T>(elem); //创建结点
int i=0;
while (i<n && elem!=inlist[inStart+i]) //在中根序列中查找根值所在位置
i++;
p->left = create(postlist, inlist, postEnd-n+i, inStart, i); //创建左子树
p->right = create(postlist, inlist, postEnd-1, inStart+i+1, n-1-i);//创建右子树
}
return p;
}