删除所有与 pattern 匹配的子树(C++ 数据结构)

问题遇到的现象和发生背景

以中根和后根序列构造二叉树,删除所有与 pattern 匹配的子树。成员函数声明如下:
BinaryNode* removeAll(BinaryTree &pattern) //删除所有与 pattern 匹配的子树
运行后虽然删除了 但后面本来应在的子树也没了
调试一直报错

问题相关代码,请勿粘贴截图
#include <iostream>
using namespace std;
#include "BinaryNode.h"
template<class T>
class BinaryTree{
public:
    BinaryNode<T>* root;

    BinaryTree();//构造空二叉树

    BinaryTree(T lalist[], T inlist[], int n);//以中根和后根序列构造二叉树

    ~BinaryTree();//析构

    bool empty();//判断是否为空二叉树

    friend ostream & operator<<<>(ostream& out, BinaryTree<T>&);//输出
    void preOrder();//输出先根次序遍历序列
    string getinOrder(BinaryNode<T>*);//获得中根次序遍历的字符串
    string getpostOrder(BinaryNode<T>*);//获得后根次序遍历的字符串
     BinaryTree<T>(BinaryTree<T>& bitree);
    BinaryNode<T>* searchone(BinaryTree<T>&);//查找子树
    BinaryNode<T>* searchhead(BinaryNode<T>*q,T key);//查找头结点
    bool matchtree(BinaryNode<T>*p, BinaryNode<T>*q);
  void remove(BinaryNode<T>*parent, bool leftchild);//删除parent节点的左或右子树//以中根和后根序列构造二叉树,删除所有与 pattern 匹配的子树。成员函数声明如下:
  BinaryNode<T>* removeAll(BinaryTree<T> & pattern);//BinaryNode<T>* removeAll(BinaryTree<T> &pattern) 
  void destroy();
private:
    void preOrder(BinaryNode<T>*p);//先根次序遍历以p节点为根的子树
    void postOrder(BinaryNode<T>*p, string &str);//后根
    void inOrder(BinaryNode<T>*p, string &str);//中根次序遍历以p节点为根的子树
    BinaryNode<T>* create(T postlist[], T inlist[], int postend, int inend, int n, BinaryNode<T>*);
    void destroy(BinaryNode<T>* p);
    
    


};

//判断树是否为空
template<class T>
bool BinaryTree<T>::empty()
{
    return this->root == NULL;
}
//构造空二叉树
template<class T>
BinaryTree<T>::BinaryTree()
{
    this->root = NULL;
}



//中根和后根构造二叉树
template<class T>
BinaryTree<T>::BinaryTree(T postlist[], T inlist[], int n)
{
    this->root = create(postlist, inlist, n - 1, n - 1, n, root);
}
template<class T>
BinaryNode<T>* BinaryTree<T>::create(T postlist[], T inlist[], int postend, int inend, int n, BinaryNode<T>*parent)
{
    BinaryNode<T>*p = NULL;
    if (n>0)
    {
        p = new BinaryNode<T>(postlist[postend]);
        int i = 0;
        while (i<n&&postlist[postend] != inlist[inend - i])
            i++;
        p->parent = parent;
        p->right = create(postlist, inlist, postend - 1, inend, i, p);
        p->left = create(postlist, inlist, postend - i - 1, inend - i - 1, n - i - 1, p);
    }
    return p;
}
//删除子树
template<class T>
void BinaryTree<T>::destroy(BinaryNode<T>*p)  
{
    if (p != NULL)
    {
        destroy(p->left);
        destroy(p->right);
        delete p;
    }

}



//析构
template<class T>
BinaryTree<T>::~BinaryTree()
{
    destroy(this->root);
}

template <class T>
void BinaryTree<T>::destroy()
{
    destroy(this->root);
}



//查找根节点
template<class T>
BinaryNode<T>* BinaryTree<T>::searchhead(BinaryNode<T>*q, T key)
{
    BinaryNode<T>*m = NULL;
    if (q != NULL)
    {
        if (q->data == key)
        {
            return q;
        }
        if ((m = searchhead(q->left, key)) == NULL)
            m = searchhead(q->right, key);
    }

    return    m;
}

//查找子树
template<class T>
BinaryNode<T>* BinaryTree<T>::searchone(BinaryTree<T>&bintree)
{
    BinaryNode<T>*p = searchhead(root, bintree.root->data);
    if (p != NULL)
    {
        if (matchtree(p, bintree.root))
            return p;
        else
            return NULL;
    }
    else
        return p;
}

//匹配子树
template<class T>
bool BinaryTree<T>::matchtree(BinaryNode<T>*p, BinaryNode<T>*q)
{
    if (q == NULL&&p == NULL)
        return true;
    if (p == NULL || q == NULL)
        return false;
    if (p->data != q->data)
        return false;
    return(matchtree(p->left, q->left) && matchtree(p->right, q->right));
}


//删除以X为根的子树







//删除
template<class T>
BinaryNode<T>*BinaryTree<T>::removeAll(BinaryTree<T>& pattern)//BinaryNode<T>* removeAll(BinaryTree<T> & pattern)
{
    BinaryNode<T>*q = searchone(pattern);
    if (q != NULL)
    {
        //(搜到的头不销毁)
        destroy(q->left);
        destroy(q->right);
        return NULL;
        
        

    }
    else
        return NULL;
    
}







//输出先根次序遍历的序列
template<class T>                                            //先根遍历
void BinaryTree<T>::preOrder()                          
{
    preshow(this->root);
}


template<class T>
void preshow(BinaryNode<T>* p) //先根
{
    if (p==NULL)
    {
        cout << "^";
    
    }
    else
    {
        
        cout << p->data;
   
     preshow(p->left);
   
      preshow(p->right);
    }
}


主函数

#include "BinaryTree.h"
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
int main(){
    char lalist[]="GDBEHFCA";
    char inlist[]="DGBAECHF";
    char inkey[]="DGB ";
    char lakey[]="GDB";
    BinaryTree<char> tree(lalist,inlist,8);
    BinaryTree<char> pattern(lakey,inkey,3);
    tree.preOrder()  ;
    cout<<endl;
    cout<<"二叉关键子树"<<endl;
     pattern.preOrder() ;
    cout<<endl;
    cout<<"删除后"<<endl;
    tree.removeAll( pattern);
    tree.preOrder() ;
    return 0;
}

运行结果及报错内容

img

img

我的解答思路和尝试过的方法
我想要达到的结果

删除后,A应该是单独一个节点,CEFH组成一个二叉树