以中根和后根遍历序列构造二叉树,替换所有与pattern匹配的子树为bitree。
public class BinaryTree
{
public BinaryNoderoot;
public BinaryTree()
{
this.root = null;
}
public boolean isEmpty() //判断是否是空
{
return this.root == null;
}
public String toString()//输出带空子树先跟序列
{
return toString(this.root);
}
public String toString(BinaryNode<T>p)
{
if(p==null)
return "^";
return p.data.toString()+""+toString(p.left)+toString(p.right);
}
public BinaryTree(T inlist[],T postlist[])
{
this.root=BinaryTreecreate(inlist,0,inlist.length-1,postlist,0,postlist.length-1);
}
//由后根遍历的次序可知,该二叉树的根是potlist[n-1];改节点必定在中根次序中
//由中根遍历次序可知,Inlist[i]节点前段节点在根的左子树上,inlist[i]后的所有节点在根节点的右子树上
public BinaryNode<T> BinaryTreecreate(T[] inlist, int inbegin, int inend, T[] postlist, int postbegin, int postend)
{
if (postbegin < 0 || inbegin > inend) //递归结束条件
r